나는 ttt 객체를 복사하고 있는데, ttt 만 변경하려고합니다. 그러나 ttt dunno를 업데이트하면 왜 내 tt를 업데이트하나요? 그것은 내 makechange() 함수에 문제가 있습니까? 이 내 출력자바 복사 객체 및 복사 된 객체 업데이트
package test;
import java.util.ArrayList;
import java.util.List;
public class Solution {
Object[][] tt=new Object[2][2];
Object[][] ttt=new Object[2][2];
List l = new ArrayList<>();
public void add(){
l.add(100);
tt[0][0]=l;
l = new ArrayList<>();
l.add(123);
tt[0][1]=l;
l = new ArrayList<>();
}
public void disOld(){
for(int i=0; i<tt.length; i++){
for(int j=0; j<tt[i].length; j++){
System.out.println(tt[i][j]);
}
}
}
public void copy(){
ttt=tt;
}
public void makechange(){
l.add(99);
ttt[1][0]=l;
}
public void disNew(){
for(int i=0; i<ttt.length; i++){
for(int j=0; j<ttt[i].length; j++){
System.out.println(ttt[i][j]);
}
}
}
}
:
package test;
public class Test {
public static void main(String[] args) {
Solution sol;
sol= new Solution();
sol.add();
sol.copy();
//this makechange function only update ttt only!!
sol.makechange();
sol.disOld();
System.out.println("===============");
sol.disNew();
}
}
이이 새로운 클래스 :
이
메인 클래스입니다[100]
[123]
[99]
null
===============
[100]
[123]
[99]
null
이있다 이런 식으로해야 내 예상 출력 :
[100]
[123]
null
null
===============
[100]
[123]
[99]
null
당신이 코딩 나에게 자세한 내용을 표시 할 수 있습니다? – hellohai
@hellohai 저는 같은 주제에 관한 또 다른 답변을 연결했습니다. [Java로 객체를 복사하는 것은 처음에는 생각하기 쉽지 않습니다.] (http://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java). – m0skit0
대답은 복잡하고 목록 대신 객체와 관련이 없습니다. – hellohai