1. Overview
In this tutorial, You'll learn how to copy the array from one to another without loosing any values.
If you are new to java programming, learn how to create an array in java in various ways?
Copying an array can be done in multiple ways. Let us discuss all possible ways.
Read the article till end to see best option for your case.
2. Copy Array Using Another Reference
Let us first create an array with 5 values and assign to a variable originalArray.
In the next step, create a new reference variable named "copiedArray" and assign the originalArray to copiedArray.
Further, printing the two arrays on the console using "Arrays.toString()" method.
import java.util.Arrays; public class CopyArrayRefAssignment { public static void main(String[] args) { // creating an array int[] originalArray = { 1, 2, 3, 4, 5 }; // assigning to the original array to new ref. int[] copiedArray = originalArray; // printing the arrays System.out.println("Original Array : " + Arrays.toString(originalArray)); System.out.println("Copied Array : " + Arrays.toString(copiedArray)); // changing the values in the original array originalArray[0] = 100; originalArray[4] = 500; // printing both arrays after modification System.out.println("\nPrinting values after original array modifications"); System.out.println("Modified Original Array : " + Arrays.toString(originalArray)); System.out.println("Copied Array : " + Arrays.toString(copiedArray)); } }
Output:
Original Array : [1, 2, 3, 4, 5] Copied Array : [1, 2, 3, 4, 5] Printing values after original array modifications Modified Original Array : [100, 2, 3, 4, 500] Copied Array : [100, 2, 3, 4, 500]
From the above output, you could see that originalArray and copiedArray have the same values.
But, we changed the values of original array using its index. After that printed the original and copied arrays.
You might have noticed that copiedArray values are changed as per the modified original array.
Hence, it is not advisable to use the array assignment if original array is going to be modified in future because two references points to same object in the heap memory area.
3. Copy Array Using Iteration
Second approach is to iterate the array using for loop and assign each value to new array.
public class CopyArrayRefIterateExample { public static void main(String[] args) { // creating an array int[] originalArray = { 1, 2, 3, 4, 5 }; // assigning to the original array to new ref. int[] copiedArray = new int[originalArray.length]; // iterating array and assigning to the new array for (int i = 0; i < originalArray.length; i++) { copiedArray[i] = originalArray[i]; } // printing the arrays System.out.println("Original Array : " + Arrays.toString(originalArray)); System.out.println("Copied Array : " + Arrays.toString(copiedArray)); // changing the values in the original array originalArray[0] = 100; originalArray[4] = 500; // printing both arrays after modification System.out.println("\nPrinting values after original array modifications"); System.out.println("Modified Original Array : " + Arrays.toString(originalArray)); System.out.println("Copied Array : " + Arrays.toString(copiedArray)); } }
Output:
Original Array : [1, 2, 3, 4, 5] Copied Array : [1, 2, 3, 4, 5] Printing values after original array modifications Modified Original Array : [100, 2, 3, 4, 500] Copied Array : [1, 2, 3, 4, 5]
In this approach, we need to copy the each value to new array. You can observe the output is that any changes to the original array does not affect the copied array.
4. Copy Array Using Clone Approach
Next third approach, you'll see the example program using clone() method. You can understand the differences between deep copy vs shallow copy cloning in java.
public class CopyArrayCloneExample { public static void main(String[] args) { // creating an array int[] originalArray = { 1, 2, 3, 4, 5 }; // cloning array using clone() method int[] copiedArray = originalArray.clone(); // printing the arrays System.out.println("Original Array : " + Arrays.toString(originalArray)); System.out.println("Copied Array : " + Arrays.toString(copiedArray)); // changing the values in the original array originalArray[0] = 100; originalArray[4] = 500; // printing both arrays after modification System.out.println("\nPrinting values after original array modifications"); System.out.println("Modified Original Array : " + Arrays.toString(originalArray)); System.out.println("Copied Array : " + Arrays.toString(copiedArray)); } }
Output:
Original Array : [1, 2, 3, 4, 5] Copied Array : [1, 2, 3, 4, 5] Printing values after original array modifications Modified Original Array : [100, 2, 3, 4, 500] Copied Array : [1, 2, 3, 4, 5]
This approach works fine as similar to the above approach.
5. Copy Array Using System.arraycopy() Method
In the last approach, you'll use the System.arraycopy() method which does copy the values from original array for a given index range.
public class CopyArraySystemArraycopyExample { public static void main(String[] args) { // creating an array int[] originalArray = { 1, 2, 3, 4, 5 }; // Creating a new array as same as original. int[] copiedArray = new int[originalArray.length]; // cloning array using clone() method System.arraycopy(originalArray, 0, copiedArray, 0, originalArray.length); // printing the arrays System.out.println("Original Array : " + Arrays.toString(originalArray)); System.out.println("Copied Array : " + Arrays.toString(copiedArray)); // changing the values in the original array originalArray[0] = 100; originalArray[4] = 500; // printing both arrays after modification System.out.println("\nPrinting values after original array modifications"); System.out.println("Modified Original Array : " + Arrays.toString(originalArray)); System.out.println("Copied Array : " + Arrays.toString(copiedArray)); } }
This approach also work well and could see it as used in many collections api internally.
Hence, this approach is better choice if you want to copy the large data set which is stored in array.
6. Conclusion
In this article, you've seen the various ways to copy array to another in java.
GitHub sample code
No comments:
Post a Comment
Please do not add any spam links in the comments section.