亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Java中等于與Arrays相等

在Java中等于與Arrays相等

海綿寶寶撒 2019-06-13 17:40:20
在Java中等于與Arrays相等在Java中比較數組時,以下2條語句之間有什么區別嗎?array1.equals(array2);Arrays.equals(array1, array2);如果是的話,他們是什么?
查看完整描述

3 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

array1.equals(array2)是相同的array1 == array2,即它是否是同一個數組。正如@Alf所指出的,這并不是大多數人所期望的。

Arrays.equals(array1, array2)比較數組的內容。


類似array.toString()可能不是很有用,您需要使用Arrays.toString(array).


查看完整回答
反對 回復 2019-06-13
?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

這是一個臭名昭著的問題:.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)不會像你期望的那樣在二維數組上工作。

希望能幫上忙。


查看完整回答
反對 回復 2019-06-13
?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

深入了解這兩種方法的實現情況:


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;

}


查看完整回答
反對 回復 2019-06-13
  • 3 回答
  • 0 關注
  • 618 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號