Copy an object array in Java
This post will discuss how to copy an object array into a new array in Java.
We have discussed how to copy a primitive array into a new array in Java in the previous post. This post will discuss how we can copy an object array.
1. Using System.arraycopy() method
System.arraycopy() simply copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. If the source array contains reference elements, they are also copied.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Arrays; class Main { // Generic method to copy an object array from `source[]` to `dest[]` private static <T> void copyArray(T[] source, T[] dest) { System.arraycopy(source, 0, dest, 0, source.length); } // Program to copy an object array in Java public static void main(String[] args) { Integer[] source = { 1, 2, 3, 4, 5 }; Integer[] dest = new Integer[source.length]; copyArray(source, dest); if (Arrays.equals(source, dest)) { System.out.println("Copied successfully: " + Arrays.toString(dest)); } } } |
Output:
Copied successfully: [1, 2, 3, 4, 5]
The above code is equivalent to:
|
1 2 3 4 5 6 7 |
// Generic method to copy an object array from `source[]` to `dest[]` private static <T> void copyArray(T[] source, T[] dest) { for (int i = 0; i < source.length; i++) { dest[i] = source[i]; } } |
2. Using Object.clone() method
We know that arrays are objects in Java, and all methods of the Object class may be invoked on an array.
Object class has clone() method for copying objects in Java, and since arrays are treated as objects, we can use this method for copying arrays as well.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; class Main { // Method to copy an object array in Java private static <T> T[] copyArray(T[] source) { return source.clone(); } // Program to copy an object array in Java public static void main(String[] args) { Integer[] source = { 1, 2, 3, 4, 5 }; Integer[] dest = copyArray(source); if (Arrays.equals(source, dest)) { System.out.println("Copied successfully: " + Arrays.toString(dest)); } } } |
Output:
Copied successfully: [1, 2, 3, 4, 5]
Please note that the Object.clone() method creates a shallow copy of the array, i.e., the new array can reference the same elements as the original array. To illustrate, consider the following example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; class Main { // Program to demonstrate shallow copying of an array in Java public static void main(String[] args) { Integer[] i = { 1, 2, 3, 4, 5 }; // source array contains a reference Object[] source = { i }; // copying using `clone()` Object[] dest = source.clone(); if (source[0] == dest[0]) { System.out.println("Shallow copy"); } } } |
Output:
Shallow copy
3. Using Arrays.copyOf() method
Arrays.copyOf() can also be used to copy the specified array into a new array, as shown below. If the specified array contains any reference elements, this method also performs a shallow copy.
|
1 2 3 4 |
// Method to copy an object array in Java private static <T> T[] copyArray(T[] source) { return Arrays.copyOf(source, source.length); } |
Arrays.copyOf() has an overloaded version where we can specify the Type of the resulting array.
|
1 2 3 4 5 6 |
// source array Number[] source = { 1, 2, 3, 4, 5 }; // get `Integer[]` from `Number[]` Integer[] dest = Arrays.copyOf(source, source.length, Integer[].class); |
4. Using Arrays.copyOfRange() method
Arrays also provides the copyOfRange() method, which is similar to copyOf() but only copies elements between the specified range into a new array.
|
1 2 3 4 |
// Method to copy an object array in Java private static <T> T[] copyArray(T[] source) { return Arrays.copyOfRange(source, 0, source.length); } |
Similar to Arrays.copyOf(), a shallow copy of the original array is created, and it also has an overloaded version where we can specify the Type of the resulting array.
5. Serialization of object array using GSON
We have seen that in all approaches discussed above, a shallow copy of the array is created, but this is something we might not want. One simple solution to this problem is Serialization. To illustrate, the following code returns a deep copy of an array by serializing the array with Google’s Gson library.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import com.google.gson.Gson; class Main { // Program to demonstrate deep copying of an array in Java public static void main(String[] args) { Integer[] i = { 1, 2, 3, 4, 5 }; // source array contains a reference Object[] source = { i }; // 1. Create a Gson object Gson gson = new Gson(); // Serialize object array String jsonString = gson.toJson(source); // Unserialize object array Object[] dest = gson.fromJson(jsonString, Object[].class); if (source[0] == dest[0]) { System.out.println("Shallow copy"); } else { System.out.println("Deep copy"); } } } |
Output:
Deep copy
That’s all about copying an object array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)