Comparing arrays for equality and Copying arrays


Comparing arrays for equality

Array types inherit their equals() (and hashCode()) implementations from java.lang.Object, so equals() will only return true when comparing against the exact same array object. To compare arrays for equality based on their values, use java.util.Arrays.equals, which is overloaded for all array types.

int[] array1 = new int[]{4, 5, 6};
int[] array2 = new int[]{4, 5, 6};
System.out.println(array1.equals(array2)); // Outputs "false" as array1 and array2 refer to different objects
System.out.println(Arrays.equals(array1, array2)); // Outputs "true" as the elements of array1 and array2 have the same values

When the element type is a reference type, Arrays.equals() calls equals() on the array elements to determine equality. In particular, if the element type is itself an array type, identity comparison will be used. To compare multidimensional arrays for equality, use Arrays.deepEquals() instead as below:

int[] array1 = { 4, 5, 6 };
int[] array2 = { 4, 5, 6 };
Object[] array1Object = { array1 }; // array1Object contains one element
Object[] array2Object = { array2 }; // array2Object contains one element
System.out.println(Arrays.equals(array1Object, array2Object)); // false
System.out.println(Arrays.deepEquals(array1Object, array2Object)); // true

Because sets and maps use equals() and hashCode(), arrays are generally not useful as set elements or map keys. Either wrap them in a helper class that implements equals() and hashCode() in terms of the array elements, or convert them to List instances and store the lists.


Copying arrays

Java provides several ways to copy an array.

for loop

int[] numbers = { 9, 5, 7, 3 };
int[] copiedNumbers = new int[numbers.length];
for (int i = 0; i < numbers.length; i++) {
   copiedNumbers[i] = numbers[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().

int[] a = { 9, 5, 7, 3 };
int[] b = a.clone(); // [9, 5, 7, 3]

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 provides an easy way to perform the copy of an array to another. Here is the basic usage:

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

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

int[] a = { 1, 2, 3, 4, 5 };
int[] b = new int[a.length];
System.arraycopy(a, 0, b, 0, a.length); // [1, 2, 3, 4, 5]

Arrays.copyOfRange()

Mainly used to copy a part of an Array, you can also use it to copy whole array to another as below:

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

Basic Programs