Use deepEquals(Object[], Object[]) .
Returns true if the two specified arrays are deeply equal to each other.
Since a int[] is an instanceof Object , a int[][] is an instanceof Object[] .
As to why Arrays.equals does not work for two-dimensional arrays, it can be explained step by step as follows:
For arrays, equals is defined in terms of object identity
System.out.println( (new int[] {1,2}).equals(new int[] {1,2}) );
This is because arrays inherit their equals from their common superclass, Object .
Often we really need equal values ββfor arrays, and that is why java.util.Arrays provides the static equals(int[], int[]) utility equals(int[], int[]) .
System.out.println( java.util.Arrays.equals( new int[] {1,2}, new int[] {1,2} ) );
Array Arrays in Java
- An
int[] is instanceof Object - An
int[][] is an instanceof Object[] int[][] NOT a instanceof int[]
There are no two-dimensional arrays in Java. It does not even have multidimensional arrays. Java has an array of arrays.
java.util.Arrays.equals is "shallow"
Now consider this snippet:
System.out.println( java.util.Arrays.equals( new int[][] { { 1 }, { 2, 3 }, }, new int[][] { { 1 }, { 2, 3 }, } ) ); // prints "false"
Here are the facts:
- Each argument to
Object[]- The element with index 0 is
int[] { 1 } - The element with index 1 is
int[] { 2, 3 } .
- There are two instances of
Object[] - There are four instances of
int[]
From the previous point, it should be clear that this causes an overload of Arrays.equals(Object[], Object[]) . From the API:
Returns true if the two specified Objects arrays are equal to each other. Two arrays are considered equal if both arrays contain the same number of elements, and all the corresponding pairs of elements in two arrays are equal. Two objects e1 and e2 are considered equal if (e1==null ? e2==null : e1.equals(e2)) .
It should now be clear why the above snippet prints "false" ; because the elements of the Object[] array are not equal to the above definition (since int[] has its own equals defined by the identifier of the object).
java.util.Arrays.deepEquals is "deep"
In contrast, here is Arrays.deepEquals(Object[], Object[]) :
Returns true if the two specified arrays are deeply equal to each other. Unlike the equals(Object[],Object[]) method, this method is suitable for use with nested arrays of arbitrary depth.
System.out.println( java.util.Arrays.deepEquals( new int[][] { { 1 }, { 2, 3 }, }, new int[][] { { 1 }, { 2, 3 }, } ) ); // prints "true"
On Arrays.toString and Arrays.deepToString
It is worth noting the analogy between the two methods and what we have discussed so far regarding nested arrays.
System.out.println( java.util.Arrays.toString( new int[][] { { 1 }, { 2, 3 }, } ) ); // prints "[[I@187aeca, [I@e48e1b]" System.out.println( java.util.Arrays.deepToString( new int[][] { { 1 }, { 2, 3 }, } ) ); // prints "[[1], [2, 3]]"
Again, the reasoning is similar: Arrays.toString(Object[]) treats each element as an Object and simply calls its toString() method. Arrays inherit their toString() from their common superclass Object .
If you want java.util.Arrays consider nested arrays you need to use deepToString as you need to use deepEquals .