/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/transient Node<E> first; // 第一个节点
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/transient Node<E> last; // 最后一个节点
构造函数
/**
* Constructs an empty list.
*/publicLinkedList() {
}
/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/publicLinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
/**
* Returns {@code true} if this list contains the specified element.
* More formally, returns {@code true} if and only if this list contains
* at least one element {@code e} such that
* <tt>(o==null ? e==null : o.equals(e))</tt>.
*
* @param o element whose presence in this list is to be tested
* @return {@code true} if this list contains the specified element
*/publicbooleancontains(Object o) {
return indexOf(o) != -1;
}
publicintindexOf(Object o) {
int index = 0;
// o 为null的时候if (o == null) {
// 遍历整个链表for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null)
return index; // 返回对象所在的位置
index++;
}
} else {
// o 不为null的时候// 遍历for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item))
return index;
index++;
}
}
// 没有找到return -1;
}
添加:
/**
* Appends the specified element to the end of this list.
*
* <p>This method is equivalent to {@link #addLast}.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/publicbooleanadd(E e) {
linkLast(e);
returntrue;
}
/**
* Links e as last element.
*/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++; // fast - fail
}
清空:
/**
* Removes all of the elements from this list.
* The list will be empty after this call returns.
*/publicvoidclear() {
// Clearing all of the links between nodes is "unnecessary", but:// - helps a generational GC if the discarded nodes inhabit// more than one generation 帮助gc的工作// - is sure to free memory even if there is a reachable Iterator 即使有迭代器可达for (Node<E> x = first; x != null; ) {
Node<E> next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
first = last = null;
size = 0;
modCount++;
}
GET
public E get(int index) {
checkElementIndex(index); // 检验边界return node(index).item;
}
/**
* Returns the (non-null) Node at the specified element index.
* 返回指定的结点
*/
Node<E> node(int index) {
// assert isElementIndex(index);// 这里有优化,当index小于size的一半// 从第一个向后遍历,否则从后向前遍历if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
移除:
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
/**
* Unlinks non-null node x.
* 返回移除的结点
*/
E unlink(Node<E> x) {
// assert x != null;final E element = x.item;
final Node<E> next = x.next;
final Node<E> prev = x.prev;
if (prev == null) {
first = next; // x 为头结点,删除后next成为头结点
} else {
prev.next = next; // 略过 x
x.prev = null; //
}
if (next == null) {
last = prev; // x是尾结点,删除后 pre是最后一个
} else {
next.prev = prev;
x.next = null;
}
x.item = null; // x.pre next item = null 让gc回收
size--;
modCount++; // fast - failreturn element;
}
新迭代器: 可向前向后
public ListIterator<E> listIterator(int index) {
checkPositionIndex(index);
returnnew ListItr(index);
}
public E previous() {
checkForComodification();
if (!hasPrevious())
thrownew NoSuchElementException();
lastReturned = next = (next == null) ? last : next.prev;
nextIndex--;
return lastReturned.item;
}