When comparing arrays in Java, are there any differences between the following 2 statements?
当比较Java中的数组时,下面两个语句之间有什么区别吗?
array1.equals(array2);
Arrays.equals(array1, array2);
And if so what are they?
如果有的话,他们是什么?
8 个解决方案
#1
254
array1.equals(array2)
is the same as array1 == array2
, i.e. is it the same array. As @alf points out it's not what most people expect.
array1.equals(array2)与array1 == array2相同,即它是相同的数组。正如@alf所指出的,这并不是大多数人所期望的。
Arrays.equals(array1, array2)
compares the contents of the arrays.
数组。equals(array1, array2)比较数组的内容。
Similarly array.toString()
may not be very useful and you need to use Arrays.toString(array)
.
同样,array.toString()可能不是很有用,您需要使用Arrays.toString(数组)。
#2
72
It's an infamous problem: .equals()
for arrays is badly broken, just don't use it, ever.
这是一个臭名昭著的问题:对于数组来说,.equals()是严重损坏的,只是不要使用它。
That said, it's not "broken" as in "someone has done it in a really wrong way" — it's just doing what's defined and not what's usually expected. So for purists: it's perfectly fine, and that also means, don't use it, ever.
也就是说,它并不是像“某人以一种非常错误的方式完成的”那样“支离破碎”——它只是做了定义,而不是通常预期的。对于纯粹主义者来说,这是完美的,这也意味着,永远不要使用它。
Now the expected behaviour for equals
is to compare data. The default behaviour is to compare the identity, as Object
does not have any data (for purists: yes it has, but it's not the point); assumption is, if you need equals
in subclasses, you'll implement it. In arrays, there's no implementation for you, so you're not supposed to use it.
现在,对等的期望行为是比较数据。默认行为是比较身份,因为对象没有任何数据(纯粹主义者:是的,它有,但不是重点);假设是,如果您需要在子类中相等,您将实现它。在数组中,没有实现,所以不应该使用它。
So the difference is, Arrays.equals(array1, array2)
works as you would expect (i.e. compares content), array1.equals(array2)
falls back to Object.equals
implementation, which in turn compares identity, and thus better replaced by ==
(for purists: yes I know about null
).
不同之处在于,数组。= (array1, array2)按照您的期望工作(即比较内容),array1.equals(array2)返回对象。=实现,它依次比较标识,因此更好地替换为==(纯粹主义者:是的,我知道null)。
Problem is, even Arrays.equals(array1, array2)
will bite you hard if elements of array do not implement equals
properly. It's a very naive statement, I know, but there's a very important less-than-obvious case: consider a 2D array.
问题是,即使数组。= (array1, array2)如果数组元素没有正确地实现,将会咬你。这是一个很幼稚的说法,我知道,但是有一个非常重要的不太明显的例子:考虑一个2D数组。
2D array in Java is an array of arrays, and arrays' equals
is broken (or useless if you prefer), so Arrays.equals(array1, array2)
will not work as you expect on 2D arrays.
在Java中,2D数组是数组的数组,数组的equals是被破坏的(如果你喜欢的话也没用),所以数组。= (array1, array2)不会像你期望的2D数组那样工作。
Hope that helps.
希望有帮助。
#3
15
Look inside the implementation of the two methods to understand them deeply:
深入了解这两种方法的实施情况,深入了解它们:
array1.equals(array2);
/**
* Indicates whether some other object is "equal to" this one.
* <p>
* The {@code equals} method implements an equivalence relation
* on non-null object references:
* <ul>
* <li>It is <i>reflexive</i>: for any non-null reference value
* {@code x}, {@code x.equals(x)} should return
* {@code true}.
* <li>It is <i>symmetric</i>: for any non-null reference values
* {@code x} and {@code y}, {@code x.equals(y)}
* should return {@code true} if and only if
* {@code y.equals(x)} returns {@code true}.
* <li>It is <i>transitive</i>: for any non-null reference values
* {@code x}, {@code y}, and {@code z}, if
* {@code x.equals(y)} returns {@code true} and
* {@code y.equals(z)} returns {@code true}, then
* {@code x.equals(z)} should return {@code true}.
* <li>It is <i>consistent</i>: for any non-null reference values
* {@code x} and {@code y}, multiple invocations of
* {@code x.equals(y)} consistently return {@code true}
* or consistently return {@code false}, provided no
* information used in {@code equals} comparisons on the
* objects is modified.
* <li>For any non-null reference value {@code x},
* {@code x.equals(null)} should return {@code false}.
* </ul>
* <p>
* The {@code equals} method for class {@code Object} implements
* the most discriminating possible equivalence relation on objects;
* that is, for any non-null reference values {@code x} and
* {@code y}, this method returns {@code true} if and only
* if {@code x} and {@code y} refer to the same object
* ({@code x == y} has the value {@code true}).
* <p>
* Note that it is generally necessary to override the {@code hashCode}
* method whenever this method is overridden, so as to maintain the
* general contract for the {@code hashCode} method, which states
* that equal objects must have equal hash codes.
*
* @param obj the reference object with which to compare.
* @return {@code true} if this object is the same as the obj
* argument; {@code false} otherwise.
* @see #hashCode()
* @see java.util.HashMap
*/
public boolean equals(Object obj) {
return (this == obj);
}
while:
而:
Arrays.equals(array1, array2);
/**
* Returns <tt>true</tt> if the two specified arrays of Objects are
* <i>equal</i> to one another. The two arrays are considered equal if
* both arrays contain the same number of elements, and all corresponding
* pairs of elements in the two arrays are equal. Two objects <tt>e1</tt>
* and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
* : e1.equals(e2))</tt>. In other words, the two arrays are equal if
* they contain the same elements in the same order. Also, two array
* references are considered equal if both are <tt>null</tt>.<p>
*
* @param a one array to be tested for equality
* @param a2 the other array to be tested for equality
* @return <tt>true</tt> if the two arrays are equal
*/
public static boolean equals(Object[] a, Object[] a2) {
if (a==a2)
return true;
if (a==null || a2==null)
return false;
int length = a.length;
if (a2.length != length)
return false;
for (int i=0; i<length; i++) {
Object o1 = a[i];
Object o2 = a2[i];
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
}
return true;
}
#4
7
Sigh. Back in the 70s I was the "system programmer" (sysadmin) for an IBM 370 system, and my employer was a member of the IBM users group SHARE. It would sometimes happen thatsomebody submitted an APAR (bug report) on some unexpected behavior of some CMS command, and IBM would respond NOTABUG: the command does what it was designed to do (and what the documentation says).
叹息。回到70年代,我是IBM 370系统的“系统程序员”(sysadmin),我的雇主是IBM用户组的成员。有时会发生这样的情况,有人提交了一个APAR (bug报告),关于一些CMS命令的一些意料之外的行为,而IBM会响应NOTABUG:命令执行它所设计的任务(以及文档说明的内容)。
SHARE came up with a counter to this: BAD -- Broken As Designed. I think this might apply to this implementation of equals for arrays.
与此相反的是:坏的——被设计出来的。我想这可能适用于数组的这个实现。
There's nothing wrong with the implementation of Object.equals. Object has no data members, so there is nothing to compare. Two "Object"s are equal if and only if they are, in fact, the same Object (internally, the same address and length).
Object.equals的实现没有什么问题。对象没有数据成员,所以没有什么可以比较的。两个“对象”是相等的,如果且仅当它们是相同的对象(内部,相同的地址和长度)。
But that logic doesn't apply to arrays. Arrays have data, and you expect comparison (via equals) to compare the data. Ideally, the way Arrays.deepEquals does, but at least the way Arrays.equals does (shallow comparison of the elements).
但这种逻辑并不适用于数组。数组有数据,您希望比较(通过equals)来比较数据。最理想的方式是Arrays. deepequals,但至少是数组。等于(元素的浅比较)。
So the problem is that array (as a built-in object) does not override Object.equals. String (as a named class) does override Object.equals and give the result you expect.
所以问题是数组(作为一个内建的对象)不重写object .equals。String(作为一个命名的类)会重写对象。等于并给出你期望的结果。
Other answers given are correct: [...].equals([....]) simply compares the pointers and not the contents. Maybe someday somebody will correct this. Or maybe not: how many existing programs would break if [...].equals actually compared the elements? Not many, I suspect, but more than zero.
其它答案是正确的:[…].equals([....])简单比较了指针,而不是内容。也许有一天会有人纠正这个错误。或者可能不是:如果[…],现有的程序将会中断多少?等于实际上比较了元素?我怀疑不是很多,但超过了零。
#5
5
Arrays inherit equals()
from Object
and hence compare only returns true if comparing an array against itself.
数组从对象继承equals(),因此只比较数组与自身的比较时才会返回true。
On the other hand, Arrays.equals
compares the elements of the arrays.
另一方面,数组。=比较数组的元素。
This snippet elucidates the difference:
这段代码说明了区别:
Object o1 = new Object();
Object o2 = new Object();
Object[] a1 = { o1, o2 };
Object[] a2 = { o1, o2 };
System.out.println(a1.equals(a2)); // prints false
System.out.println(Arrays.equals(a1, a2)); // prints true
See also Arrays.equals()
. Another static method there may also be of interest: Arrays.deepEquals()
.
参见Arrays.equals()。另一个静态方法也可能有兴趣:Arrays.deepEquals()。
#6
1
The Arrays.equals(array1, array2)
:
的数组。equals(array1 array2):
check if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal.
检查这两个数组是否包含相同数量的元素,并且两个数组中所有对应的元素对都是相等的。
The array1.equals(array2)
:
array1.equals(array2):
compare the object to another object and return true only if the reference of the two object are equal as in the Object.equals()
将对象与另一个对象进行比较,仅当两个对象的引用在object .equals()中相等时才返回true。
#7
0
The equals()
of arrays is inherited from Object
, so it does not look at the contents of the arrrays, it only considers each array equal to itself.
数组的equals()是从对象继承而来的,所以它不查看arrrays的内容,它只考虑每个数组本身。
The Arrays.equals()
methods do compare the arrays' contents. There's overloads for all primitive types, and the one for objects uses the objects' own equals()
methods.
equals()方法可以比较数组的内容。对于所有的基本类型,都有重载,而对象使用对象自己的equals()方法。
#8
0
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
// initiliazing three object arrays
Object[] arr1 = new Object[] { 1, 123 };
Object[] arr2 = new Object[] { 1, 123, 22, 4 };
Object[] arr3 = new Object[] { 1, 123 };
// comparing arr1 and arr2
boolean retval=Arrays.equals(arr1, arr2);
System.out.println("arr1 and arr2 equal: " + retval);
System.out.println("arr1 and arr2 equal: " + arr1.equals(arr2));
// comparing arr1 and arr3
boolean retval2=Arrays.equals(arr1, arr3);
System.out.println("arr1 and arr3 equal: " + retval2);
System.out.println("arr1 and arr2 equal: " + arr1.equals(arr3));
}
}
Here is the output:
这是输出:
arr1 and arr2 equal: false
arr1 and arr2 equal: false
arr1 and arr3 equal: true
arr1 and arr3 equal: false
Seeing this kind of problem I would personally go for Arrays.equals(array1, array2)
as per your question to avoid confusion.
看到这样的问题,我个人就会选择数组。等于(array1, array2),以避免混淆。
#1
254
array1.equals(array2)
is the same as array1 == array2
, i.e. is it the same array. As @alf points out it's not what most people expect.
array1.equals(array2)与array1 == array2相同,即它是相同的数组。正如@alf所指出的,这并不是大多数人所期望的。
Arrays.equals(array1, array2)
compares the contents of the arrays.
数组。equals(array1, array2)比较数组的内容。
Similarly array.toString()
may not be very useful and you need to use Arrays.toString(array)
.
同样,array.toString()可能不是很有用,您需要使用Arrays.toString(数组)。
#2
72
It's an infamous problem: .equals()
for arrays is badly broken, just don't use it, ever.
这是一个臭名昭著的问题:对于数组来说,.equals()是严重损坏的,只是不要使用它。
That said, it's not "broken" as in "someone has done it in a really wrong way" — it's just doing what's defined and not what's usually expected. So for purists: it's perfectly fine, and that also means, don't use it, ever.
也就是说,它并不是像“某人以一种非常错误的方式完成的”那样“支离破碎”——它只是做了定义,而不是通常预期的。对于纯粹主义者来说,这是完美的,这也意味着,永远不要使用它。
Now the expected behaviour for equals
is to compare data. The default behaviour is to compare the identity, as Object
does not have any data (for purists: yes it has, but it's not the point); assumption is, if you need equals
in subclasses, you'll implement it. In arrays, there's no implementation for you, so you're not supposed to use it.
现在,对等的期望行为是比较数据。默认行为是比较身份,因为对象没有任何数据(纯粹主义者:是的,它有,但不是重点);假设是,如果您需要在子类中相等,您将实现它。在数组中,没有实现,所以不应该使用它。
So the difference is, Arrays.equals(array1, array2)
works as you would expect (i.e. compares content), array1.equals(array2)
falls back to Object.equals
implementation, which in turn compares identity, and thus better replaced by ==
(for purists: yes I know about null
).
不同之处在于,数组。= (array1, array2)按照您的期望工作(即比较内容),array1.equals(array2)返回对象。=实现,它依次比较标识,因此更好地替换为==(纯粹主义者:是的,我知道null)。
Problem is, even Arrays.equals(array1, array2)
will bite you hard if elements of array do not implement equals
properly. It's a very naive statement, I know, but there's a very important less-than-obvious case: consider a 2D array.
问题是,即使数组。= (array1, array2)如果数组元素没有正确地实现,将会咬你。这是一个很幼稚的说法,我知道,但是有一个非常重要的不太明显的例子:考虑一个2D数组。
2D array in Java is an array of arrays, and arrays' equals
is broken (or useless if you prefer), so Arrays.equals(array1, array2)
will not work as you expect on 2D arrays.
在Java中,2D数组是数组的数组,数组的equals是被破坏的(如果你喜欢的话也没用),所以数组。= (array1, array2)不会像你期望的2D数组那样工作。
Hope that helps.
希望有帮助。
#3
15
Look inside the implementation of the two methods to understand them deeply:
深入了解这两种方法的实施情况,深入了解它们:
array1.equals(array2);
/**
* Indicates whether some other object is "equal to" this one.
* <p>
* The {@code equals} method implements an equivalence relation
* on non-null object references:
* <ul>
* <li>It is <i>reflexive</i>: for any non-null reference value
* {@code x}, {@code x.equals(x)} should return
* {@code true}.
* <li>It is <i>symmetric</i>: for any non-null reference values
* {@code x} and {@code y}, {@code x.equals(y)}
* should return {@code true} if and only if
* {@code y.equals(x)} returns {@code true}.
* <li>It is <i>transitive</i>: for any non-null reference values
* {@code x}, {@code y}, and {@code z}, if
* {@code x.equals(y)} returns {@code true} and
* {@code y.equals(z)} returns {@code true}, then
* {@code x.equals(z)} should return {@code true}.
* <li>It is <i>consistent</i>: for any non-null reference values
* {@code x} and {@code y}, multiple invocations of
* {@code x.equals(y)} consistently return {@code true}
* or consistently return {@code false}, provided no
* information used in {@code equals} comparisons on the
* objects is modified.
* <li>For any non-null reference value {@code x},
* {@code x.equals(null)} should return {@code false}.
* </ul>
* <p>
* The {@code equals} method for class {@code Object} implements
* the most discriminating possible equivalence relation on objects;
* that is, for any non-null reference values {@code x} and
* {@code y}, this method returns {@code true} if and only
* if {@code x} and {@code y} refer to the same object
* ({@code x == y} has the value {@code true}).
* <p>
* Note that it is generally necessary to override the {@code hashCode}
* method whenever this method is overridden, so as to maintain the
* general contract for the {@code hashCode} method, which states
* that equal objects must have equal hash codes.
*
* @param obj the reference object with which to compare.
* @return {@code true} if this object is the same as the obj
* argument; {@code false} otherwise.
* @see #hashCode()
* @see java.util.HashMap
*/
public boolean equals(Object obj) {
return (this == obj);
}
while:
而:
Arrays.equals(array1, array2);
/**
* Returns <tt>true</tt> if the two specified arrays of Objects are
* <i>equal</i> to one another. The two arrays are considered equal if
* both arrays contain the same number of elements, and all corresponding
* pairs of elements in the two arrays are equal. Two objects <tt>e1</tt>
* and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
* : e1.equals(e2))</tt>. In other words, the two arrays are equal if
* they contain the same elements in the same order. Also, two array
* references are considered equal if both are <tt>null</tt>.<p>
*
* @param a one array to be tested for equality
* @param a2 the other array to be tested for equality
* @return <tt>true</tt> if the two arrays are equal
*/
public static boolean equals(Object[] a, Object[] a2) {
if (a==a2)
return true;
if (a==null || a2==null)
return false;
int length = a.length;
if (a2.length != length)
return false;
for (int i=0; i<length; i++) {
Object o1 = a[i];
Object o2 = a2[i];
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
}
return true;
}
#4
7
Sigh. Back in the 70s I was the "system programmer" (sysadmin) for an IBM 370 system, and my employer was a member of the IBM users group SHARE. It would sometimes happen thatsomebody submitted an APAR (bug report) on some unexpected behavior of some CMS command, and IBM would respond NOTABUG: the command does what it was designed to do (and what the documentation says).
叹息。回到70年代,我是IBM 370系统的“系统程序员”(sysadmin),我的雇主是IBM用户组的成员。有时会发生这样的情况,有人提交了一个APAR (bug报告),关于一些CMS命令的一些意料之外的行为,而IBM会响应NOTABUG:命令执行它所设计的任务(以及文档说明的内容)。
SHARE came up with a counter to this: BAD -- Broken As Designed. I think this might apply to this implementation of equals for arrays.
与此相反的是:坏的——被设计出来的。我想这可能适用于数组的这个实现。
There's nothing wrong with the implementation of Object.equals. Object has no data members, so there is nothing to compare. Two "Object"s are equal if and only if they are, in fact, the same Object (internally, the same address and length).
Object.equals的实现没有什么问题。对象没有数据成员,所以没有什么可以比较的。两个“对象”是相等的,如果且仅当它们是相同的对象(内部,相同的地址和长度)。
But that logic doesn't apply to arrays. Arrays have data, and you expect comparison (via equals) to compare the data. Ideally, the way Arrays.deepEquals does, but at least the way Arrays.equals does (shallow comparison of the elements).
但这种逻辑并不适用于数组。数组有数据,您希望比较(通过equals)来比较数据。最理想的方式是Arrays. deepequals,但至少是数组。等于(元素的浅比较)。
So the problem is that array (as a built-in object) does not override Object.equals. String (as a named class) does override Object.equals and give the result you expect.
所以问题是数组(作为一个内建的对象)不重写object .equals。String(作为一个命名的类)会重写对象。等于并给出你期望的结果。
Other answers given are correct: [...].equals([....]) simply compares the pointers and not the contents. Maybe someday somebody will correct this. Or maybe not: how many existing programs would break if [...].equals actually compared the elements? Not many, I suspect, but more than zero.
其它答案是正确的:[…].equals([....])简单比较了指针,而不是内容。也许有一天会有人纠正这个错误。或者可能不是:如果[…],现有的程序将会中断多少?等于实际上比较了元素?我怀疑不是很多,但超过了零。
#5
5
Arrays inherit equals()
from Object
and hence compare only returns true if comparing an array against itself.
数组从对象继承equals(),因此只比较数组与自身的比较时才会返回true。
On the other hand, Arrays.equals
compares the elements of the arrays.
另一方面,数组。=比较数组的元素。
This snippet elucidates the difference:
这段代码说明了区别:
Object o1 = new Object();
Object o2 = new Object();
Object[] a1 = { o1, o2 };
Object[] a2 = { o1, o2 };
System.out.println(a1.equals(a2)); // prints false
System.out.println(Arrays.equals(a1, a2)); // prints true
See also Arrays.equals()
. Another static method there may also be of interest: Arrays.deepEquals()
.
参见Arrays.equals()。另一个静态方法也可能有兴趣:Arrays.deepEquals()。
#6
1
The Arrays.equals(array1, array2)
:
的数组。equals(array1 array2):
check if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal.
检查这两个数组是否包含相同数量的元素,并且两个数组中所有对应的元素对都是相等的。
The array1.equals(array2)
:
array1.equals(array2):
compare the object to another object and return true only if the reference of the two object are equal as in the Object.equals()
将对象与另一个对象进行比较,仅当两个对象的引用在object .equals()中相等时才返回true。
#7
0
The equals()
of arrays is inherited from Object
, so it does not look at the contents of the arrrays, it only considers each array equal to itself.
数组的equals()是从对象继承而来的,所以它不查看arrrays的内容,它只考虑每个数组本身。
The Arrays.equals()
methods do compare the arrays' contents. There's overloads for all primitive types, and the one for objects uses the objects' own equals()
methods.
equals()方法可以比较数组的内容。对于所有的基本类型,都有重载,而对象使用对象自己的equals()方法。
#8
0
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
// initiliazing three object arrays
Object[] arr1 = new Object[] { 1, 123 };
Object[] arr2 = new Object[] { 1, 123, 22, 4 };
Object[] arr3 = new Object[] { 1, 123 };
// comparing arr1 and arr2
boolean retval=Arrays.equals(arr1, arr2);
System.out.println("arr1 and arr2 equal: " + retval);
System.out.println("arr1 and arr2 equal: " + arr1.equals(arr2));
// comparing arr1 and arr3
boolean retval2=Arrays.equals(arr1, arr3);
System.out.println("arr1 and arr3 equal: " + retval2);
System.out.println("arr1 and arr2 equal: " + arr1.equals(arr3));
}
}
Here is the output:
这是输出:
arr1 and arr2 equal: false
arr1 and arr2 equal: false
arr1 and arr3 equal: true
arr1 and arr3 equal: false
Seeing this kind of problem I would personally go for Arrays.equals(array1, array2)
as per your question to avoid confusion.
看到这样的问题,我个人就会选择数组。等于(array1, array2),以避免混淆。