Collection 数组存储

 

 

package TestCollection;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;



public class TestEmploy1 {
    
    public static void main(String[] args) throws ParseException {
        
        //一个Employee对象对用一行记录
        Employee e1=new Employee(01,"张三",5000,"数据中心","2016-03");
        Employee e2=new Employee(02,"李四",10000,"数据处理","2017-03");
        Employee e3=new Employee(03,"王五",20000,"研究院","2018-12");
        
        List<Employee> list=new ArrayList<Employee>();
        list.add(e1);
        list.add(e2);
        list.add(e3);
        
        //for遍历
        for(int i=0;i<list.size();i++) {
            Employee e=list.get(i);
            System.out.println(e.getName());
            
        }
    }
    
    
}
package TestCollection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Employee {
    private int id;
    private String name;
    private int salary;
    private String department;
    private Date hireDate;
    
    public Employee(int id,String name,int salary,
            String department,String hireDate) throws ParseException {
        this.id=id;
        this.name=name;
        this.salary=salary;
        this.department=department;
        //时间转换
        DateFormat df=new SimpleDateFormat("yyyy-MM");
        this.hireDate=df.parse(hireDate);
    }
    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 int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public Date getHireDate() {
        return hireDate;
    }
    public void setHireDate(Date hireDate) {
        this.hireDate = hireDate;
    }    

}
package TestCollection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestEmploy2 {
    
    
    public static void main(String[] args) {
        //一个Map对象对应一行记录
        Map map1=new HashMap();
        map1.put("id", 01);
        map1.put("name","张三");
        map1.put("salary",5000);
        map1.put("department","数据中心");
        map1.put("hireDate","2016-03");
        
        Map map2=new HashMap();
        map2.put("id", 02);
        map2.put("name","李四");
        map2.put("salary",10000);
        map2.put("department","数据中心");
        map2.put("hireDate","2017-03");
        
        Map map3=new HashMap();
        map3.put("id", 01);
        map3.put("name","王五");
        map3.put("salary",20000);
        map3.put("department","研究院");
        map3.put("hireDate","2018-12");
        
        
        List<Map> list=new ArrayList<Map>();
        list.add(map1);
        list.add(map2);
        list.add(map3);
        
        //for循环
        for(int i=0;i<list.size();i++) {
            Map map =list.get(i);
            System.out.println(map.get("name"));
            
        }
        
        
        
        System.out.println(list.size());
    }

}

 

posted on 2020-03-15 15:12  happygril3  阅读(175)  评论(0)    收藏  举报

导航