How to copy an Array in Java
The methods described below are only applicable to one dimensional arrays. Before we talk about the different ways to copy an array in Java we will show you how NOT to copy an Array.
How NOT to copy an Array in Java
Arrays in Java are Objects. If you try to treat them as variables… well you can(!) but what you are really copying is the reference!. The example below explains this statement.
package com.mkyong.copyarray;
import java.util.Arrays;
public class HowNOTtoCopyAnArray {
public static void main(String[] args){
int[] x = {1, 2, 3, 4, 5};
int[] y = x; //don't copy array like this!
System.out.println(Arrays.toString(x));
System.out.println(Arrays.toString(y)+"\n");
x[1] = 22; // y[1] will display 22! same reference
System.out.println(Arrays.toString(x));
System.out.println(Arrays.toString(y)+"\n");
y[4] = 55; // x[4] will display 55!
System.out.println(Arrays.toString(x));
System.out.println(Arrays.toString(y));
}
}
Output:
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 22, 3, 4, 5]
[1, 22, 3, 4, 5]
[1, 22, 3, 4, 55]
[1, 22, 3, 4, 55]
1. Object.clone()
Arrays
inherit methods from Object
class, and clone
is one of them. If you need to copy an Array as it is then this is the method you should use.
package com.mkyong.copyarray;
import java.util.Arrays;
public class CloneArray {
public static void main(String[] args){
int[] x = {1, 2, 3, 4, 5};
int[] y = x.clone();
System.out.println(Arrays.toString(x));
System.out.println(Arrays.toString(y)+"\n");
x[1] = 22;
System.out.println(Arrays.toString(x));
System.out.println(Arrays.toString(y)+"\n");
y[4] = 55;
System.out.println(Arrays.toString(x));
System.out.println(Arrays.toString(y));
}
}
Output:
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 22, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 22, 3, 4, 5]
[1, 2, 3, 4, 55]
2. Arrays.copyOf()
In Arrays
class there are two methods that copy an array fully or partially. Here’s an example of copyOf()
method.
package com.mkyong.copyarray;
import java.util.Arrays;
public class ArraysCopyOfMethod {
public static void main(String[] args){
String[] x = {"one", "two", "three", "four", "five"};
String[] y = Arrays.copyOf(x, x.length);
String[] z = Arrays.copyOf(x, 3); //will copy the 3 first elements of array x
System.out.println("Array x: " + Arrays.toString(x));
System.out.println("Array y: " + Arrays.toString(y));
System.out.println("Array z: " + Arrays.toString(z));
}
}
Output:
Array x: [one, two, three, four, five]
Array y: [one, two, three, four, five]
Array z: [one, two, three]
3. Arrays.copyOfRange()
And this is an example of copyOfRange() method.
package com.mkyong.copyarray;
import java.util.Arrays;
public class ArraysCopyOfRangeMethod {
public static void main(String[] args){
String[] x = {"one", "two", "three", "four", "five"};
String[] y = Arrays.copyOfRange(x, 0, x.length); //full copy of the array
String[] z = Arrays.copyOfRange(x, x.length-2, x.length); //copy only the last 2 elements
System.out.println("Array x: " + Arrays.toString(x));
System.out.println("Array y: " + Arrays.toString(y));
System.out.println("Array z: " + Arrays.toString(z));
}
}
Output:
Array x: [one, two, three, four, five]
Array y: [one, two, three, four, five]
Array z: [four, five]
4. System.arraycopy()
With System.arraycopy()
you can control the range of elements from the source array that you want to copy,
and the destined position.
Review the System.arraycopy
signature (JavaDoc) :
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
package com.mkyong.copyarray;
import java.util.Arrays;
public class SystemArrayCopy {
public static void main(String[] args){
String[] x = {"one", "two", "three", "four", "five"};
String[] y = new String[2];
System.arraycopy(x, 3, y, 0, 2);
System.out.println("Array x: " + Arrays.toString(x));
System.out.println("Array y: " + Arrays.toString(y) + "\n");
Object[] z = new Object[5];
System.arraycopy(x, 0, z, 0, 5);
System.out.println("Array z: " + Arrays.toString(z)+"\n");
Integer[] w = {3, 4, 5};
System.out.println("Array w: " + Arrays.toString(w));
//copy from the second value (1) of array w to z and place in the fourth place (3) the 2 values
System.arraycopy(w, 1, z, 3, 2);
System.out.println("Array z: " + Arrays.toString(z));
}
}
Output:
Array x: [one, two, three, four, five]
Array y: [four, five]
Array z: [one, two, three, four, five]
Array w: [3, 4, 5]
Array z: [one, two, three, 4, 5]
Don’t forget to surround your code with try catch to handle Exceptions thrown
It was excellent.
Hi, how we can take the reference of JSON array? Can u help me?
Hello Manish, and thank you.
Let’s say you have the class Car and you create an Object for that class as follows:
Car myCar = new Car(“Toyota Avensis”);
Now let’s create another Car Object.
Car mySecondaryCar = new Car(“Toyota Yaris”);
If you type mySecondaryCar = myCar then your mySecondaryCar will now reference myCar and as a result it will have the value “Toyota Avensis” instead of “Toyota Yaris” that you declared when you created it.
You changed the reference and now it’s referencing exactly what myCar references.
Arrays in Java are Objects. So exactly the same thing happens to them too as shown in the Case 1.
I hope this helps you change your perspective of Arrays in Java and answers your question.
thank you
Very Nice Article, Can you please elaborate the case 1, and why we can not use
y= x;
in detail.
thank you