Hi, im having a problem with trying to deep copy or even shallow copy an array, i have tried nearly every single code sample on the web nothing works what am i missing ?
assuming i have already created 2 new arrays and filled 1 with ints
int[][] test = new int[test2.length][test2[0].length];
for (int i=0; i <test2.length; i++) {
test[i] = Arrays.copyOf(test2[i],test2[i].length);
}
full code
import java.util.*;
public class test_copyarray {
public static int test[][] = new int[4][4];
public static int test2[][] = new int[4][4];
public static void dotest(){
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
System.out.println("test = "+test[i][j]);
}
}
}
public static void copytest(){
int[][] test = new int[test2.length][test2[0].length];
for (int i=0; i <test2.length; i++) {
test[i] = Arrays.copyOf(test2[i],test2[i].length);
}
}
public static void dotest2(){
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
test2[i][j]=i;
System.out.println("test2 = "+test2[i][j]);
}
}
}
public static void dotest3(){
int c=10;
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
test2[i][j]=c++;
System.out.println("test3 = "+test2[i][j]);
}
}
}
public static void dotest4(){
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
System.out.println("test4 = "+test[i][j]);
}
}
}
public static void main(String args []) {
dotest();
dotest2();
System.out.println("copying test2 to test");
copytest();
dotest();
System.out.println("changing data in test 2");
dotest3();
System.out.println("copying test2 to test");
copytest();
dotest4();
}
}
any pointers would be great thank you