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

Posted by Achilles on Stack Overflow See other posts from Stack Overflow or by Achilles
Published on 2010-04-27T12:17:12Z Indexed on 2010/04/27 12:23 UTC
Read the original article Hit count: 335

Filed under:
|

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

For example I performed the following test. ` [java]

  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");
[/java]
`

Returns false and prints "Not-equal.

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

[java]

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

[/java]

returns true and prints "Equal". Does the method only work with single dimensions? if so, is there something similar for multi-dimensional arrays in Java?

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays