LinkedList底层分析
源码场景
LinkedList<String> list = new LinkedList<>();
list.add("小宇");
list.add("小蒲");
list.add("小康");
源码分析
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
protected transient int modCount = 0;
}
public abstract class AbstractSequentialList<E> extends AbstractList<E> {
}
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>{
transient int size = 0;
transient Node<E> first;
transient Node<E> last;
public LinkedList() {
}
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++;
}
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;
}
}
}