Comparing Arrays

The java.util.Arrays class provides several methods to compare arrays effectively: Arrays.equals() for one-dimensional arrays and Arrays.deepEquals() for nested (multi-dimensional) arrays. These methods make it easy to verify if two arrays are equal in content, considering element-wise comparison rather than comparing object references.

Using Arrays.equals() for One-Dimensional Arrays

The Arrays.equals() method is designed to compare the elements of two one-dimensional arrays. It checks if both arrays have the same length and if corresponding elements in each array are equal. For primitive types, it uses value comparison, and for objects, it uses equals() on each pair of corresponding elements.

Example with Primitive Arrays:

int[] array1 = {1, 2, 3};
int[] array2 = {1, 2, 3};
int[] array3 = {1, 2, 4};

System.out.println(Arrays.equals(array1, array2)); // Output: true
System.out.println(Arrays.equals(array1, array3)); // Output: false

Example with Object Arrays:

String[] array1 = {"apple", "banana"};
String[] array2 = {"apple", "banana"};
String[] array3 = {"apple", "orange"};

System.out.println(Arrays.equals(array1, array2)); // Output: true
System.out.println(Arrays.equals(array1, array3)); // Output: false

Using Arrays.deepEquals() for Multi-Dimensional Arrays

For nested or multi-dimensional arrays, Arrays.equals() only compares references in the inner arrays, which won’t give the desired result. To handle nested arrays properly, use Arrays.deepEquals(), which performs a deep comparison of each nested element in multi-dimensional arrays, checking every level of nesting for equality.

Example with Nested Arrays:

int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}};
int[][] matrix2 = {{1, 2, 3}, {4, 5, 6}};
int[][] matrix3 = {{1, 2, 3}, {4, 5, 7}};

System.out.println(Arrays.equals(matrix1, matrix2));      // Output: false (only checks references)
System.out.println(Arrays.deepEquals(matrix1, matrix2));  // Output: true
System.out.println(Arrays.deepEquals(matrix1, matrix3));  // Output: false

In this example, Arrays.deepEquals() correctly identifies matrix1 and matrix2 as equal, while Arrays.equals() does not, because it only checks if the inner arrays are the same objects, not if they have identical content.

Important Notes on Arrays.equals() and Arrays.deepEquals()

  1. Shallow vs. Deep Comparison: Arrays.equals() performs a shallow comparison, suitable only for one-dimensional arrays, while Arrays.deepEquals() performs a deep comparison, making it suitable for multi-dimensional or nested arrays.
  2. Null Elements: Both methods handle null values. If both arrays being compared contain null in corresponding positions, they are considered equal.
  3. Performance: While Arrays.equals() is faster for simple comparisons, Arrays.deepEquals() has additional overhead because it must recursively check each level of nested arrays.

Summary

  • Arrays.equals(): Use for one-dimensional arrays of both primitives and objects.
  • Arrays.deepEquals(): Use for multi-dimensional or nested arrays to ensure a true deep comparison.