Java: Testing array equality

if (Arrays.equals(arr1, arr2)) {
    ...
}

Note that this performs a shalow comparison, i.e. it checks

  • arr1[0].equals(arr2[0]),
  • arr1[1].equals(arr2[1]),

This is not suitable for multidimensional arrays.

Multidimensional arrays

if (Arrays.deepEquals(arr1, arr2)) {
    ...
}

This method recurses on the arrays and compares the actual elements.

Comments

Be the first to comment!