ArrayList在头尾操作元素和LinkedList使用特有方法在头尾操作元素的效率 提示 : 效率比较, 就是ArrayList 头尾增删用的时间, 与LinkedList头尾增删用的时间,

public class Homework04_ {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add("abc");
        addArrayList(list);
        LinkedList list1 = new LinkedList();
        list.add("123");
        addLinkedList(list1);
    }

    public static void addArrayList(ArrayList list){
        long start = System.currentTimeMillis();
        for (int i = 1; i < 10000;i++){
            list.add(0,"first");
            list.add(list.size()-1,"end");
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    public static void addLinkedList(LinkedList list){
        long start = System.currentTimeMillis();
        for (int i = 1; i < 10000;i++){
            list.addFirst("first");
            list.addLast("end");
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}
上一篇:Java中栈和队列的实现类


下一篇:2021-08-06 List的子类 ArrayList,LinkedList集合