gpt4 book ai didi

java - 在 Java 中调用数组实例的 hashCode()

转载 作者:行者123 更新时间:2023-11-30 07:07:56 25 4
gpt4 key购买 nike

<分区>

我刚刚在写代码,突然在Netbeans中看到这个警告:

hashCode() called on array instance

出现在这段代码中:

public class SomeObject {

private String a;
private char[] b;

@Override
public boolean equals(Object anotherObject) {
if (!(anotherObject instanceof SomeObject)) {
return false;
}
SomeObject object = (SomeObject) anotherObject;
return (this.a.equals(object.a) && arraysAreEqual(this.b, object.b));
}

// When I created the equals() method, Netbeans warned me:
// 'Generate missing hashCode()'. Okay then, here it comes:

@Override
public int hashCode() {
return (43 * this.a.hashCode() + 11 * this.b.hashCode()); // MARKED LINE.
}
}

警告出现在标记的行上。 IDE 发现我应该避免在数组实例上调用 hashCode()

现在为什么我应该避免在数组上使用 hashCode()

注意我读了this question and answer , 但他们没有提到这一点。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com