java 容器-数据存储练习

package collection;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
*容器存储综合练习:如何存储下面表格中的数据

  • ID 姓名 薪水 部门 入职时间

  • 0101 张三 5000 项目部 2019-10

  • 0102 李四 5500 施工部 2019-11

  • 0103 王伟 6000 人事部 2019-12

  • @author Administrator
    */
    public class Employee {//Employee雇员 也称为javabean 实体类
    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, Date hireDate) {
    //把 Date换成String
    public Employee(int id, String name, int salary, String department, String hireDate) {
    super();
    this.id = id;
    this.name = name;
    this.salary = salary;
    this.department = department;

     //在这里进行格式化
     DateFormat format = new SimpleDateFormat("yyyy-MM");
     try {
     	this.hireDate = format.parse(hireDate);//捕获异常
     } catch (ParseException e) {
     	e.printStackTrace();
     }
    

    }

    //获取set和get方法
    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 collection;
/**

  • 1: 一个对象对应一行记录 javabean 实体类
    */
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;

public class test01 {
public static void main(String[] args) {
//存数据, 一个对象对应一行记录
Employee e = new Employee(0101,“张三”,5000,“项目部”,“2019-10”);
Employee e1 = new Employee(0102,“李四”,5500,“施工部”,“2019-11”);
Employee e2 = new Employee(0103,“王伟”,6000,“人事部”,“2019-12”);

	//创建容器 ;<Employee>泛型即容器里只能放Employee类里的对象,其他类的对象放不进去
	List<Employee> list = new ArrayList<Employee>();
	
	list.add(e);
	list.add(e1);
	list.add(e2);
	
	//调用
	printEmpName(list);
	//这部分可以放到构造器里进行封装
	/*e.setId(0101);
	e.setName("张三");
	e.setSalary(5000);
	e.setDepartment("项目部");
	
	String strDate = "2019-10";//转成字符串
	
	DateFormat format = new SimpleDateFormat("yyyy-MM");
	try {
		e.setHireDate(format.parse(strDate));//捕获异常
	} catch (ParseException e1) {
		e1.printStackTrace();
	}*/
	
}
//打印名字
public static void printEmpName(List<Employee> list){
	for(int i=0;i<list.size();i++){//size大小;尺寸
		System.out.println(list.get(i).getName());
	}
}

}
//---------------------------------------------------
package collection;

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

/**

  • 2:用容器:一个map对象对应一行记录
  • @author Administrator

*/
public class Test02 {
public static void main(String[] args) {
Map map = new HashMap();
//一个map对象对应一行记录
map.put(“id”,0101);
map.put(“name”, “张三三”);
map.put(“salary”, 5000);
map.put(“department”, “项目部”);
map.put(“hireDate”, “2019-10”);

	Map map1 = new HashMap();
	map1.put("id",0102);
	map1.put("name", "李四");
	map1.put("salary", 5500);
	map1.put("department", "施工部");
	map1.put("hireDate", "2019-11");
	
	Map map2 = new HashMap();
	map2.put("id",0103);
	map2.put("name", "王伟");
	map2.put("salary", 6000);
	map2.put("department", "人事部");
	map2.put("hireDate", "2019-12");
	
	//创建容器
	List<Map> list = new ArrayList<Map>();
	list.add(map);
	list.add(map1);
	list.add(map2);
	
	printEmpName(list);
	
}
//打印名字
	public static void printEmpName(List<Map> list){
		for(int i=0;i<list.size();i++){//size大小;尺寸
			Map tempMap = list.get(i);
					
			System.out.println(tempMap.get("name")+"--"+tempMap.get("salary"));
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值