[英]how to subtract ArrayList using removeall() in java?
我在Java中有数组列表:
List<Correction> Auv=new ArrayList<>();
List<Correction> Produv=new ArrayList<>();
然后我想用Auv减去Produv的值,这是一个例子:
Produv.add(new Correction("a","b"));
Produv.add(new Correction("a","c"));
Produv.add(new Correction("b","d"));
Produv.add(new Correction("b","c"));
Auv.add(new Correction("c","a"));
Auv.add(new Correction("b","c"));
Produv.removeall(Auv);
但是没有减去任何东西,数组仍然包含它的初始值,有什么办法吗? 我尝试覆盖equals(),仍然得到相同的结果
这里是我的改正类的代码:
public class Correction {
private String node0;
private String node1;
public Correction(String node0, String node1) {
this.node0 = node0;
this.node1 = node1;
}
public void setNode0(String node0){
this.node0=node0;
}
public void setNode1(String node1){
this.node1=node1;
}
public String getNode0(){
return node0;
}
public String getNode1(){
return node1;
}
@Override
public boolean equals(Object object){
boolean same = false;
if (object != null && object instanceof Correction)
{
same = this.node0 == ((Correction) object).node1 && this.node1 == ((Correction) object).node1;
}
return same;
}
}
解决了!! 这只是在重写equals()方法(感谢伙计们)时出现的错误,这里是我的修正:
@Override
public boolean equals(Object object){
boolean same = false;
if (object != null && object instanceof Correction)
{
same = (this.node0 == ((Correction) object).node1 && this.node1 == ((Correction) object).node0)||(this.node0 == ((Correction) object).node0 && this.node1 == ((Correction) object).node1);
}
return same;
}
您的equals方法看起来不对。 比较this.node0
与((Correction) object).node0
更有意义。
我认为应该是:
public boolean equals(Object object){
boolean same = false;
if (object != null && object instanceof Correction)
{
same = this.node0.equals(((Correction) object).node0) && this.node1.equals(((Correction) object).node1);
}
return same;
}
另外,这是错字吗?
Auv.add("c","a");
Auv.add("b","c");
可能应该是:
Auv.add(new Correction ("c","a"));
Auv.add(new Correction ("b","c"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.