How can I copy an array by value in Java?

arrays, java

Solution

You need to clone your array.

test2=test.clone();

Problem

Possible Duplicate: Make copy of array Java I'm a beginner at Java and I need to copy the contents of one array into another variable. However, Java always passes the array by reference instead of by value. Here's what I mean if that was confusing: ``` int test[]={1,2,3,4}; int test2[]; test2=test; test2[2]=8; for(int i=0;i<test2.length;i++) System.out.print(test[i]); // Prints 1284 instead of 1234 ``` In this example, I don't want the value of `test` to change. Is this possible without using any of the more advanced features of Java such as ArrayList and Vectors? Edit: I tried System.ArrayCopy and test.clone(), but they still don't seem to work. Here's my actual code: ``` temp_image=image.clone(); for(int a=0;a<image.length;a++) for(int b=0;b<image[0].length;b++) image[a][b]=temp_image[image.length-1-a][b]; ``` Basically I'm trying to flip the "image" upside down. Is there an error somewhere in the code?

Original source

Related problems