Arrays.copyOf not working any help please ?[SOLVED]

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

The problem seems to be in this line:


int[][] test = new int[test2.length][test2[0].length];

The copying works just fine, but the problem is that you’re declaring a local instance of int[][] test within the copyTest function, which gets discarded after the function exits. Just remove the int[][] and you should be copying to the global array instead of the local one.

Thank you kindly Sir, i thought it must be some thing i had done, i just couldn’t see the wood for the trees.

Some thing I’d like to add here:
use


System.arraycopy(...);

(javadoc)

I’m not sure, and it seems like the javadoc doesn’t give information about this either, but I’m pretty sure that this method is inlined using C’s memcopy, which should be pretty fast.

Doesn’t

System.arraycopy(…);

just do a shallow copy of the reference to the array and not the array objects ?

as this array copy has to be done continuously i think i may well be working on this problem longer than i expected.

You can use it with 1D arrays. You can simply loop on all 1D arrays in your 2D arrays. There is no method to make a deep copy of multidimensional arrays in the standard API as far as I know. You should not use Arrays.copyOf in your case because your 2D array is already allocated, rather use System.arraycopy.