/** * 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); }
/** * Appends the specified element to the end of this list. * 将指定的元素附加到此列表的末尾。这个方法没什么好讲的,就是调用本类中的linkLast。 * <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; }
/** * Inserts the specified element at the specified position in this list. * Shifts the element currently at that position (if any) and any * subsequent elements to the right (adds one to their indices). * *在此列表中的指定位置插入指定元素。 *将当前在该位置的元素(如果有)和任何后续元素向右移动(向它们的索引添加一个)。 * @param index index at which the specified element is to be inserted index 要插入指定元素的索引 * @param element element to be inserted 要插入的元素元素 * @throws IndexOutOfBoundsException {@inheritDoc} */ publicvoidadd(int index, E element){ //校验index是否在范围内 checkPositionIndex(index);
/** * Returns the (non-null) Node at the specified element index. * 返回指定元素索引处的(非空)节点。 */ Node<E> node(int index){ // assert isElementIndex(index);
//下面的判断是判断应该从头循环还是从尾循环好一些 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; } }
/** * Inserts element e before non-null Node succ. * 在非空节点 succ 之前插入元素 e */ voidlinkBefore(E e, Node<E> succ){ // assert succ != null; final Node<E> pred = succ.prev; final Node<E> newNode = new Node<>(pred, e, succ); succ.prev = newNode; //如果前置节点地址为空,说明是要插入首节点 if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++; }
/** * Links e as last element.链接 e 作为最后一个元素。 */ voidlinkLast(E e){ final Node<E> l = last; //新建一个节点 final Node<E> newNode = new Node<>(l, e, null); last = newNode; //如果l是空,那么说明该链表是空的,就把新建的节点作为第一个节点 if (l == null) first = newNode; else l.next = newNode; //增加容量 size++; //增加程序计数器,这个值和快速遍历有关(在迭代器中调用过),这里不需要管。 modCount++; }
add()的方法很简单,主要围绕着linkBefore(E e, Node<E> succ)和linkLast(E e)两个方法,无非就是判断是应该在末尾增加节点呢?还是应该在某个节点之前增加节点。
/** * Appends all of the elements in the specified collection to the end of * this list, in the order that they are returned by the specified * collection's iterator. The behavior of this operation is undefined if * the specified collection is modified while the operation is in * progress. (Note that this will occur if the specified collection is * this list, and it's nonempty.) * * @param c collection containing elements to be added to this list * @return {@code true} if this list changed as a result of the call * @throws NullPointerException if the specified collection is null */ publicbooleanaddAll(Collection<? extends E> c){ return addAll(size, c); }
/** * Inserts all of the elements in the specified collection into this * list, starting at the specified position. Shifts the element * currently at that position (if any) and any subsequent elements to * the right (increases their indices). The new elements will appear * in the list in the order that they are returned by the * specified collection's iterator. * *将指定集合中的所有元素插入此列表,从指定位置开始。 *将当前在该位置的元素(如果有)和任何后续元素向右移动(增加它们的索引)。 *新元素将按照指定集合的迭代器返回的顺序出现在列表中。 * * @param index index at which to insert the first element * from the specified collection 从指定集合插入第一个元素的索引 * @param c collection containing elements to be added to this list 包含要添加到此列表的元素的集合 * @return {@code true} if this list changed as a result of the call 如果此列表因调用而更改 * @throws IndexOutOfBoundsException {@inheritDoc} * @throws NullPointerException if the specified collection is null */ publicbooleanaddAll(int index, Collection<? extends E> c){ //判断是否在链表范围内 checkPositionIndex(index);
//调用集合框架的接口把集合返回为数组 Object[] a = c.toArray(); int numNew = a.length; //如果这个集合为空就返回false if (numNew == 0) returnfalse;
//定义两个node类型的 Node<E> pred, succ; //如果index==size,说明是在尾部插入一个数组,那么第一个元素的前置位置等于尾节点 if (index == size) { succ = null; pred = last; //否则,就找到索引的那个节点,把该节点的节点设置为后置节点 } else { succ = node(index); pred = succ.prev; }
//遍历数据,插入数据 for (Object o : a) { @SuppressWarnings("unchecked") E e = (E) o; //新建节点 Node<E> newNode = new Node<>(pred, e, null); //如果前置节点是空的话,说明插入位置是在表头 if (pred == null) first = newNode; //否则就把该节点的后置节点设置为新建的节点 else pred.next = newNode; //设置pred为当前被遍历的节点,以便后面遍历的节点能连上 pred = newNode; }
//如果插入位置在尾部,则重置尾部节点的位置 if (succ == null) { last = pred; //就把之前的链表连接起来 } else { pred.next = succ; succ.prev = pred; }
/** * Inserts the specified element at the beginning of this list. * 在此列表的开头插入指定的元素。 * @param e the element to add */ publicvoidaddFirst(E e){ linkFirst(e); }
/** * Links e as first element. * 链接 e 作为第一个元素。 */ privatevoidlinkFirst(E e){ //获得首位节点 final Node<E> f = first; //新建一个节点,该节点没有前置节点 final Node<E> newNode = new Node<>(null, e, f); first = newNode; //如果首位节点为空,说明链表为空,则把尾节点也指向这个节点 if (f == null) last = newNode; //把曾经的首位节点的前置节点链接到新建的节点 else f.prev = newNode; size++; modCount++; }
/** * Appends the specified element to the end of this list. * *将指定的元素附加到此列表的末尾。 * <p>This method is equivalent to {@link #add}. * * @param e the element to add */ publicvoidaddLast(E e){ linkLast(e); }
/** * Links e as last element. * 链接 e 作为最后一个元素。该方法也是类似的道理 */ voidlinkLast(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++; }
/** * Returns the index of the first occurrence of the specified element * in this list, or -1 if this list does not contain the element. * More formally, returns the lowest index {@code i} such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, * or -1 if there is no such index. * * 返回此列表中指定元素第一次出现的索引,如果此列表不包含该元素,则返回 -1。更正式地说,返回最低索引 *{@code i} 使得 <tt>(o==null  ;? get(i)==null : o.equals(get(i)))</tt>,如果没有这样的索引,则为 -1。 * @param o element to search for * @return the index of the first occurrence of the specified element in * this list, or -1 if this list does not contain the element */ publicintindexOf(Object o){ int index = 0; if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) return index; index++; } } return -1; }
/** * Returns the index of the last occurrence of the specified element * in this list, or -1 if this list does not contain the element. * More formally, returns the highest index {@code i} such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, * or -1 if there is no such index. * * @param o element to search for * @return the index of the last occurrence of the specified element in * this list, or -1 if this list does not contain the element */ publicintlastIndexOf(Object o){ int index = size; if (o == null) { for (Node<E> x = last; x != null; x = x.prev) { index--; if (x.item == null) return index; } } else { for (Node<E> x = last; x != null; x = x.prev) { index--; if (o.equals(x.item)) return index; } } return -1; }
/** * Removes the first occurrence of the specified element from this list, * if it is present. If this list does not contain the element, it is * unchanged. More formally, removes the element with the lowest index * {@code i} such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> * (if such an element exists). Returns {@code true} if this list * contained the specified element (or equivalently, if this list * changed as a result of the call). * * 翻译了一大堆,实际上就是遍历以后找到对应的节点,然后把这个节点赋值为空,修改前后节点的链接 * @param o element to be removed from this list, if present * @return {@code true} if this list contained the specified element */ 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; }
/** * Removes the element at the specified position in this list. Shifts any * subsequent elements to the left (subtracts one from their indices). * Returns the element that was removed from the list. * * 删除此列表中指定位置的元素。 将任何后续元素向左移动(从它们的索引中减去一个)。返回从列表中删除的元素。 * @param index the index of the element to be removed * @return the element previously at the specified position * @throws IndexOutOfBoundsException {@inheritDoc} */ public E remove(int index){ checkElementIndex(index); return unlink(node(index)); }
/** * Unlinks non-null node x. * 取消链接非空节点 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; } else { prev.next = next; x.prev = null; }
if (next == null) { last = prev; } else { next.prev = prev; x.next = null; }