You might have an array that you need a copy of. This might occur if you need to do some sorting but do not want to destroy the original array.
Java provides four built in methods for making a copy of an array.
These methods are:
- Object.clone()
- Arrays.copyOf
- Arrays.copyOfRange
- System.arraycopy
The Arrays class is located in the java.util package.
Clone method
I'm going to start with clone because it is a bit interesting to note the theory behind it. This method we are going to use is defined in the Object class. You may ask, so what? What does this have to do with arrays? To answer this, let us take a look at declaring an object. How do you declare an object? To create an object we use the new operator.
Example:
Integer i = new Integer(5);
We use the new operator to allocate a new Object in memory. Now let us look at how we declare an array.
int[] nums = new int[3];
Notice, how we also use the new operator? So if we are using the new operator then the array must be an object. You can use various properties of the array such as length by typing .length after the array name. So this means that the array must be an object. A little side note for users of Netbeans. In Netbeans, you can press Ctrl-B on an objects name to open the class file for that object. I tried this with an array and I got nothing. So how is the array created? If you look at the Object class it is declared much like an interface but it's methods are declared to be native meaning they are not written in Java but some form of C or C++. This has nothing to do with cloning objects but is a bit interesting.
Example of cloning an array:
int[] nums = {3,2,4,5,6}; int[] a = nums.clone();
You can verify that this works by outputting the array. You do not get a reference to the array, and modifying one modifies the other. You get two exact copies of the array. This just as easily works with with multidimensional arrays.
int[][] nums = { {3,2,4,5}, {2,4,6,7}, {3,4,5,6} }; int[][] b = nums.clone();
Arrays.copyOf
This method is more flexible than the clone method. This method copies the array but you can specify the length of the new array.
If the new length is greater than the previous length then the end of the array is padded with default values. If the new length is less than the length of the array being copied then length characters are copied.
Consider this:
int[] nums = {3,2,5,6,9,10}; int[] c = Arrays.copyOf(nums,nums.length); // This makes an exact copy of the array. This is the same as nums.clone() int[] b = Arrays.copyOf(nums,3); // copies the first 3 items. The resulting array is {3,2,5} int[] a = Arrays.copyOf(nums,10); // copies the entire array and the remaining portion of the array is set to 0. The resulting array is {3,2,5,6,9,10,0,0,0,0}
Arrays.copyOfRange
This has to be the most flexible function for copying arrays. You specify the indices of the array to be copied.
The method prototype is as follows:
T[] Arrays.copyOfRange(T[] arr, int start, int end)
This means that it is a generic function. The method copies all the values in the range [start,end) into a new array and returns the new array.
Example:
int[] nums = {3,2,4,5,6,7,8,9,10,11}; int[] copy = Arrays.copyOfRange(nums,2,6); for (int i=0;i<copy.length;i++) { System.out.print(copy[i] + " "); } System.out.println();
This code creates a new array that consists of the elements in nums from index 2 to index 6 (but not including the item at index 6.)
System.arraycopy
This is another method that makes copies of arrays. However, I am not going to cover what it does. As I simply don't care.

Manual Copying
Another method for copying arrays is manual copying. Basically you use loops and manually assign items to a new array. Using this method to copy an array of N dimensions requires N nested loops. I'm sure you could use Arrays.copyOf and Arrays.copyOfRange with multidimensional arrays as well. Granted I haven't figured that out yet but I'm sure it is possible.
Enjoy!