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

前端 未结 4 890
挽巷
挽巷 2020-12-02 00:00

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 a

相关标签:
4条回答
  • 2020-12-02 00:28

    contains() method checks the equal() method on Object while checking .

    You have to ovveride equals method in order to make it work.

    public boolean contains(Object o)

    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)).

    Edit:
    

    If you not ovveriding equals method, Then default Object equals method executes and, as per docs of Equals method

    The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

    So your userInputHole == leftFlowInnerHole is always false as they are pointing to different instances.

    Hence to avoid the default implementation ,you just ovveride that equals in yout class and provide your implementation.

    An efficient equals(Object o) implementation

    0 讨论(0)
  • 2020-12-02 00:34

    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;
    }
    
    0 讨论(0)
  • 2020-12-02 00:42

    This is not working

       if(priceidslist.contains(extraId)){
                                   //Not working 
                                }
    

    I just added this lines for checking that condition in (for -loop ),then its working fine

      String gh = String.valueOf(priceidslist.get(j));
                               if(gh.equals(extraId)){
                                   rw.put("extraPrice",pricelist.get(j));
                               }
    
    0 讨论(0)
  • 2020-12-02 00:53

    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.

    0 讨论(0)
提交回复
热议问题