什麼狀況下ArrayList增刪 比LinkedList 更快


public
static void main(String[] args){ final int MAX_VAL = 10000; List<Integer> linkedList = new LinkedList<Integer>(); List<Integer> arrayList = new ArrayList<Integer>(); for(int i = 0; i < MAX_VAL; i++) { linkedList.add(i); arrayList.add(i); } long time = System.nanoTime(); for(int i = 0; i < MAX_VAL; i++) { linkedList.add(MAX_VAL/2, i); } System.out.println("LL time: " + (System.nanoTime() - time)); time = System.nanoTime(); for(int i = 0; i < MAX_VAL; i++) { arrayList.add(MAX_VAL/2, i); } System.out.println("AL time: " + (System.nanoTime() - time)); }

從中間插入結果:java

怎麼會這樣, 不該該是LinkedList更快嗎? ArrayList底層是數組, 添加數據須要移動後面的數據, 而LinkedList使用的是鏈表, 直接移動指針就行, 按理說應該是LinkedList更快.node

再來看數組

從尾插入函數

public static void main(String[] args){
        final int MAX_VAL = 10000;
        List<Integer> linkedList = new LinkedList<Integer>();
        List<Integer> arrayList = new ArrayList<Integer>();
        for(int i = 0; i < MAX_VAL; i++) {
            linkedList.add(i);
            arrayList.add(i);
        }
        long time = System.nanoTime();
        for(int i = 0; i < MAX_VAL; i++) {
            linkedList.add(i);
        }
        System.out.println("LL time: " + (System.nanoTime() - time));
        time = System.nanoTime();
        for(int i = 0; i < MAX_VAL; i++) {
            arrayList.add(i);
        }
        System.out.println("AL time: " + (System.nanoTime() - time));
    }

從頭開始插入spa

public static void main(String[] args){
        final int MAX_VAL = 10000;
        List<Integer> linkedList = new LinkedList<Integer>();
        List<Integer> arrayList = new ArrayList<Integer>();
        for(int i = 0; i < MAX_VAL; i++) {
            linkedList.add(i);
            arrayList.add(i);
        }
        long time = System.nanoTime();
        for(int i = 0; i < MAX_VAL; i++) {
            linkedList.add(0,i);
        }
        System.out.println("LL time: " + (System.nanoTime() - time));
        time = System.nanoTime();
        for(int i = 0; i < MAX_VAL; i++) {
            arrayList.add(0,i);
        }
        System.out.println("AL time: " + (System.nanoTime() - time));
    }

結果3d

而後從三分之一的位置開始插入指針

結果code

從三分之二的位置插入blog

結果ci

 

源碼部分

LinkedList源碼

// 在index前添加節點,且節點的值爲element
public void add(int index, E element) {
    addBefore(element, (index==size ? header : entry(index)));
}

// 獲取雙向鏈表中指定位置的節點
private Entry<E> entry(int index) {
    if (index < 0 || index >= size)
        throw new IndexOutOfBoundsException("Index: "+index+
                                            ", Size: "+size);
    Entry<E> e = header;
    // 獲取index處的節點。
    // 若index < 雙向鏈表長度的1/2,則從前向後查找;
    // 不然,從後向前查找。
    if (index < (size >> 1)) {
        for (int i = 0; i <= index; i++)
            e = e.next;
    } else {
        for (int i = size; i > index; i--)
            e = e.previous;
    }
    return e;
}

// 將節點(節點數據是e)添加到entry節點以前。
private Entry<E> addBefore(E e, Entry<E> entry) {
    // 新建節點newEntry,將newEntry插入到節點e以前;而且設置newEntry的數據是e
    Entry<E> newEntry = new Entry<E>(e, entry, entry.previous);
    // 插入newEntry到鏈表中
    newEntry.previous.next = newEntry;
    newEntry.next.previous = newEntry;
    size++;
    modCount++;
    return newEntry;

從中,咱們能夠看出:經過add(int index, E element)向LinkedList插入元素時。先是在雙向鏈表中找到要插入節點的位置index;找到以後,再插入一個新節點
雙向鏈表查找index位置的節點時,有一個加速動做若index < 雙向鏈表長度的1/2,則從前向後查找; 不然,從後向前查找

接着,咱們看看ArrayList.java中向指定位置插入元素的代碼。以下:

// 將e添加到ArrayList的指定位置
public void add(int index, E element) {
    if (index > size || index < 0)
        throw new IndexOutOfBoundsException(
        "Index: "+index+", Size: "+size);

    ensureCapacity(size+1);  // Increments modCount!!
    System.arraycopy(elementData, index, elementData, index + 1,
         size - index);
    elementData[index] = element;
    size++;
}

ensureCapacity(size+1) 的做用是「確認ArrayList的容量,若容量不夠,則增長容量。
真正耗時的操做是 System.arraycopy(elementData, index, elementData, index + 1, size - index);

 

實際上,咱們只須要了解: System.arraycopy(elementData, index, elementData, index + 1, size - index); 會移動index以後全部元素便可這就意味着,ArrayList的add(int index, E element)函數,會引發index以後全部元素的改變!

 

結論

如今大概知道了,插入位置的選取對LinkedList有很大的影響,一直往數據中間部分插入刪除的時候,ArrayList比LinkedList更快

緣由大概就是當數據量大的時候,system.arraycopy的效率要比每次插入LinkedList都須要從端查找index和分配節點node來的更快。

總之,對於99%或更多的現實狀況,ArrayList是更好的選擇,而且利用LinkedList的狹隘優點須要很是當心。

參考:https://stackoverflow.com/questions/16808777/is-linkedlist-really-faster-than-arraylist-in-the-case-of-insertion-in-the-middl

相關文章
相關標籤/搜索