//遍历集合c中的元素,并依次链接到pred的next节点 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 = newNode; }
//集合c中的元素遍历完后,设置链表的尾节点。此时pred是集合c中最后一个元素组成的节点 if (succ == null) { last = pred; } else { pred.next = succ; succ.prev = pred; }
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; } }
//在节点succ之前添加元素 voidlinkBefore(E e, Node<E> succ){ // assert succ != null; //获取节点succ的父结点 final Node<E> pred = succ.prev; //组装节点 final Node<E> newNode = new Node<>(pred, e, succ);
//设置节点succ的父节点为新节点 succ.prev = newNode;
//如果succ的父结点为null,说明是在链表的头部插入了新元素 if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++; }
//与unlinkLast()方法类似, 但是移除的是非空的头节点l private E unlinkFirst(Node<E> f){ // assert f == first && f != null; final E element = f.item; final Node<E> next = f.next; f.item = null; f.next = null; // help GC first = next; if (next == null) last = null; else next.prev = null; size--; modCount++; return element; }
//移除非空的尾节点l private E unlinkLast(Node<E> l){ // assert l == last && l != null; //获取节点l内的item值 final E element = l.item; //获取节点l的父节点prev final Node<E> prev = l.prev; //将节点l的域值,父节点置空,用于GC l.item = null; l.prev = null; // help GC
//重新赋值last节点 last = prev;
//如果父节点prev为空,说明删除的为链表中的最后一个元素;否则将父节点prev的next置空 if (prev == null) first = null; else prev.next = null;
//从链表中删除元素o publicbooleanremove(Object o){ //整体逻辑:遍历链表中的所有节点,如果找到节点的item值等于元素o,则调用unlink()方法将该节点移除 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; }
//移除非空节点 E unlink(Node<E> x){ // assert x != null; //获取节点x的item值,父节点prev及子节点next final E element = x.item; final Node<E> next = x.next; final Node<E> prev = x.prev;
//如果节点x的父节点为null。则说明节点x为头结点,则直接将first指向节点x的next节点 if (prev == null) { first = next; } else { //如果节点x的父节点不为null。则需要重新设置节点x的父节点prev的子节点next指向节点x的子节点;节点x的父节点prev置为空。即:将节点x与其父节点断开连接 prev.next = next; x.prev = null; }
//如果节点x的子节点为null。则说明节点x为尾结点,则直接将last指向节点x的prev节点 if (next == null) { last = prev; } else { //如果节点x的子节点不为null。则需要重新设置节点x的子节点next的父节点prev指向节点x的父节点;节点x的子节点next置为空。即:将节点x与其子节点断开连接 next.prev = prev; x.next = null; }
//remove(Object o)方法的简化版 public E remove(int index){ checkElementIndex(index); return unlink(node(index)); }
找到index处的节点,然后删除。
remove源码
1 2 3 4
//从链表的头部移除元素,与poll()方法不同之处在于:链表中没有元素时,poll()方法返回null,而remove()抛异常 public E remove(){ return removeFirst(); }
从链表的头节点删除节点。
clear源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicvoidclear(){ // Clearing all of the links between nodes is "unnecessary", but: // - helps a generational GC if the discarded nodes inhabit // more than one generation // - is sure to free memory even if there is a reachable Iterator
//从头结点开始遍历,依次将节点的item,prev和next值都置为空 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++; }
该方法的功能:清空列表中的所有元素。
poll源码
1 2 3 4 5
//从链表的头部移除元素 public E poll(){ final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); }
publicbooleanremoveLastOccurrence(Object o){ if (o == null) { for (Node<E> x = last; x != null; x = x.prev) { if (x.item == null) { unlink(x); returntrue; } } } else { for (Node<E> x = last; x != null; x = x.prev) { if (o.equals(x.item)) { unlink(x); returntrue; } } } returnfalse; }
//获取元素o在链表中的索引 publicintindexOf(Object o){ int index = 0; //因为链表运行允许存储null元素,所以在查找元素o时,会分两种情况。 // 但整理逻辑为:遍历链表中的节点,如果节点内的item值等于元素o,则返回对应的索引;否则,返回-1 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; }
功能:获取元素o在链表中的索引。
peek源码
1 2 3 4 5
//获取链表中的头元素,但不从链表中去除 public E peek(){ final Node<E> f = first; return (f == null) ? null : f.item; }
element源码
1 2 3 4
//检索但不删除此列表的头(第一个元素)。 public E element(){ return getFirst(); }
peekFirst源码
1 2 3 4 5
//获取头元素 public E peekFirst(){ final Node<E> f = first; return (f == null) ? null : f.item; }
更新元素
set源码
1 2 3 4 5 6 7 8
//先通过node()方法获取index位置上的节点,然后将其item值设置为新值 public E set(int index, E element){ checkElementIndex(index); Node<E> x = node(index); E oldVal = x.item; x.item = element; return oldVal; }