底层使用双向链表进行存储
    如果需要对数据频繁的插入和删除使用LinkedList的效率更高

    属性

    1. transient int size = 0;
    2. /**
    3. * Pointer to first node.
    4. * Invariant: (first == null && last == null) ||
    5. * (first.prev == null && first.item != null)
    6. */
    7. transient Node<E> first;
    8. /**
    9. * Pointer to last node.
    10. * Invariant: (first == null && last == null) ||
    11. * (last.next == null && last.item != null)
    12. */
    13. transient Node<E> last;

    存储单元Node
    Node就是双向链表中的每一个结点。

    1. private static class Node<E> {
    2. E item;
    3. Node<E> next;
    4. Node<E> prev;
    5. Node(Node<E> prev, E element, Node<E> next) {
    6. this.item = element;
    7. this.next = next;
    8. this.prev = prev;
    9. }
    10. }

    add方法

    1. public boolean add(E e) {
    2. linkLast(e);
    3. return true;
    4. }

    linkLast( )方法

    1. private void linkFirst(E e) {
    2. final Node<E> f = first;
    3. final Node<E> newNode = new Node<>(null, e, f);
    4. first = newNode;
    5. if (f == null)
    6. last = newNode;
    7. else
    8. f.prev = newNode;
    9. size++;
    10. modCount++;
    11. }