Java provides several ways to copy an array.

for loop

int[] a = { 4, 1, 3, 2 };
int[] b = new int[a.length]; 
for (int i = 0; i < a.length; i++) {
    b[i] = a[i];
}

Note that using this option with an Object array instead of primitive array will fill the copy with reference to the original content instead of copy of it.

Object.clone()

Since arrays are Objects in Java, you can use [Object.clone()](<https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#clone()>).

int[] a = { 4, 1, 3, 2 };
int[] b = a.clone(); // [4, 1, 3, 2]

Note that the Object.clone method for an array performs a shallow copy, i.e. it returns a reference to a new array which references the same elements as the source array.


Arrays.copyOf()

[java.util.Arrays](<https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html>) provides an easy way to perform the copy of an array to another. Here is the basic usage:

int[] a = {4, 1, 3, 2};
int[] b = Arrays.copyOf(a, a.length); // [4, 1, 3, 2]

Note that Arrays.copyOf also provides an overload which allows you to change the type of the array:

Double[] doubles = { 1.0, 2.0, 3.0 };
Number[] numbers = Arrays.copyOf(doubles, doubles.length, Number[].class);

System.arraycopy()

`public static void arraycopy(Object src,

int srcPos,
Object dest,
int destPos,
int length)` Copies an array from the specified source array,

beginning at the specified position, to the specified position of the destination array.

Below an example of use