简体   繁体   English

如何使用equals方法比较不同类中的对象数组

[英]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.

相关问题 如何使用equals方法将对象添加到数组列表中以排除相似对象? - How can I add objects to an array list using the equals method to exclude similar objects? 如何在其他类中使用方法? - How can I use a method in a different class? 如何创建布尔值 equals 方法比较数组中的对象 - How do I create an boolean equals method compares objects in an array 对象的equals()方法与String对象和我创建的对象的行为有所不同? - equals() method of objects is acting different with String objects and the Objects I created? 如何使用来自不同类中的类的方法? - How can I use a Method from a class in a different class? 如何在实体的equals方法中使用@DiscriminatorColumn? - How can i use @DiscriminatorColumn in the equals method of the entity? 如何在 get 方法和 if 语句中使用不等于 - How can I use not equals in the get method and if statement 我应该比较班级“等于”方法中的所有字段吗? - Should I compare all fields in my class's “equals” method? 当数组由类中的字段组成时,应该在每个元素上使用Objects.equals()而不是Arrays.equals()吗? - Should I use Objects.equals() on each element rather than Arrays.equals() when the array is composed of fields in my class? 如何使用不同类中的数组? - How can I use an array which is in a different class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM