How to copy an array in Java?

To copy an array in Java, do this: Arrays.copyOf(array, array.length).

Here's how you do it:

int[] array = new int[] { 1, 2, 3 };
int[] copyOfArray = java.util.Arrays.copyOf(array, array.length);

// [1, 2, 3]
System.out.println(java.util.Arrays.toString(array));
// [1, 2, 3]
System.out.println(java.util.Arrays.toString(copyOfArray));