集合练习——List部分

利用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,在插入和删除时效率会比较高,但是在 查找时效率会比较低

与一个 很相像的比较 Vector。

两者 拥有的方法是一样的。就是一个镜像一样的存在。唯一区别在于同异步。

Vector是线程安全的,也就是说,在多线程操作的时候他仍然是可靠地,当A个线程对其进行操作的时候,其他任何线程不允许访问,当A线程完成访问以后,其他线程获得访问该集合的权限。而对于ArrayList来说,他是线程不安全的,就是 在线程A在访问ArrayList的时候,别的线程也可以访问的,这样就会不安全。

那么为什么不安全的反而应用更广,因为多个线程可以访问,那么执行效率就高,访问速度就快,所以 在效率上ArrayList扳回一城,在两者的取舍过程中,大家倾向于效率,所以 ArrayList更加火爆。

同样的 既然牵扯到了这个 问题 那就 连带说一下 Stringbuffer 和 StringBuilder

两者 也是镜像般存在的。

StringBuffer 是线程安全的。StringBuilder 是不安全的。

但是 在使用上,我们利用StringBuffer更多一些。猜想,因为 异步的关系,我们希望得到更加靠谱的字符串。如果考虑到正确性的问题还是需要为stringBuilder加上同步锁。所以就干脆直接使用StringBuffer了。

此处为引用:

这个 参考 百度知道的一个文献吧:

1.  在执行速度方面的比较:StringBuilder >  StringBuffer   
2. StringBuffer与StringBuilder,他们是字符串变量,是可改变的对象,每当我们用它们对字符串做操作时,实际上是在一个对象上操作的,不像String一样创建一些对象进行操作,所以速度就快了。
3. StringBuilder:线程非安全的
  StringBuffer:线程安全
当我们在字符串缓冲去被多个线程使用是,JVM不能保证StringBuilder的操作是安全的,虽然他的速度最快,但是可以保证StringBuffer是可以正确操作的。当然大多数情况下就是我们是在单线程下进行的操作,所以大多数情况下是建议用StringBuilder而不用StringBuffer的,就是速度的原因。

对于三者使用的总结:1.如果要操作少量的数据用 = String
       2.单线程操作字符串缓冲区 下操作大量数据 = StringBuilder
       3.多线程操作字符串缓冲区 下操作大量数据 = StringBuffer

引用结束。
附网址链接:http://zhidao.baidu.com/link?url=NdcmurUXZeD30zVPk5o---5Gj0WmSR-9whPpal4Ln_mWC6E1gIq41Ndk3N7QI3APLGOabr7QNNh_T6fGYypHM_


原文地址:https://www.cnblogs.com/letben/p/5182947.html