![](/img/trans.png)
[英]How can I add objects to an array list using the equals method to exclude similar objects?
[英]How can I use the equals method to compare an array of objects in a different class
public boolean equals(Object obj) {
boolean result = false;
if (obj instanceof Recipe) {
Recipe that = (Recipe) obj;
if(this.getName().equals(that.getName())){
for (int cnt = 0; cnt < I.length; cnt++) {
if (I[cnt].getName().equals(that.getName())) {
result = (this.getName() == that.getName());
}
}
}
}
return result;
}
我可以比較配方的名稱,一旦它們相等就需要逐一比較成分,但是我不知道如何進行比較。 我知道我需要一個for循環,將其添加到其中以比較所有成分,但是之后要做什么。 我真的很感謝任何類型的幫助。
看起來像您在嘗試在食譜中覆蓋equals(),
然后您必須比較配方的每個字段
但是你應該發布食譜的來源
“列表等於”檢查是否以相同順序引用了相等的對象,這可能是您需要的。 如果是這樣,請考慮Arrays.asList,它為您提供了引用數組的List視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.