/** * Links e as last element. */ voidlinkLast(E e){ // 获取当前 last 节点 final Node<E> l = last; // 创建一个新节点,节点的前驱节点为原 last 节点,节点的后继节点为 null final Node<E> newNode = new Node<>(l, e, null); // 将当前节点置为末尾绩点 last = newNode; // 如果原 last 节点为 null,就说明链表为空,将 first 设置为新节点 if (l == null) first = newNode; // 如果原末尾节点不为 null,将原 last 节点的后继节点指向新节点 else l.next = newNode; // 链表大小 +1 size++; // 修改次数 + 1 modCount++; }
add(int index, E element)
将指定元素插入到列表中的指定位置。通过改变后续元素的指向。
1 2 3 4 5 6 7 8 9 10 11
publicvoidadd(int index, E element){ // 检查给定的索引是否合法 checkPositionIndex(index);
// 如果索引值等于链表大小,就相当于在尾部追加节点 if (index == size) linkLast(element); // 调用 linkBefore ,在指定节点前插入元素 element else linkBefore(element, node(index)); }
node方法根据索引返回指定位置的节点,其实就是遍历链表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/** * Returns the (non-null) Node at the specified element index. */ Node<E> node(int index){ // assert isElementIndex(index); // 等同于 index < size/2,如果索引在链表的前半部分就从 first 节点往后遍历 if (index < (size >> 1)) { Node<E> x = first; for (int i = 0; i < index; i++) x = x.next; return x; // 如果 索引在链表的后半部分就从 last 节点往前遍历 } else { Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }
publicbooleanremove(Object o){ // 如果 o 为 null,就移除第一个元素为 null 的节点 if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); returntrue; } } // 否则移除第一个 equals o 的元素节点 } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); returntrue; } } } returnfalse; }
/** * Unlinks non-null node x. */ E unlink(Node<E> x){ // assert x != null; // 获取当前节点的元素 element final E element = x.item; // 获取当前节点的后继节点 next final Node<E> next = x.next; // 获取当前节点的前驱节点 prev final Node<E> prev = x.prev;
// 如果 prev 节点为 null,说明是 first 节点,就将 next 节点指向 first if (prev == null) { first = next; } // 否则 else { // 将 prev 节点的后继节点指向 next 节点 prev.next = next; // 当前节点的前驱节点置为 null x.prev = null; } // 如果 next 节点为 null,说明是 last 节点,就将 prev 节点指向 last if (next == null) { last = prev; } // 否则 else { /// 将 next 节点的前驱节点指向 prev 节点 // 当前节点的后继节点置为 null next.prev = prev; x.next = null; } // 当前节点元素置为 null x.item = null; // 链表大小 -1 size--; // 修改次数 + 1 modCount++; // 返回被移除的元素 return element; }
removeFirst()
移除链表的 first 节点并返回被移除的元素
1 2 3 4 5 6 7 8 9 10
public E removeFirst(){ // 获取当前 first 节点 final Node<E> f = first; // 如果 first 节点为 null,也就是链表为空,就抛出 NoSuchElementException 异常 if (f == null) thrownew NoSuchElementException(); // 移除 first 节点 return unlinkFirst(f); }
/** * Unlinks non-null first node f. */ private E unlinkFirst(Node<E> f){ // assert f == first && f != null; // 获取 f 节点的元素 final E element = f.item; // 获取 f (原 first)节点的后继节点 next final Node<E> next = f.next; // 将 f 节点 元素置为 null f.item = null; // 将 f 节点 后继节点置为 null f.next = null; // help GC // 将 first 节点指向 f (原 first) 节点的 next 节点 first = next; // 如果 next 节点为 null,说明此时链表为空,last 节点也应该为 null if (next == null) last = null; // 否则,将 next (当前 first)节点的前驱节点置为 null else next.prev = null; // 链表大小 -1 size--; // 修改次数 + 1 modCount++; // 返回被移除的元素 return element; }
removeLast()
移除链表的 last 节点并返回被移除的元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** * Removes and returns the last element from this list. * * @return the last element from this list * @throws NoSuchElementException if this list is empty */ public E removeLast(){ // 获取当前 last 节点 final Node<E> l = last; // 如果 last 节点为 null,也就是链表为空,就抛出 NoSuchElementException 异常 if (l == null) thrownew NoSuchElementException(); // 移除 last 节点 return unlinkLast(l); }