Object[] array1, array2;
array1.equals(array2);
Arrays.equals(array1, array2);
如果有的话,它们是什么?
Object[] array1, array2;
array1.equals(array2);
Arrays.equals(array1, array2);
如果有的话,它们是什么?
array1.equals(array2)
与array1 == array2
是相同的,即它们指向同一数组。正如@alf所指出的那样,这并不是大多数人预期的。
Arrays.equals(array1, array2)
比较数组的内容。
同样地,array.toString()
可能没有太多用处,您需要使用Arrays.toString(array)
。
Arrays.equals()
对于多维数组的效果与预期不符,它仅比较第一维的项是否具有引用相等性。Apache Commons 的 ArrayUtils.isEquals
可用于多维数组。 - Adam Parkin.equals
方法来说,它不会比较内容,这就是你需要该方法的原因。 - Peter Lawrey一个臭名昭著的问题是:针对数组的.equals()
方法存在严重问题,永远不要使用它。
也就是说,它并不是“错误”的意思,而是它只做了定义中的事情,而不是通常期望的事情。所以对于纯粹主义者:这是完全可以的,这也意味着,永远不要使用它。
现在,equals
方法的预期行为是比较数据。默认行为是比较身份,因为Object
类没有任何数据(对于纯粹主义者:它确实有数据,但不是重点)。假设是,如果您需要在子类中使用equals
方法,则必须自己实现该方法。在数组中,没有为您实现它,因此您不应该使用它。
因此,区别在于,Arrays.equals(array1, array2)
按照您预期的方式工作(即比较内容),array1.equals(array2)
则回退到Object.equals
实现,后者比较身份,因此最好用==
代替(对于纯粹主义者:是的,我知道有关null
的情况)。
问题是,即使Arrays.equals(array1, array2)
在数组元素正确实现equals
方法的情况下,也可能会让您遭受重创。我知道这是一个非常幼稚的陈述,但有一个非常重要而不明显的案例:考虑一个二维数组。
Java中的二维数组是一个数组的数组,并且数组的equals
方法是不起作用的(或者如果您愿意说的话,是无用的),因此Arrays.equals(array1, array2)
在二维数组上将不能按照预期工作。
equals()
实现?我认为它没有从Object中重写。 - Martijn CourteauxArrays.deepEquals
--- 这就是someArray.equals
一直应该做的。 (相关: Objects.deepEquals
.) - Kevin J. Chase深入了解这两个方法的实现:
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);
}
当:
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;
}
叹气。在70年代,我是IBM 370系统的“系统程序员”(系统管理员),我的雇主是IBM用户组SHARE的成员。有时会出现这样的情况:有人针对某些CMS命令的某些意外行为提交一个APAR(错误报告),然后IBM会回复NOTABUG:该命令执行了它设计应执行的操作(以及文档所说的操作)。
SHARE提出了一个对策:BAD -- 损坏但设计如此。我认为这可能适用于数组的equals实现。
Object.equals的实现没有任何问题。Object没有数据成员,因此没有什么可以比较的。如果两个“Object”实际上是相同的对象(在内部,地址和长度相同),则它们是相等的。
但是,这种逻辑并不适用于数组。数组有数据,并且希望通过equals进行比较。理想情况下,应该像Arrays.deepEquals一样比较,但至少也应该像Arrays.equals一样(浅层比较元素)。
因此,问题在于数组(作为内置对象)没有覆盖Object.equals。String(作为命名类)覆盖了Object.equals并提供了您期望的结果。
其他给出的答案是正确的:[...].equals([....])只是比较指针而不是内容。也许有一天有人会纠正这个问题。或者也许不会:如果[...].equals实际上比较元素,会有多少现有程序会出问题?我认为不会很多,但不会是零。
数组继承Object
中的equals()
,因此只有在将数组与自身进行比较时才返回true。
另一方面,Arrays.equals
会比较数组元素。
这段片段阐明了它们之间的差异:
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
此外还有Arrays.equals()
,也是一个静态方法。另一个可能感兴趣的静态方法是:Arrays.deepEquals()
。
数组的equals()
方法是继承自Object
,因此不会查看数组的内容,它只认为每个数组与自身相等。
Arrays.equals()
方法比较数组的内容。有所有原始类型的重载,对象类型的一个使用对象自己的equals()
方法。
Arrays.equals(array1, array2)
:
检查两个数组是否包含相同数量的元素,并且这两个数组中对应的元素对是相等的。
array1.equals(array2)
:
将该对象与另一个对象进行比较,仅在两个对象的引用相等时返回true,就像Object.equals()
一样。
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
// initializing three object arrays
Object[] array1 = new Object[] { 1, 123 };
Object[] array2 = new Object[] { 1, 123, 22, 4 };
Object[] array3 = new Object[] { 1, 123 };
// comparing array1 and array2
boolean retval=Arrays.equals(array1, array2);
System.out.println("array1 and array2 equal: " + retval);
System.out.println("array1 and array2 equal: " + array1.equals(array2));
// comparing array1 and array3
boolean retval2=Arrays.equals(array1, array3);
System.out.println("array1 and array3 equal: " + retval2);
System.out.println("array1 and array3 equal: " + array1.equals(array3));
}
}
这里是输出结果:
array1 and array2 equal: false
array1 and array2 equal: false
array1 and array3 equal: true
array1 and array3 equal: false
Arrays.equals(array1, array2)
来避免混淆,以回答你的问题。我认为Objects.deepEquals(Obj1, Obj2)是最好的统一解决方案,如果Obj1和Obj2是两个int数组,则会为您调用Arrays.deepEquals0(a, b)方法。如果您不比较字符串,它将只使用“.equals”(“==”)传统方法,因此在比较字符串时也非常有用。
它将涵盖像黄油一样的那些正常用法,无需记住何时使用().equals()、Arrays.equals、(String a).equals((String b))或其他任何方法。
这个Objects.deepEquals操作的时间复杂度将是O(n)。
public static boolean deepEquals(Object a, Object b) {
if (a == b)
return true;
else if (a == null || b == null)
return false;
else
return Arrays.deepEquals0(a, b);
}
常见用法如下: 比较整数数组,哇哦:
int[] num1 = { 1, 2, 3, 4 };
int[] num2 = { 1, 2, 3, 4 };
System.out.println(Objects.deepEquals(num1, num2));
还可以比较二维数组,太棒了:
int[][] nums1 = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
int[][] nums2 = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
System.out.println(Objects.deepEquals(nums1, nums2));
比较字符串,甚至更大:
String s1 = "sasfd!";
String s2 = "sasfd" + "!";
System.out.println(Objects.deepEquals(s1, s2));
我喜欢通用的方法