i am copying object tt to ttt and i want to make change to ttt only but when i update ttt dunno why it update my tt along???? it's my makechange() function got problem?
this is main class:
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();
}
}
this is new class:
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]);
}
}
}
}
this is my output:
[100]
[123]
[99]
null
===============
[100]
[123]
[99]
null
this is my expected output should be like this:
[100]
[123]
null
null
===============
[100]
[123]
[99]
null