简体   繁体   中英

How to compare object parameter to another object parameter (inside array)

What do I need to write inside the " if " statement in order to check if the object's variable (number) is equal to any of the object's variables (number) inside my array?

public class Question{

    private int number;
    private String questionString;
    private int index = 0;

    public Question(int number, String questionString){


if(number < 0){
        System.out.print("Number cannot be negative!");
    }
    if(questionString.equals(null) || questionString.trim().isEmpty()){
        System.out.print("Question cannot be empty!");
    }

        this.number = number;
        this.questionString = questionString;
    }

    public boolean equals(Object o){
            if(o instanceof Question){  
               for(int i = 0; i < index; i++){
                   if(.... ){

                   }
               }
                return true;
            }
            return false;   
        }


}

Test Class (main Class), ofc not yet completed I just hope it gives you enough information, on how to proceed on my Question class:

public class Test{
        String name;
        Question[] questions;

        public Testi(String name, int nrQuestions){

        }

        public static void main(String[]args){

        }

}

This should do:

    public boolean equals(Object o){
    if (o == null )
    return false;

    if(o instanceof Question){
        if (((Question)o).number == this.number ){
            if (this.questionString.equals(((Question)o).questionString)){
                return true;
            }
        }

    }
    return false;
}

Be careful though, if you're implementing equals() you should also implement hashCode() . It's your "model" that should make clear when two objects are equal. In your case, I would say that when two Question's number are the same, then the Question is also the same. If not, you should also add questionString to the check

From your last comment, I think I now understand your REAL issue: You tried to use the equals() method to compare an object to an array of objects that you pass to this method, right? The call to equals would look like this:

Question[] questions;
question.equals(questions);

Technically this is possible with Java, but strongly discouraged I would say. equals() should only be used to compare to one object at a time (and stick to the hashCode/equals contract).

So I suggest you to do something like this:

     //your application code
    public boolean equals(Object obj){
        if (obj instanceof Question){  
            return (((Question)obj).number == this.number);
        }
        return false;   
    }

    //use this to obey the equals/hashCode contract
    public int hashCode() {
        return this.number;
    }

    // your test code -------------

    public void testQuestions(Question testAgainstThisObject, Question[] testQuestions) {
        for (Question testThisQuestion : testQuestions) {
            assertTrue(testAgainstThisObject.equals(testThisQuestion));
        }
    }

Another way would be to use a specialised method in Question.java , something like this:

    public boolean isThisContained(Question[] questions) {
        for (Question question : questions) {
            if (this.equals(question)) {
                return true;
            }
        }

        return false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM