日期:2014-05-20  浏览次数:20752 次

ArrayList容器中怎么根据输出Id删除对象元素
People.java
Java code
public class People {
    private  int id;//联系人id
       private String name;//联系人姓名
       private String pass;//联系人密码
       private  String num;//联系人电话
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPass() {
        return pass;
    }
    public void setPass(String pass) {
        this.pass = pass;
    }
    public String getNum() {
        return num;
    }
    public void setNum(String num) {
        this.num = num;
    }
    
    public People(int id,String name,String pass,String num){
        this.id=id;
        this.name=name;
        this.pass=pass;
        this.num=num;
    }
}


------解决方案--------------------
方法正确 要在People中重写一下 equals方法 
在People类中加如下代码
Java code
    @Override
    public boolean equals(Object obj) {
        if(obj == null) {
            return false;
        } else if(obj.getClass() == this.getClass()) {
            People p = (People)obj;
            return p.id == this.id;
        }
        return false;
    }
    @Override
    public int hashCode() {
        int type = this.getClass().hashCode();
        return id + type*41;
    }