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

如何覆盖list里面的信息


Java code


//Usercenter

public  class Usercenter {
    
    private String id;//用户名
    private String password;//密码
    private String email;//邮箱
   
    
    static List <Object> list = new ArrayList<Object> ();
    
    
    
  
    
    //id
    public void setId (String id){
        this.id=id;
    }
    
    public String getId (){
        return id;
    }
    
    //password
    public void setPassword (String password){
        this.password=password;
    }
    
    public String getPassword (){
        return password;
    }
    
  //email
    public void setEmail (String email){
        this.email=email;
    }
    
    public String getEmail (){
        return email;
    }
    















Java code


//添加到list

Usercenter use2 = new Usercenter();

        // 第一次添加

        use2.setIdnum(01);// 获得学号
        use2.setId("张三");// 姓名
                use2.setPassword("1234");//密码
        

        use2.list.add(use2.getIdnum());
        use2.list.add(use2.getId());
                use1.setPassword("abcd");//密码
        

        // 第二次添加
        Usercenter use1 = new Usercenter();
        use1.setIdnum(02);// 获得学号
        use1.setId("李四");// 姓名
                use2.list.add(use2.getPassword);
        

        // 放入list
        use1.list.add(use1.getIdnum());
        use1.list.add(use1.getId());
        use1.list.add(use1.getPassword);





假如要根据学号修改list里面的password或者其他值,应该怎么修改?(我自己想的是:先删除要修改的值,然后在添加,不过貌似行不通。。)

------解决方案--------------------
Java code

package coverlist;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Yanhua.Chen
 * @time 2012-8-26
 */
public class Usercenter {
    private String id;//用户名
    private String password;//密码
    private String email;//邮箱
   
    
    public static List <Usercenter> list = new ArrayList<Usercenter> ();
    
    
    public Usercenter(){
        
    }
    
    public Usercenter(String id){
        this.id = id;
    }
  
    
    //id
    public void setId (String id){
        this.id=id;
    }
    
    public String getId (){
        return id;
    }
    
    //password
    public void setPassword (String password){
        this.password=password;
    }
    
    public String getPassword (){
        return password;
    }
    
  //email
    public void setEmail (String email){
        this.email=email;
    }
    
    public String getEmail (){
        return email;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    /* 
     * 很重要
     * 为了int index = list.indexOf(new Usercenter("id"));他正确
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Usercenter other = (Usercenter) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
    
    
}