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

前端 未结 4 916
挽巷
挽巷 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: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;
    }
    

提交回复
热议问题