public void addFirst(E e) { //添加元素到列表頭 linkFirst(e); } public void addLast(E e) { //添加元素到列表尾 linkLast(e); } public boolean add(E e) { //添加元素到列表頭,會返回添加是否成功 linkLast(e); return true; } public void add(int index, E element) { //添加元素到index位置 checkPositionIndex(index); //檢查index是否越界 if (index == size) //若是index等於鏈表長度則插入到鏈表尾 linkLast(element); else //不然插入到鏈表頭 linkBefore(element, node(index)); } public boolean addAll(int index, Collection<? extends E> c) { //在index位置插入集合c的所有元素 checkPositionIndex(index); //檢查index是否越界 Object[] a = c.toArray(); int numNew = a.length; if (numNew == 0) return false; Node<E> pred, succ; if (index == size) { //若是index爲鏈表長度,則插入位置爲鏈表尾 succ = null; pred = last; } else { //不然將記錄index位置的前一個元素,爲以後鏈接鏈表準備 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 = newNode; } //插入完畢,將原集合index以後的部分鏈接到鏈表尾 if (succ == null) { last = pred; } else { pred.next = succ; succ.prev = pred; } size += numNew; modCount++; return true; } public E set(int index, E element) { //修改index位置元素爲element checkElementIndex(index); Node<E> x = node(index); E oldVal = x.item; x.item = element; return oldVal; }
public E getFirst() { //獲取鏈表頭元素 final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return f.item; } public E getLast() { //獲取鏈表尾元素 final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return l.item; } public E get(int index) { //獲取鏈表index位置的元素 checkElementIndex(index); return node(index).item; } Node<E> node(int index) { //獲取特定位置元素的實現方法 // assert isElementIndex(index); if (index < (size >> 1)) { //若是index小於鏈表長度的一半則從前向後遍歷前一半鏈表 Node<E> x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { //若是index大於等於鏈表長度的一半則從後向前遍歷後一半鏈表 Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }
public E removeFirst() { //刪除鏈表頭元素,相似pop final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(f); } public E removeLast() { //刪除鏈表尾元素 final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return 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; // 這兩個置爲空是爲了方便GC first = next; //第二個元素提置第一位,將原第一位元素踢出鏈表 if (next == null) //若是沒有next說明刪除鏈表頭元素後鏈表爲空,將last置爲空 last = null; else //next不爲空說明刪除後鏈表還存在元素,將現第一位元素的prev置爲空(鏈表頭元素prev爲空) next.prev = null; size--; modCount++; return element; } private E unlinkLast(Node<E> l) { //刪除鏈表尾元素的實現方法 // assert l == last && l != null; final E element = l.item; final Node<E> prev = l.prev; l.item = null; l.prev = null; // 這兩個置爲空是爲了方便GC last = prev; //將倒數第二個元素置爲鏈表尾元素,將原鏈表尾元素踢出鏈表 if (prev == null) //若是prev爲空則說明刪除鏈表尾元素後鏈表爲空,沒有已保存的元素,此時first爲空 first = null; else //若是prev不爲空,說明刪除以後鏈表還存在元素,此時需將鏈表尾元素的next元素置爲空(鏈表尾元素next爲空) prev.next = null; size--; modCount++; return element; } public boolean remove(Object o) { //刪除指定元素實現方法 if (o == null) { //遍歷空對象 for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) { //若是有符合的元素則刪除 unlink(x); return true; } } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) { //若是有符合的元素則刪除 unlink(x); return true; } } } return false; } public E remove(int index) { //刪除指定位置的元素 checkElementIndex(index); return unlink(node(index)); } public E remove() { //刪除鏈表頭元素 return removeFirst(); } E unlink(Node<E> x) { 刪除指定元素實現方法 // assert x != null; final E element = x.item; //參數元素的value,用於返回值 final Node<E> next = x.next; //參數元素的下一位元素 final Node<E> prev = x.prev; //參數元素的上一位元素 if (prev == null) { //prev爲空說明參數元素是鏈表頭元素,將下一位元素提置鏈表頭 first = next; } else { //將上一位元素和下一位元素相連 prev.next = next; x.prev = null; } if (next == null) { //若是next爲空說明參數元素是鏈表尾元素,將上一位元素置爲鏈表尾 last = prev; } else { //將上一位元素和下一位元素相連 next.prev = prev; x.next = null; } x.item = null; size--; modCount++; return element; } public boolean removeLastOccurrence(Object o) { //找到元素出現的最後位置並刪除 if (o == null) { for (Node<E> x = last; x != null; x = x.prev) { if (x.item == null) { unlink(x); return true; } } } else { for (Node<E> x = last; x != null; x = x.prev) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; }
public E next() { //獲取下一個元素 checkForComodification(); if (!hasNext()) throw new NoSuchElementException(); lastReturned = next; next = next.next; nextIndex++; return lastReturned.item; } public E previous() { //獲取上一個元素,須要注意的是當調用next而後調用previous時會返回同一個元素,由於調用next的時候nextIndex被加一,而previous中nextIndex減一,因此會取到同一個元素 checkForComodification(); if (!hasPrevious()) throw new NoSuchElementException(); lastReturned = next = (next == null) ? last : next.prev; nextIndex--; return lastReturned.item; } public void remove() { //實現了fail-fast的刪除 checkForComodification(); if (lastReturned == null) throw new IllegalStateException(); Node<E> lastNext = lastReturned.next; unlink(lastReturned); if (next == lastReturned) next = lastNext; else nextIndex--; lastReturned = null; expectedModCount++; } public void set(E e) { //實現了fail-fast的修改 if (lastReturned == null) throw new IllegalStateException(); checkForComodification(); lastReturned.item = e; } public void add(E e) { //實現了fail-fast的新增 checkForComodification(); lastReturned = null; if (next == null) linkLast(e); else linkBefore(e, next); nextIndex++; expectedModCount++; }