/** 删除指定位置的元素 */ public E remove(int index){ rangeCheck(index);
modCount++; // 返回被删除的元素值 E oldValue = elementData(index);
int numMoved = size - index - 1; if (numMoved > 0) // 将 index + 1 及之后的元素向前移动一位,覆盖被删除值 System.arraycopy(elementData, index+1, elementData, index, numMoved); // 将最后一个元素置空,并将 size 值减1 elementData[--size] = null; // clear to let GC do its work
return oldValue; }
E elementData(int index){ return (E) elementData[index]; }
/** 删除指定元素,若元素重复,则只删除下标最小的元素 */ publicbooleanremove(Object o){ if (o == null) { for (int index = 0; index < size; index++) if (elementData[index] == null) { fastRemove(index); returntrue; } } else { // 遍历数组,查找要删除元素的位置 for (int index = 0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); returntrue; } } returnfalse; }
/** 快速删除,不做边界检查,也不返回删除的元素值 */ privatevoidfastRemove(int index){ modCount++; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work }
public E get(int index){ checkElementIndex(index); return node(index).item; }
Node<E> node(int index){ /* * 则从头节点开始查找,否则从尾节点查找 * 查找位置 index 如果小于节点数量的一半, */ if (index < (size >> 1)) { Node<E> x = first; // 循环向后查找,直至 i == index 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; } }
/** 在链表指定位置插入元素 */ publicvoidadd(int index, E element){ checkPositionIndex(index); // 判断 index 是不是链表尾部位置,如果是,直接将元素节点插入链表尾部即可 if (index == size) linkLast(element); else linkBefore(element, node(index)); }
/** 将元素节点插入到链表尾部 */ voidlinkLast(E e){ final Node<E> l = last; // 创建节点,并指定节点前驱为链表尾节点 last,后继引用为空 final Node<E> newNode = new Node<>(l, e, null); // 将 last 引用指向新节点 last = newNode; // 判断尾节点是否为空,为空表示当前链表还没有节点 if (l == null) first = newNode; else l.next = newNode; // 让原尾节点后继引用 next 指向新的尾节点 size++; modCount++; }
/** 将元素节点插入到 succ 之前的位置 */ voidlinkBefore(E e, Node<E> succ){ // assert succ != null; final Node<E> pred = succ.prev; // 1. 初始化节点,并指明前驱和后继节点 final Node<E> newNode = new Node<>(pred, e, succ); // 2. 将 succ 节点前驱引用 prev 指向新节点 succ.prev = newNode; // 判断尾节点是否为空,为空表示当前链表还没有节点 if (pred == null) first = newNode; else pred.next = newNode; // 3. succ 节点前驱的后继引用指向新节点 size++; modCount++; }
publicbooleanremove(Object o){ if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); returntrue; } } } else { // 遍历链表,找到要删除的节点 for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); // 将节点从链表中移除 returntrue; } } } returnfalse; }
public E remove(int index){ checkElementIndex(index); // 通过 node 方法定位节点,并调用 unlink 将节点从链表中移除 return unlink(node(index)); }
/** 将某个节点从链表中移除 */ 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;
// prev 为空,表明删除的是头节点 if (prev == null) { first = next; } else { // 将 x 的前驱的后继指向 x 的后继 prev.next = next; // 将 x 的前驱引用置空,断开与前驱的链接 x.prev = null; }
// next 为空,表明删除的是尾节点 if (next == null) { last = prev; } else { // 将 x 的后继的前驱指向 x 的前驱 next.prev = prev; // 将 x 的后继引用置空,断开与后继的链接 x.next = null; }