我想在第ArrayList中使用包含方法,其中存储了Employee类的对象。但是,当我用这个方法,它总是返回false即使我实现了在Employee类的equals方法在ArrayList中使用包含方法
这是我的尝试:
Employee类:
public class Employee {
.
.
.
@Override
public boolean equals(Object o){
if(o instanceof Employee)
{
Employee e = (Employee) o;
return this.id==(e.id);
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
hash = 79 * hash + (int) (this.id^(this.id >>> 32));
return hash;
}
测试类 :
public class Test {
LinkedList <Employee>list=new LinkedList <Employee>();
Employee e=new Employee();
long id=0;
e.setId(20710456);
list.add(e);
e.setId(30560432);
list.add(e);
public void Edit()
{
id=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the id of the
employee"));
e.setId(id);
System.out.print(list.contains(e.getId()));
if(list.contains(e.getId())){
.
.
//rest of the code
}
}
}
请给一个简短而*完整*例子,最好有更好的压痕。我们不知道你的ID是什么,你的代码永远不会给列表添加任何东西...... – 2013-04-03 21:03:08
@JonSkeet,该列表包含ida,但我没有提到为快点:eidted – 2013-04-03 21:11:57
将来,请花点时间问一个问题,确保你已经包含了所有相关的*和*尽可能好的内容。阅读http://tinyurl.com/so-list – 2013-04-03 21:14:10