2014-10-24 4 views
0

나는 문제가있다. 그리고 나는 이것에 관해 전혀 모른다. 그래서 나는 유사한 코딩을하고, 나의 질문을 게시한다, 사람이 나를 도울 수 있기를 바란다.데이터를 다른 객체에 저장하지만 덮어 쓴다

package test; 

import java.util.ArrayList; 
import java.util.List; 

public class newclass { 

private Object[][] array; 
private int x, y; 
List<Integer> slot = new ArrayList<>(); 

public newclass(Object[][]array, int x, int y){ 
    this.array = array; 
    this.x = x; 
    this.y = y; 
} 

public void storedata(){ 
    int i=0; 
    while(i < 2){ 
     int a,b; 
     a=(int) Math.floor(Math.random()*(x)); 
     b=(int) Math.floor(Math.random()*(y)); 
     if(NC[a][b]==null){ 
      slot.add(0); 
      NC[a][b] = slot; 
      slot = new ArrayList<>(); 
     } 
     else{ 
      List templist = (List)NC[a][y]; 
      templist.add(0); 
     } 
     i++; 
     } 
} 

public void display(){ 
    for(int i=0; i<x ;i++){ 
     for(int j=0; j<y ;j++){ 
      List templist = (List)array[i][j]; 
      if(templist==null){ 
       System.out.print(""); 
      } 
      else{ 
      for (int k = 0; k < templist.size(); k++) { 
       System.out.print(templist.get(k)); 
       } 
      } 
      System.out.print(",");  
     } 
     System.out.print("\n"); 
    } 
} 

} 

내 문제는 덮어 메인 클래스에서 두 번째 루프에있는 "0 |":이 내 새로운 클래스가

package test; 

public class Test { 

public static void main(String[] args) { 
    newclass [] NC; 
    NC = new newclass[2]; 
    Object[][] array = new Object[5][5]; 
    int x=5, y=5; 
    for (int i=0 ; i<2 ; i++){ 
     NC[i]= new newclass(array, x, y); 
    } 

    //solve roomHC 
    for (int i=0 ; i<2 ; i++){ 
     NC[i].storedata(); 
    } 

    //display solution 
    for (int i=0 ; i<2 ; i++){ 
     System.out.print("\n"); 
     NC[i].display(); 
     System.out.print("========================"); 
    } 
} 
} 

입니다

이 내 메인 클래스입니다 배열에 다시 입력하십시오.

+0

어떻게 당신이 이제까지 일관된 결과를 기대할 수'인 Math.random()는'a와 b를 설정하는 방법? 또한 'slot = new ArrayList <>();'는 루프의 각 반복문을 다시 초기화하여 어떤 값도 보유하지 않도록한다는 것을 의미합니다. – hooda

+0

내 질문을 편집 했으므로 다시 점검 할 수 있습니다 .. – user3832964

+0

이제 Math.random()을 제거 했으므로 두 객체 모두에서 동일한 결과가 나타납니다. 두 개체의 두 번째 열에 0을 넣을뿐입니다. 당신이 여기서 무엇을하려하는지 명확하게 알지 못합니다. – RealSkeptic

답변

2

newclass의 두 인스턴스에 동일한 객체 배열을 전달하고 있습니다. 그래서 당신이 그것들 중 하나에서 그것을 바꿀 때, 변화는 다른 배열 내부를 가리키는 동일한 배열로 만들어집니다.

배열이나 개체를 메서드에 전달할 때 참조이 아닌 새 복사본이 전달됩니다.

newclass 인스턴스에 외부, 개체 배열에서 액세스 할 수없는 별도의 개인 개체를 사용하려면 생성자 내에 new을 사용해야합니다. 이 같은

public newclass(int x, int y){ 
    this.NC = new Object[x][y]; 
    this.x = x; 
    this.y = y; 
} 

그리고 그것을 사용 : 예를 들어 당신이 사용하는 경우

public static void main(String[] args) { 
    newclass [] NC; 
    NC = new newclass[2]; 

    int x=5, y=5; 
    for (int i=0 ; i<2 ; i++){ 
     NC[i]= new newclass(x,y); 
    } 

    //solve roomHC 
    for (int i=0 ; i<2 ; i++){ 
     NC[i].storedata(); 
    } 

    //display solution 
    for (int i=0 ; i<2 ; i++){ 
     System.out.print("\n"); 
     NC[i].display(); 
     System.out.print("========================"); 
    } 
} 
+0

미안하지만, 설명 할 필요가 없습니다. 메인 클래스에서 배열 NC [5] [5] – user3832964

+0

의 크기를 선언하고 내 코드에 조언을 추가하려고하지만 변경이 없습니다. – user3832964

+0

코드에 내 메서드를 추가하면 안됩니다. 배열을 받아들이는 생성자 대신에 내가 준 생성자를 사용해야한다. 메인 클래스에 배열을 생성하면 안됩니다. – RealSkeptic

관련 문제