[英]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;
}
I can compare the name of the recipe and once they are equals I need to compare one by one the ingredients but I do not know how to do the comparison. 我可以比较配方的名称,一旦它们相等就需要逐一比较成分,但是我不知道如何进行比较。 I know I need a for loop which I added it to compare all the ingredients, but what to do after that. 我知道我需要一个for循环,将其添加到其中以比较所有成分,但是之后要做什么。 I would really appreciate any type of help. 我真的很感谢任何类型的帮助。
override equals() in Recipe, as it looks you are trying, 看起来像您在尝试在食谱中覆盖equals(),
then you have to compare each field of Recipe 然后您必须比较配方的每个字段
But you shoul post the source of the fields of Recipe 但是你应该发布食谱的来源
The List equals checks for references to equal objects in the same order, which may be what you need. “列表等于”检查是否以相同顺序引用了相等的对象,这可能是您需要的。 If so, consider Arrays.asList which gives you a List view of an array of references. 如果是这样,请考虑Arrays.asList,它为您提供了引用数组的List视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.