Given an array, we need to copy its elements in a different array.
We might be tempted to proceed like this:
int a[] = { 1 , 8 , 3 }; // Create an array b[] of same size as a[] int b[] = new int [a.length]; // Doesn't copy elements of a[] to b[], only makes // b refer to same location b = a; |
However it’s incorrect!
When we do “b = a”, we actually assigning reference of array. Hence if we make any change to one array, it would be reflected in other array as well because both a and b refer to same location.
// A Java program to demonstrate that simply assigning one array // reference is incorrect. public class Test { public static void main(String[] args) { int a[] = { 1 , 8 , 3 }; // Create an array b[] of same size as a[] int b[] = new int [a.length]; // Doesn't copy elements of a[] to b[], only makes // b refer to same location b = a; // Change to b[] will also reflect in a[] as 'a' and // 'b' refer to same location. b[ 0 ]++; System.out.println( "Contents of a[] " ); for ( int i= 0 ; i<a.length; i++) System.out.print(a[i] + " " ); System.out.println( "\n\nContents of b[] " ); for ( int i= 0 ; i<b.length; i++) System.out.print(b[i] + " " ); } } |
Output:
Contents of a[] 2 8 3 Contents of b[] 2 8 3
We might iterate each element of the given original array and copy one element at a time. Using this method guarantees that any modifications to b, will not alter the original array a.
// A Java program to demonstrate copying by one by one // assigning elements of a[] to b[]. public class Test { public static void main(String[] args) { int a[] = { 1 , 8 , 3 }; // Create an array b[] of same size as a[] int b[] = new int [a.length]; // Copy elements of a[] to b[] for ( int i= 0 ; i<a.length; i++) b[i] = a[i]; // Change b[] to verify that b[] is different // from a[] b[ 0 ]++; System.out.println( "Contents of a[] " ); for ( int i= 0 ; i<a.length; i++) System.out.print(a[i] + " " ); System.out.println( "\n\nContents of b[] " ); for ( int i= 0 ; i<b.length; i++) System.out.print(b[i] + " " ); } } |
Output:
Contents of a[] 1 8 3 Contents of b[] 2 8 3
In the previous method we had to iterate over the entire array to make a copy, can we do better? The answer is YES!
We can use clone method in Java.
// A Java program to demonstrate array copy using clone() public class Test { public static void main(String[] args) { int a[] = { 1 , 8 , 3 }; // Copy elements of a[] to b[] int b[] = a.clone(); // Change b[] to verify that b[] is different // from a[] b[ 0 ]++; System.out.println( "Contents of a[] " ); for ( int i= 0 ; i<a.length; i++) System.out.print(a[i] + " " ); System.out.println( "\n\nContents of b[] " ); for ( int i= 0 ; i<b.length; i++) System.out.print(b[i] + " " ); } } |
Output:
Contents of a[] 1 8 3 Contents of b[] 2 8 3
We can also use System.arraycopy() Method. System is present in java.lang package. Its signature is as :
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Where src denotes the source array, srcPos is the index from which copying starts. Similarly, dest denotes the destination array, destPos is the index from which the copied elements are placed in the destination array. length is the length of subarray to be copied. The below program illustrates the same:
// A Java program to demonstrate array copy using // System.arraycopy() public class Test { public static void main(String[] args) { int a[] = { 1 , 8 , 3 }; // Create an array b[] of same size as a[] int b[] = new int [a.length]; // Copy elements of a[] to b[] System.arraycopy(a, 0 , b, 0 , 3 ); // Change b[] to verify that b[] is different // from a[] b[ 0 ]++; System.out.println( "Contents of a[] " ); for ( int i= 0 ; i<a.length; i++) System.out.print(a[i] + " " ); System.out.println( "\n\nContents of b[] " ); for ( int i= 0 ; i<b.length; i++) System.out.print(b[i] + " " ); } } |
Output:
Contents of a[] 1 8 3 Contents of b[] 2 8 3
Overview of above methods:
- Simply assigning reference is wrong
- Array can be copied by iterating over array, and one by one assigning elements.
- We can avoid iteration over elements using clone() or System.arraycopy()
- clone() creates a new array of same size, but System.arraycopy() can be used to copy from a source range to a destination range.
- System.arraycopy() is faster than clone() as it uses Java Native Interface (Source : StackOverflow)
This article is contributed by Ashutosh Kumar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.