多维数组的运用 增删改查

来源:互联网 发布:dpp软件下载 编辑:程序博客网 时间:2024/05/16 00:25
import java.util.Arrays;


public class MangerPerson{
 
    private Person[] ps=new Person[3];//多维数组
    private int count=0;
    
    //多维数组的添加
    public void add(Person p){
        
        if(count>=ps.length)
        {
          count+=(count)/2;    
          ps=Arrays.copyOf(ps, count);
        }
        
        ps[count]=p;
        count++;
        
    }
    //多维数组的删除
    public void del(Person p){
        for(int i=0;i<ps.length;i++)
        {
            if(p.getName().equals(ps[i].getName()))
            {
                //循环把后面对象往前移动
                for(int j=i;j<count-1;j++)
                {
                    ps[j]=ps[j+1];
                }
                
                ps[count-1]=null;
                count--;
                break;
            }
        }
    }
    //查看数组
    public Person find(Person p){
        for(int i=0;i<ps.length;i++)
        {
            if(ps[i].getName().equals(p.getName())){
                return ps[i];
            }
        }
        return null;
    }
    //修改数组
    public void update(Person p)
    {
        Person p1=find(p);
        if(p1!=null)
        {
            p1.setSex(p.getSex());
        }
        
    }
    //编译数组
    public void list(){
        for(Person a:ps)
        {
            System.out.println(a.getName()+"==="+a.getSex());
        }
    }

}


//--------------------------------------------------------------------------------------------


public class Person {
   private String name;
   private char sex;
   public Person() {
    super();
    // TODO Auto-generated constructor stub
   }
    public Person(String name, char sex) {
        super();
        this.name = name;
        this.sex = sex;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public char getSex() {
        return sex;
    }
    public void setSex(char sex) {
        this.sex = sex;
    }
   
   public String say(){
       return "我是:"+this.name+"我的性别是:"+this.sex;
   }
}



0 0
原创粉丝点击