What do you compare the contents() method of ArrayList?

Asked 2 years ago, Updated 2 years ago, 168 views

ArrayList<Thing> basket = new ArrayList<Thing>();  
Thing thing = new Thing(100);  
basket.add(thing);  
Thing another = new Thing(100);  
basket.contains(another); // true or false?

class Thing {  
    public int value;  

    public Thing (int x) {
        value = x;
    }

    equals (Thing x) {
        if (x.value == value) return true;
        return false;
    }
}

I created an object from the code above and put it in ArrayList. And we added the same value to create a different object. Does the contents() method evaluate whether two objects are the same? The constructor only receives the input value and both objects have the same variable Is there a way to implement the contents() method to return true?

java object arraylist evaluation

2022-09-22 22:22

1 Answers

ArrayList is a class that implements the List interface. If you look at the contents method by looking at the list in the Java document, you can see that the contents() is made into the equals() method. The equals() method is a method of determining whether two objects are the same.


2022-09-22 22:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.