美文网首页
JDK 源码解析 —— LinkedList

JDK 源码解析 —— LinkedList

作者: 01_小小鱼_01 | 来源:发表于2018-05-27 21:24 被阅读10次

LinkedList 是线程不安全的,允许元素为null的双向链表。因其底层数据结构是链表,所以可想而知,它的增删只需要移动指针即可,故时间效率较高。不需要批量扩容,也不需要预留空间,所以空间效率比ArrayList高。

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable

成员变量

transient int size = 0;
transient Node<E> first;
transient Node<E> last;

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

    Node(Node<E> prev, E element, Node<E> next) {
        this.item = element;
        this.next = next;
        this.prev = prev;
    }
}

成员函数

public boolean add(E e) {
    linkLast(e);
    return true;
}

void linkLast(E e) {
    final Node<E> l = last;
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
}

相关文章

网友评论

      本文标题:JDK 源码解析 —— LinkedList

      本文链接:https://www.haomeiwen.com/subject/vneijftx.html