import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; //员工信息类 //date类 public class test50 { private String name; private String sex; private Date birth; public void showme(){ System.out.println(this.getName()); System.out.println(this.getSex()); // System.out.println(DateFormat.getDateInstance().format(this.getBirth())); //System.out.println(this.getBirth()); SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String data=sdf.format(birth); System.out.println(data); } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setSex(String sex) { this.sex = sex; } public String getSex() { return sex; } public void setBirth(Date birth) { this.birth = birth; } public Date getBirth(Date birth) { return this.birth; } } 测试类//arraylist集合类实现类 import java.util.ArrayList; import java.util.Scanner; public class test56 { public static void main(String[] args){ Scanner in =new Scanner(System.in); ArrayList emps=new ArrayList(); System.out.println("请输入三个员工的信息"); //从控制台传输数据到list集合中 for(int i=0;i<3;i++){ test50 emp=new test50(); System.out.println("请输入第"+(i+1)+"个的名字"); System.out.println("输入姓名,回车继续"); String name=in.next(); emp.setName(name); System.out.println("输入性别,回车继续"); String sex=in.next(); emp.setSex(sex); emps.add(emp); } /* 遍历list对象 使用for循环遍历集合*/ for(int i=0;i<emps.size();i++){ Object obj=emps.get(i);//按照集合元素索引获取集合元素对象 test50 emp=(test50)obj;//实现强制转换 System.out.println(emp.getName()+"\t"+emp.getSex()); } } }