Java Arrays.equals() 为二维数组返回 false [英] Java Arrays.equals() returns false for two dimensional arrays
问题描述
我只是想知道 - 为什么 Arrays.equals(double[][], double[][]) 返回 false?当实际上数组具有相同数量的元素并且每个元素都相同时?
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");
返回 false 并打印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");
返回 true 并打印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?
推荐答案
使用 deepEquals(Object[], Object[])
.
返回 true
如果两个指定的数组彼此深度相等.
Returns
true
if the two specified arrays are deeply equal to one another.
因为 int[]
是一个 instanceof Object
,所以 int[][]
是一个 instanceof Object[]代码>.
Since an int[]
is an instanceof Object
, an int[][]
is an instanceof Object[]
.
至于为什么 Arrays.equals
对二维数组不起作用,可以分步解释如下:
As to why Arrays.equals
doesn't "work" for two dimensional arrays, it can be explained step by step as follows:
System.out.println(
(new int[] {1,2}).equals(new int[] {1,2})
); // prints "false"
这是因为数组从它们共同的超类继承了它们的equals
,Object
.
This is because arrays inherit their equals
from their common superclass, Object
.
当然,我们通常真的希望数组的值相等,这就是为什么 java.util.Arrays
提供了 static
实用方法 equals(int[], int[])
.
Often we really want value equality for arrays, of course, which is why java.util.Arrays
provides the static
utility method equals(int[], int[])
.
System.out.println(
java.util.Arrays.equals(
new int[] {1,2},
new int[] {1,2}
)
); // prints "true"
Java 中的数组数组
int[]
是一个instanceof Object
int[][]
是一个instanceof Object[]
int[][]
是 NOT 一个instanceof int[]
- An
int[]
is aninstanceof Object
- An
int[][]
is aninstanceof Object[]
- An
int[][]
is NOT aninstanceof int[]
Array of arrays in Java
Java 并没有真正的二维数组.它甚至没有真正的多维数组.Java 有数组数组.
Java doesn't really have two dimensional arrays. It doesn't even really have multidimensional arrays. Java has array of arrays.
现在考虑这个片段:
System.out.println(
java.util.Arrays.equals(
new int[][] {
{ 1 },
{ 2, 3 },
},
new int[][] {
{ 1 },
{ 2, 3 },
}
)
); // prints "false"
事实如下:
- 每个参数都是一个
Object[]
- 索引 0 处的元素是
int[] { 1 }
- 索引 1 处的元素是
int[] { 2, 3 }
.
从上一点应该很清楚,这会调用
Arrays.equals(Object[], Object[])
重载.来自 API:It should be clear from the previous point that this invokes
Arrays.equals(Object[], Object[])
overload. From the API:如果
Objects
的两个指定数组彼此相等,则返回true
.如果两个数组包含相同数量的元素,并且两个数组中所有对应的元素对都相等,则认为这两个数组相等
.如果(e1==null ? e2==null : e1.equals(e2))
.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))
.现在应该清楚为什么上面的代码片段会打印
"false"
;这是因为Object[]
数组的元素根据上述定义不相等(因为int[]
的equals
由 object 定义身份).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).相比之下,这里是
Arrays.deepEquals(Object[], Object[])
:In contrast, here's what
Arrays.deepEquals(Object[], Object[])
does:如果两个指定的数组深度彼此相等,则返回
true
.与equals(Object[],Object[])
方法不同,该方法适用于任意深度的嵌套数组.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"
<小时>
在
Arrays.toString
和Arrays.deepToString
值得注意的是这两种方法之间的类比,以及我们迄今为止讨论的有关嵌套数组的内容.
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]]"
同样,推理是相似的:
Arrays.toString(Object[])
将每个元素视为一个Object
,并调用它的toString()代码>方法.数组从它们的公共超类
Object
继承它的toString()
.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
.如果你想让
java.util.Arrays
考虑嵌套数组,你需要使用deepToString
,就像你需要使用deepEquals
.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屋!
- 索引 0 处的元素是