LinkedList&ArrayList

linkedlist is a doubly linked list; and is a variable length array ArrayList

In general, insert and delete data linkedlist efficient than arraylist high;

 

 

 

 

1, when the tail insert elements: ① the amount of data of tens of thousands of: linkedlist high efficiency, because there linkedlist tail pointer, increments will linkedlist a new object node elements, data out of the new target node is less less, ArrayList linkedlist new node time is greater than the expansion time, the high efficiency linkedlist; ② the amount of data when more than 10 million ArrayList high efficiency, a new node will be the object because linkedlist time increments element, a small amount of data out of the new node is also less subject , but when the amount of data, the new new node corresponding to the object is increased, so new new node time is greater than the expansion time, there will be more efficient than ArrayList linkedlist

2, insert / delete data more forward position, linkedlist efficient than ArrayList, because linkedlist traverse position to spend less time insert, and the original ArrayList need all the elements of an array once the array copy

3, insert / delete data more to the middle position, ArrayList efficient than linkedlist, because linkedlist need to traverse the middle of a query comparing a waste of time, so slow

import java.util.*;
public class Test
{
    public static void main(String[] args)
    {
        ArrayList<Integer> List = new ArrayList<Integer>();
        long start=System.currentTimeMillis();
        for( int i=0;i <10000;i++ ) //给数组增加10个Int元素
            List.add(i/2,i);
        System.out.println(System.currentTimeMillis()-start);
        //System.out.println(List);
        
        LinkedList<Integer> LList = new LinkedList<Integer>();
        long startL=System.currentTimeMillis();
        for( int i=0;i <10000;i++ ) //给数组增加10个Int元素
            LList.add(i/2,i);
        System.out.println(System.currentTimeMillis()-startL);
        //System.out.println(LList);
    }
}

结果:
ArrayList 19毫秒

LinkList 129毫秒
View Code

 

Guess you like

Origin www.cnblogs.com/enhance/p/11311794.html