Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ArrayList Contain always return false although it contain the same value

This is my Hole Class

class Hole {

public  int a;
public  int b;

Hole(int a, int b) {
    this.a = a;
    this.b = b;
}

So i adding an ArrayList that contain several several hole

public void checkPathLoop(int x, int y) {
        //rough code

        ArrayList<Hole> leftFlowInnerHole = new ArrayList<>();


        //left holes rules
        leftFlowInnerHole.add(new Hole(0, 1));
        leftFlowInnerHole.add(new Hole(1, 5));
        leftFlowInnerHole.add(new Hole(5, 4));
        leftFlowInnerHole.add(new Hole(0, 4));

when i add

Hole userInputHole = new Hole(0,1);
System.out.print(leftFlowInnerHole.contain(userInputHole));

it always return false !! it suppose to return true.

Is there anything i miss ??

Thank you in advance

like image 312
Shan Markus Avatar asked Dec 31 '13 15:12

Shan Markus


People also ask

How do you check if an ArrayList contains the same value?

ArrayList. contains() method can be used to check if a Java ArrayList contains a given item or not. This method has a single parameter i.e. the item whose presence in the ArrayList is tested. Also it returns true if the item is present in the ArrayList and false if the item is not present.

Does ArrayList contain equals in Java?

It uses equals, not ==. The source comes with the JDK, remember: ?

How does contains work in Java ArrayList?

ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Returns: It returns true if the specified element is found in the list else it returns false.

Can ArrayList have multiple values?

ArrayList<Object> list = new ArrayList<Object>(); The above list can hold values of any type. The code given below presents an example of the ArrayList with the Objects of multiple types.


2 Answers

You need to override the equals method herited from the Object class (and hence also hashCode to respect the contract, see Why do I need to override the equals and hashCode methods in Java? ) in your Hole class.

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

Basically the default equals implementation is an == comparison between the two objects

public boolean equals(Object obj) {
   return (this == obj);
}

Since you created two different objects, while they have the same value as attributes they're two distincts objects and hence this == obj returns false.

If you did :

Hole a = new Hole(0,1);
leftFlowInnerHole.add(a);
System.out.print(leftFlowInnerHole.contains(a));

You'll see that it outputs true.

like image 178
Alexis C. Avatar answered Oct 19 '22 10:10

Alexis C.


You should overide the equals method of the Hole class:

@Override
public boolean equals(Object obj) {

if (obj == this) {
    return true;
}

if (!(obj instanceof Hole)) {
    return false;
}

Hole other = (Hole) obj;

return a == other.a && b == other.b;
}
like image 32
Jamel ESSOUSSI Avatar answered Oct 19 '22 10:10

Jamel ESSOUSSI