集合练习

利用ArrayList

1、存储多个员工信息,包括工号,姓名,年龄,入职时间,逐条打印所有员工姓名,并输出员工个数。

package CollectionPart;

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

public class ArrayListPractise_1 {
    
    public static void main(String[] args) {
        
        Employee_1 e1 = new Employee_1("dept1_001", "lifei", 24, "2016-03-28");
        Employee_1 e2 = new Employee_1("dept1_002", "li2fe", 23, "2015-01-22");
        Employee_1 e3 = new Employee_1("dept1_003", "lifei3", 28, "2016-02-05");
        Employee_1 e4 = new Employee_1("dept2_001", "lif4i", 25, "2017-03-28");
        
        List<Employee_1> myList1 = new ArrayList();
        myList1.add(e1);
        myList1.add(e2);
        myList1.add(e3);
        myList1.add(e4);
        
        for (Employee_1 employee_1 : myList1) {
            System.out.println(employee_1.getEmployeeName());
        }
        System.out.println("员工总个数为:"+myList1.size());
        
    }

}
package CollectionPart;

public class Employee_1 {
    
    private String employeeId;
    private String employeeName;
    private int employeeAge;
    private String employeeHireDate;
    /*
    这里就存储成String 类型,在 数据库里面设置成 date格式。
    然后 利用Date today = new Date();
    SimpleDateFormat fm = new SimpleDateFormat("YYYY-MM-dd");
    来做
    */
    public String getEmployeeId() {
        return employeeId;
    }
    public void setEmployeeId(String employeeId) {
        this.employeeId = employeeId;
    }
    public String getEmployeeName() {
        return employeeName;
    }
    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }
    public int getEmployeeAge() {
        return employeeAge;
    }
    public void setEmployeeAge(int employeeAge) {
        this.employeeAge = employeeAge;
    }
    public String getEmployeeHireDate() {
        return employeeHireDate;
    }
    public void setEmployeeHireDate(String employeeHireDate) {
        this.employeeHireDate = employeeHireDate;
    }
    @Override
    public String toString() {
        return "Employee_1 [employeeId=" + employeeId + ", employeeName=" + employeeName + ", employeeAge="
                + employeeAge + ", employeeHireDate=" + employeeHireDate + "]";
    }
    public Employee_1(String employeeId, String employeeName, int employeeAge, String employeeHireDate) {
        super();
        this.employeeId = employeeId;
        this.employeeName = employeeName;
        this.employeeAge = employeeAge;
        this.employeeHireDate = employeeHireDate;
    }
    public Employee_1() {
        super();
    }
    

}

 

练习2:

当有员工入职时添加,员工信息,当有员工离职时,删除该信息。

示例代码:【有bug,在 输入日期的时候,没等到输入日期,就直接询问是否还有入职的同志】

下面算是 为 解决 ConcurrentModificationException 提出了一个 方案。就是 得到 当前元素的 下标然后再删除。下面的代码没有怎么写注释,如果有问题的话欢迎讨论。

package CollectionPart;

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

public class ArrayListPractise_2 {
    
    public static void main(String[] args) {
        ArrayListPractise_2 ap2 = new ArrayListPractise_2();
        
        List<Employee_1> myList = new ArrayList();
        System.out.println("是否有员工入职?");
        Scanner in = new Scanner(System.in);
        String string = in.nextLine();
        while(string.equals("是")){
            System.out.println("请输入员工的编号");
            String id = in.nextLine();
            System.out.println("请输入员工的姓名");
            String name = in.nextLine();
            System.out.println("请输入员工的年龄");
            int age = in.nextInt();
            System.out.println("请输入员工的入职时间");
            String hireDate = in.nextLine();
            System.out.println("?");
            Employee_1 e1 = new Employee_1(id, name, age, hireDate);
            myList.add(e1);
            System.out.println("是否还有新员工入职?");
            string = in.nextLine();
        }
        
        
        System.out.println("共有员工:"+myList.size()+"人");
        
        List<Employee_1> newList =null;
        System.out.println("是否有员工离职?");
        string  = in.nextLine();
        while(string.equals("是")){
            System.out.println("该员工的姓名是");
            String name = in.nextLine();
            newList = ap2.deleteFromTheList(myList,name);
            System.out.println("是否还有员工离职?");
            string = in.nextLine();
        }
        for (Employee_1 employee_1 : newList) {
            System.out.println("员工人数为:"+newList.size());
        }
        
    }

    private List<Employee_1> deleteFromTheList(List<Employee_1> myList, String name) {

        int index = 0;
        boolean hasTheElement = false; 
        for(int i = 0; i<myList.size();i++){
            if(myList.get(i).getEmployeeName().equals(name)){
                index = i;
                hasTheElement = true;
            }
        }
        if(!hasTheElement){
            System.out.println("不存在此元素");
        }else{
            myList.remove(index);
        }
        return myList;
    }

}

ArrayList 和 LinkedList 分别在什么时候使用?

ArrayList,在遍历元素和随机访问元素时效率会比较高,在插入删除操作情况下效率会比较低

LinkedList,在插入和删除时效率会比较高,但是在 查找时效率会比较低

 

文章来自:http://www.cnblogs.com/letben/p/5182947.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3