Java的满足Arrays.equals()的二维数组返回false [英] Java Arrays.equals() returns false for two dimensional arrays
问题描述
我只是好奇,想知道 - 为什么满足Arrays.equals(双[] [],双击[] [])返回假的?而实际上阵列具有相同数量的元素,每个元素是一样的吗?
例如我进行下面的测试。
双击[] [] A,B;
INT大小= 5;A =新的双[尺寸] [SIZE]
B =新的双[尺寸] [SIZE]的for(int i = 0; I<大小;我++)
对于(INT J = 0; J<大小; J ++){
一个[I] [J] = 1.0;
B〔I] [J] = 1.0;
}如果(满足Arrays.equals(a,b))的
的System.out.println(平等);
其他
的System.out.println(不等于);
返回false,并打印不等于。
在另一方面,如果我有这样的事情:
双击[] A,B;
INT大小= 5;A =新的双[尺寸]
B =新的双[尺寸]的for(int i = 0; I<大小;我++){
一个由[i] = 1.0;
B〔Ⅰ〕= 1.0;
}如果(满足Arrays.equals(a,b))的
的System.out.println(平等);
其他
的System.out.println(不等于);
返回true,并打印平等。请问方法仅采用单维度的工作原理?如果是的话,是有多维数组类似的东西在Java中?
使用<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#deepEquals%28java.lang.Object%5B%5D,%20java.lang.Object%5B%5D%29\"><$c$c>deepEquals(Object[],对象[]) 。
返回
真正
如果两个指定数组的深层相等的彼此。
块引用>由于一个
INT []
是的instanceof对象
,一个INT [] [ ]
是的instanceof对象[]
。至于为什么的
满足Arrays.equals
不工作的二维数组,它可以通过一步解释如下步骤:对于数组,
等于
在对象标识来定义的System.out.println(
(新INT [] {1,2})。等于(新INT [] {1,2})
); //输出假这是因为数组继承其
等于
从他们的公共超,<一个href=\"http://java.sun.com/javase/6/docs/api/java/lang/Object.html#equals%28java.lang.Object%29\"><$c$c>Object$c$c>.通常我们真的想为数组值相等,当然,这也就是为什么
java.util.Arrays中的
提供了静态
实用方法等于(INT [],INT [])
。的System.out.println(
java.util.Arrays.equals(
新的INT [] {1,2},
新的INT [] {1,2}
)
); //输出真在Java中数组的数组
- 的
INT []
是的instanceof对象
- 的
INT [] []
是的instanceof对象[]
- 的
INT [] []
为的不的的的instanceof INT []
Java不真的有二维数组。它甚至没有真正拥有多维数组。 Java有数组的数组。
java.util.Arrays.equals
是浅现在考虑这个片断:
的System.out.println(
java.util.Arrays.equals(
新的INT [] [] {
{1},
{2,3},
},
新的INT [] [] {
{1},
{2,3},
}
)
); //输出假下面是事实:
- 每个参数是一个
对象[]
- 在索引为0的元素是
INT [] {1}
- 在索引1的元素是
INT [] {2,3}
。- 有两个
对象[]
实例- 有四个
INT []
实例有应该从previous点,这将调用<清晰href=\"http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#equals%28java.lang.Object%5B%5D,%20java.lang.Object%5B%5D%29\"><$c$c>Arrays.equals(Object[],对象[]) 超载。从API:
返回
真正
如果对象
两个指定数组彼此相等。这两个数组被认为是等于
如果两个数组包含相同数量的元素,并且两个数组中元素的所有相应对都相等。两个物体E1
和E2
如果(E1 == NULL?E2被视为相等==空:e1.equals(E2))
块引用>现在应该很清楚为什么上面的片断输出
假
;这是因为对象的元素[]
数组不是由上述定义相等(因为一个INT []
有其等于
按对象的身份定义)。
java.util.Arrays.deepEquals
是深在此相反,这里是<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#deepEquals%28java.lang.Object%5B%5D,%20java.lang.Object%5B%5D%29\"><$c$c>Arrays.deepEquals(Object[],对象[]) 做:
返回
真正
如果两个指定数组的深受的彼此相等。不同于等于(Object []对象,对象[])
的方法,此方法适用于任意深度的嵌套数组。
块引用>的System.out.println(
java.util.Arrays.deepEquals(
新的INT [] [] {
{1},
{2,3},
},
新的INT [] [] {
{1},
{2,3},
}
)
); //输出真在
Arrays.toString
和Arrays.deepToString
值得注意的是这两种方法,我们已经与问候嵌套数组到目前为止讨论。之间有什么比喻
的System.out.println(
java.util.Arrays.toString(
新的INT [] [] {
{1},
{2,3},
}
)
); //输出[我@ 187aeca,[I @ e48e1b]的System.out.println(
java.util.Arrays.deepToString(
新的INT [] [] {
{1},
{2,3},
}
)
); //输出[[1],[2,3]]再次推理是相似的:
Arrays.toString(对象[])
将每个元素作为对象
,并调用它的的toString()
方法。数组继承其的toString()
从他们的公共超对象
。如果你想
java.util.Arrays中的
来考虑嵌套数组,你需要使用deepToString
,就像你需要使用deepEquals
。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.
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 "Not-equal".
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 the method only work with single dimensions? if so, is there something similar for multi-dimensional arrays in Java?
解决方案Use
deepEquals(Object[], Object[])
.Returns
true
if the two specified arrays are deeply equal to one another.Since an
int[]
is aninstanceof Object
, anint[][]
is aninstanceof Object[]
.
As to why
Arrays.equals
doesn't "work" for two dimensional arrays, it can be explained step by step as follows:For arrays,
equals
is defined in terms of object identitySystem.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 want value equality for arrays, of course, which is why
java.util.Arrays
provides thestatic
utility methodequals(int[], int[])
.System.out.println( java.util.Arrays.equals( new int[] {1,2}, new int[] {1,2} ) ); // prints "true"
Array of arrays in Java
- An
int[]
is aninstanceof Object
- An
int[][]
is aninstanceof Object[]
- An
int[][]
is NOT aninstanceof int[]
Java doesn't really have two dimensional arrays. It doesn't even really have multidimensional arrays. Java has 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 is an
Object[]
- The element at index 0 is an
int[] { 1 }
- The element at index 1 is an
int[] { 2, 3 }
.- There are two
Object[]
instances- There are four
int[]
instancesIt should be clear from the previous point that this invokes
Arrays.equals(Object[], Object[])
overload. From the API:Returns
true
if the two specified arrays ofObjects
are equal to one another. The two arrays are consideredequal
if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. Two objectse1
ande2
are considered equal if(e1==null ? e2==null : e1.equals(e2))
.Now it should be clear why the above snippet prints
"false"
; it's because the elements of theObject[]
arrays are not equal by the above definition (since anint[]
has itsequals
defined by object identity).
java.util.Arrays.deepEquals
is "deep"In contrast, here's what
Arrays.deepEquals(Object[], Object[])
does:Returns
true
if the two specified arrays are deeply equal to one another. Unlike theequals(Object[],Object[])
method, this method is appropriate 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
andArrays.deepToString
It's worth noting the analogy between these two methods and what we've discussed so far with regards to 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 anObject
, and just call itstoString()
method. Arrays inherit itstoString()
from their common superclassObject
.If you want
java.util.Arrays
to consider nested arrays, you need to usedeepToString
, just like you need to usedeepEquals
.这篇关于Java的满足Arrays.equals()的二维数组返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!