Vector、ArrayList與LinkedList的區別?

LinkedList基於雙向鏈表實現的java

private static class Node<E> {
  E item;
  Node<E> next;
  Node<E> prev;
}

每一個鏈表存儲了 first 和 last 指針:數組

transient Node<E> first;
transient Node<E> last;

Vector:指針

public synchronized boolean add(E e) {
    modCount++;
    ensureCapacityHelper(elementCount + 1);
    elementData[elementCount++] = e;
    return true;
}

public synchronized E get(int index) {
    if (index >= elementCount)
        throw new ArrayIndexOutOfBoundsException(index);

    return elementData(index);
}

ArrayList與LinkedList、Vector的區別:code

  • LinkedList基於雙向鏈表實現的,ArrayList基於動態數組實現,Vector基於動態數組
  • ArrayList支持隨機訪問
  • ArrayList添加元素是,可能會致使數組從新分配,LinkedList添加元素開銷是固定的。
  • LinkedList在任意位置添加和刪除操做效率高。
  • Vector與Arraylist相似,Vector是同步訪問的,訪問速度慢。
  • Vector 每次擴容請求其大小的 2 倍空間,而 ArrayList 是 1.5 倍
相關文章
相關標籤/搜索