Java ArrayList Contain 尽管包含相同的值,但它始终返回 false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20859706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java ArrayList Contain always return false although it contain the same value
提问by Shan Markus
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
所以我添加了一个包含几个洞的 ArrayList
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.
它总是返回 false !它假设返回true。
Is there anything i miss ??
有什么我想念的吗?
Thank you in advance
先感谢您
采纳答案by Alexis C.
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.
您需要在您equals
的Object
班级中覆盖从该类继承的方法(因此也要hashCode
遵守合同,请参阅为什么我需要覆盖 Java 中的 equals 和 hashCode 方法?)Hole
。
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)
).
如果此列表包含指定的元素,则返回 true。更正式地说,当且仅当此列表包含至少一个元素 e 使得 (
o==null ? e==null : o.equals(e)
)时才返回 true 。
Basically the default equals
implementation is an ==
comparison between the two objects
基本上默认equals
实现是==
两个对象之间的比较
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
.
由于您创建了两个不同的对象,虽然它们与属性具有相同的值,但它们是两个不同的对象,因此this == obj
返回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
.
你会看到它输出true
.
回答by Suresh Atta
contains()
method checks the equal()
method on Object while checking .
contains()
方法在检查equal()
时检查 Object 上的方法。
You have to ovveride equals method in order to make it work.
您必须覆盖 equals 方法才能使其工作。
public boolean contains(Object o)
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)).
如果此列表包含指定的元素,则返回 true。更正式地说,当且仅当此列表包含至少一个元素 e 使得(o==null ? e==null : o.equals(e))时才返回 true 。
Edit:
If you not ovveriding equals method, Then default Object equals method executes and, as per docs of Equals method
如果您没有覆盖 equals 方法,则执行默认的 Object equals 方法,并且根据 Equals 方法的文档
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).
Object 类的 equals 方法实现了对象上最有区别的可能等价关系;也就是说,对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象(x == y 的值为 true)时,此方法才返回 true。
So your userInputHole == leftFlowInnerHole
is always false as they are pointing to different instances.
所以你userInputHole == leftFlowInnerHole
总是错误的,因为它们指向不同的实例。
Hence to avoid the default implementation ,you just ovveride that equals in yout class and provide your implementation.
因此,为了避免默认实现,您只需在您的类中覆盖它并提供您的实现。
回答by Jamel ESSOUSSI
You should overide the equals method of the Hole class:
您应该覆盖 Hole 类的 equals 方法:
@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;
}
回答by Venkatesh
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
我只是添加了这一行来检查 (for -loop ) 中的条件,然后它工作正常
String gh = String.valueOf(priceidslist.get(j));
if(gh.equals(extraId)){
rw.put("extraPrice",pricelist.get(j));
}