Java Arrays.equals () returns false for two-dimensional arrays

I was just curious to know - why Arrays.equals (double [] [], double [] []) returns false? when in fact arrays have the same number of elements, and each element is the same?

For example, I performed the following test.

double[][] a, b; int size =5; a=new double[size][size]; b=new double[size][size]; for( int i = 0; i < size; i++ ) for( int j = 0; j < size; j++ ) { a[i][j]=1.0; b[i][j]=1.0; } if(Arrays.equals(a, b)) System.out.println("Equal"); else System.out.println("Not-equal"); 

Returns false and prints "Uneven".

on the other hand, if I have something like this:

 double[] a, b; int size =5; a=new double[size]; b=new double[size]; for( int i = 0; i < size; i++ ){ a[i]=1.0; b[i]=1.0; } if(Arrays.equals(a, b)) System.out.println("Equal"); else System.out.println("Not-equal"); 

returns true and prints "Equal". Does this method only work with individual dimensions? if so, something like this for multidimensional arrays in Java?

+33
java arrays
Apr 27 '10 at 12:17
source share
2 answers

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}) ); // prints "false" 

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} ) ); // prints "true" 

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 .

+101
Apr 27 '10 at 12:18
source share

java doesn't actually have multidimensional arrays. Instead, it has only a one-dimensional array, and multi d arrays will be arrays of these 1d arrays. String.equals () can only be performed on basic single-block arrays and therefore does not work for multidimensional measurements.

0
Apr 09 '13 at 1:31
source share



All Articles