在Java中等于与Arrays相等

在Java中等于与Arrays相等

在Java中比较数组时,以下2条语句之间有什么区别吗?

array1.equals(array2);Arrays.equals(array1, array2);

如果是的话,他们是什么?


海绵宝宝撒
浏览 591回答 3
3回答

不负相思意

array1.equals(array2)是相同的array1 == array2,即它是否是同一个数组。正如@Alf所指出的,这并不是大多数人所期望的。Arrays.equals(array1, array2)比较数组的内容。类似array.toString()可能不是很有用,您需要使用Arrays.toString(array).

心有法竹

这是一个臭名昭著的问题:.equals()因为数组坏了,请永远不要使用它。也就是说,它并没有像“某人以一种非常错误的方式去做”那样“被打破”-它只是在做定义上的事情,而不是通常预期的事情。所以对于纯粹主义者来说,这是非常好的,这也意味着,永远不要用它。现在预期的行为equals就是比较数据。默认行为是比较身份,如Object没有任何数据(对于纯粹主义者:是的,但这不是重点);假设是,如果你需要的话equals在子类中,您将实现它。在数组中,您没有实现,因此不应该使用它。所以区别在于,Arrays.equals(array1, array2)作品如你所料(即比较内容),array1.equals(array2)回到Object.equals实现,这反过来比较身份,因此更好地由==(对纯粹主义者来说:是的我知道null).问题是,甚至Arrays.equals(array1, array2)如果数组的元素没有实现,就会咬死你。equals恰到好处。我知道,这是一个非常幼稚的说法,但有一个非常重要的不太明显的例子:考虑一个2D数组。Java中的2D数组是数组的数组,而数组是equals是坏的(或者如果你愿意的话是没用的),所以Arrays.equals(array1, array2)不会像你期望的那样在二维数组上工作。希望能帮上忙。

Qyouu

深入了解这两种方法的实现情况:array1.equals(array2);/**&nbsp;* Indicates whether some other object is "equal to" this one.&nbsp;* <p>&nbsp;* The {@code equals} method implements an equivalence relation&nbsp;* on non-null object references:&nbsp;* <ul>&nbsp;* <li>It is <i>reflexive</i>: for any non-null reference value&nbsp;*&nbsp; &nbsp; &nbsp;{@code x}, {@code x.equals(x)} should return&nbsp;*&nbsp; &nbsp; &nbsp;{@code true}.&nbsp;* <li>It is <i>symmetric</i>: for any non-null reference values&nbsp;*&nbsp; &nbsp; &nbsp;{@code x} and {@code y}, {@code x.equals(y)}&nbsp;*&nbsp; &nbsp; &nbsp;should return {@code true} if and only if&nbsp;*&nbsp; &nbsp; &nbsp;{@code y.equals(x)} returns {@code true}.&nbsp;* <li>It is <i>transitive</i>: for any non-null reference values&nbsp;*&nbsp; &nbsp; &nbsp;{@code x}, {@code y}, and {@code z}, if&nbsp;*&nbsp; &nbsp; &nbsp;{@code x.equals(y)} returns {@code true} and&nbsp;*&nbsp; &nbsp; &nbsp;{@code y.equals(z)} returns {@code true}, then&nbsp;*&nbsp; &nbsp; &nbsp;{@code x.equals(z)} should return {@code true}.&nbsp;* <li>It is <i>consistent</i>: for any non-null reference values&nbsp;*&nbsp; &nbsp; &nbsp;{@code x} and {@code y}, multiple invocations of&nbsp;*&nbsp; &nbsp; &nbsp;{@code x.equals(y)} consistently return {@code true}&nbsp;*&nbsp; &nbsp; &nbsp;or consistently return {@code false}, provided no&nbsp;*&nbsp; &nbsp; &nbsp;information used in {@code equals} comparisons on the&nbsp;*&nbsp; &nbsp; &nbsp;objects is modified.&nbsp;* <li>For any non-null reference value {@code x},&nbsp;*&nbsp; &nbsp; &nbsp;{@code x.equals(null)} should return {@code false}.&nbsp;* </ul>&nbsp;* <p>&nbsp;* The {@code equals} method for class {@code Object} implements&nbsp;* the most discriminating possible equivalence relation on objects;&nbsp;* that is, for any non-null reference values {@code x} and&nbsp;* {@code y}, this method returns {@code true} if and only&nbsp;* if {@code x} and {@code y} refer to the same object&nbsp;* ({@code x == y} has the value {@code true}).&nbsp;* <p>&nbsp;* Note that it is generally necessary to override the {@code hashCode}&nbsp;* method whenever this method is overridden, so as to maintain the&nbsp;* general contract for the {@code hashCode} method, which states&nbsp;* that equal objects must have equal hash codes.&nbsp;*&nbsp;* @param&nbsp; &nbsp;obj&nbsp; &nbsp;the reference object with which to compare.&nbsp;* @return&nbsp; {@code true} if this object is the same as the obj&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; argument; {@code false} otherwise.&nbsp;* @see&nbsp; &nbsp; &nbsp;#hashCode()&nbsp;* @see&nbsp; &nbsp; &nbsp;java.util.HashMap&nbsp;*/public boolean equals(Object obj) {&nbsp; &nbsp; return (this == obj);}同时:Arrays.equals(array1, array2);/**&nbsp;* Returns <tt>true</tt> if the two specified arrays of Objects are&nbsp;* <i>equal</i> to one another.&nbsp; The two arrays are considered equal if&nbsp;* both arrays contain the same number of elements, and all corresponding&nbsp;* pairs of elements in the two arrays are equal.&nbsp; Two objects <tt>e1</tt>&nbsp;* and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null&nbsp;* : e1.equals(e2))</tt>.&nbsp; In other words, the two arrays are equal if&nbsp;* they contain the same elements in the same order.&nbsp; Also, two array&nbsp;* references are considered equal if both are <tt>null</tt>.<p>&nbsp;*&nbsp;* @param a one array to be tested for equality&nbsp;* @param a2 the other array to be tested for equality&nbsp;* @return <tt>true</tt> if the two arrays are equal&nbsp;*/public static boolean equals(Object[] a, Object[] a2) {&nbsp; &nbsp; if (a==a2)&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; if (a==null || a2==null)&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; int length = a.length;&nbsp; &nbsp; if (a2.length != length)&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; for (int i=0; i<length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; Object o1 = a[i];&nbsp; &nbsp; &nbsp; &nbsp; Object o2 = a2[i];&nbsp; &nbsp; &nbsp; &nbsp; if (!(o1==null ? o2==null : o1.equals(o2)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; return true;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java