2015-01-26 2 views
1

내 코드에 약간의 문제가 있습니다. 클래스 생성자에 테이블 (배열)을 넣을 수 있습니까? int/string과 같은 타입을 넣을 때 클래스는 완벽하게 작동하지만 배열 int[][]을 클래스의 모든 객체 생성자에 넣으려고하면 항상 배열의 마지막 값을 반환합니다. 생성자 안에 테이블 (배열)을 두는 방법이 있습니까?배열을 포함하는 클래스의 객체를 만들 수 있습니까?

예제 코드 :

public class Population { 
int[][] population_object; 
int test; 

public Population(int[][] population_object){ 
    this.population_object = population_object; 
} 


public int[][] population_print(){ 
    return this.population_object; 
    } 

} 

그리고 그것을 사용 :

static ArrayList<Population> population_list; 
population_list = new ArrayList<Population>(); 
Population buff = new Population(pupulation_object_buffor); 
population_list.add(buff); 

이 마지막에 추가 테이블 (배열) 반환은 ... 물론 객체는 같은 테이블 (population_object_buffor) 포함되어 있지 않습니다.

int[][] test = population_list.get(s).population_print(); 

for(int k=0; k<test.length; k++){ 
       for(int kk=0; kk<test[k].length; kk++){ 
        System.out.print(" "+test[k][kk]); 
       } 
       System.out.println(); 
      } 
+1

는 관련 코드를 기입하십시오 – Eran

+2

'디버깅 도움 ("왜 ISN 추구하는 질문 이 코드는 작동하지 않습니까? ")에는 원하는 동작, 특정 문제 또는 오류 및 질문 자체에서이를 재현하는 데 필요한 가장 짧은 코드가 포함되어야합니다. 분명한 문제 설명이없는 질문은 다른 독자에게 유용하지 않습니다. See : 최소한의 완전하고 검증 가능한 예제를 만드는 방법. ': http://stackoverflow.com/help/mcve – EpicPandaForce

+0

나는 그것을 고쳤다. .. 미안. –

답변

1

그것은 당신이 당신의 Population 개체 초기화 방법에 따라 달라집니다 :

public class Population 
{ 
    private int[][] object; 

    public Population(int[][] object) { 
     this.object = object; 
    } 

    public void print() { 
     for (int i = 0; i < object.length; i++) { 
      for (int j = 0; j < object[i].length; j++) 
       System.out.print(object[i][j]); 
      System.out.println(); 
     } 
    } 
} 



public static void main(String[] args) { 
    ArrayList<Population> population = new ArrayList<>(); 

    // 5 Population 
    for (int nr = 1; nr <= 5; nr++) { 
     // each Population has an int[5][5] 
     int[][] object = new int[5][5]; 
     // int the object 
     for (int i = 0; i < 5; i++) 
      for (int j = 0; j < 5; j++) 
      object[i][j] = nr; 
     // add the Population with the object just created 
     population.add(new Population(object)); 
    } 

    // print out each Population object 
    for (int i = 0; i < population.size(); i++) { 
     System.out.println("Population " + (i + 1) + ":"); 
     population.get(i).print(); 
     System.out.println(); 
    } 
} 

을 그리고 출력은 다음과 같습니다

Population 1: 
11111 
11111 
11111 
11111 
11111 

Population 2: 
22222 
22222 
22222 
22222 
22222 

Population 3: 
33333 
33333 
33333 
33333 
33333 

Population 4: 
44444 
44444 
44444 
44444 
44444 

Population 5: 
55555 
55555 
55555 
55555 
55555 
+0

답장을 보내 주셔서 감사합니다. 아주 좋습니다! 하지만 .. 문제는 내 코드가 전부 네 코드 같아 보이지만 작동하지 않는다는 거지. 네가 작동한다는 걸 안다. 내가 왜 작동하지 않는지 도울 수 있니? 다음은 pastebin에 대한 링크입니다 : http://pastebin.com/MiSLzQpV –

+0

코드에 int [] [] pupulation_object_buffor 중 하나만 인스턴스가 있기 때문입니다. 이 인스턴스는 값을 변경하는'main()'에서 사용되며 모든 'Population' 객체를 통해 전달됩니다. 그래서, 여러분의'Population' 객체들은 모두'int [] []'객체의 동일한 인스턴스를 참조합니다. 보시다시피, 내 코드에서 각 [Population]에 대한 int [] [] 객체를 선언합니다 ('new int [5] [5];). –

+0

예! 그건 사실이야 .. 너는 내 하루를 만들어 줬어! 당신은 정말 많이 도와 줘요! 정말 대단히 고마워! :) –

관련 문제