首先上一幅框架图:
package java.util; public abstract class AbstractSequentialList<E> extends AbstractList<E> { protected AbstractSequentialList() {//只有一个构造器 } public E get(int index) {//获取指定位置的值 try { return listIterator(index).next();//通过迭代器的方式 } catch (NoSuchElementException exc) {//找不到就抛出异常 throw new IndexOutOfBoundsException("Index: "+index);//这是个Runtime异常 } } public E set(int index, E element) { try { ListIterator<E> e = listIterator(index);//同样调用的listiterator E oldVal = e.next();//记录 e.set(element); return oldVal;//返回 } catch (NoSuchElementException exc) { throw new IndexOutOfBoundsException("Index: "+index); } } public void add(int index, E element) { try { listIterator(index).add(element); } catch (NoSuchElementException exc) { throw new IndexOutOfBoundsException("Index: "+index); } } public E remove(int index) { try { ListIterator<E> e = listIterator(index); E outCast = e.next(); e.remove(); return outCast; } catch (NoSuchElementException exc) { throw new IndexOutOfBoundsException("Index: "+index); } } // Bulk Operations public boolean addAll(int index, Collection<? extends E> c) { try { boolean modified = false; ListIterator<E> e1 = listIterator(index); Iterator<? extends E> e2 = c.iterator(); while (e2.hasNext()) { e1.add(e2.next()); modified = true; } return modified; } catch (NoSuchElementException exc) { throw new IndexOutOfBoundsException("Index: "+index); } } // Iterators public Iterator<E> iterator() { return listIterator(); } public abstract ListIterator<E> listIterator(int index);//参数为索引位置。表示从哪开始遍历 }
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable
transient int size = 0;//集合大小(结点个数) transient Node<E> first;//头指针 transient Node<E> last;//尾指针前面说过,linkedList是通过双向链表实现,故而不需要有扩容的方法,因为结点是动态申请的。而这个结点的类型即为Node。下面看Node源码:
private static class Node<E> { E item;//数据 Node<E> next;//后继指针 Node<E> prev;//前驱指针 Node(Node<E> prev, E element, Node<E> next) { this.item = element; this.next = next; this.prev = prev; } }
public LinkedList() {} public LinkedList(Collection<? extends E> c) { this(); addAll(c); }
private void linkFirst(E e) {//插到头部 final Node<E> f = first; //创建一个新结点,前驱为空,后继为f(也就是当前的头结点) final Node<E> newNode = new Node<>(null, e, f);//注意这种泛型的写法也是可以的 first = newNode;//头指针指向新结点 if (f == null)//链表为空时 last = newNode;//尾指针指向新结点 else//否则 f.prev = newNode;//让f的前驱指向新结点 size++; modCount++; } void linkLast(E e) {//插到尾部 final Node<E> l = last;//临时变量记录尾结点 final Node<E> newNode = new Node<>(l, e, null);//创建新结点,前驱为l last = newNode;//更新尾指针 if (l == null)//如果链表为空 first = newNode; else l.next = newNode; size++; modCount++;//用于快速失败机制 } void linkBefore(E e, Node<E> succ) {//将e插入succ之前 // assert succ != null;//调用者需要保证succ不为空 final Node<E> pred = succ.prev;//记录succ的前驱 final Node<E> newNode = new Node<>(pred, e, succ);//新结点的前驱指向succ的前驱,新结点的后继指向succ succ.prev = newNode;//succ的前驱指向新结点 if (pred == null)//succ为头结点 first = newNode;//更改头指针 else pred.next = newNode;//否则succ的前驱的后继指向新结点 size++; modCount++; } private E unlinkFirst(Node<E> f) {//干掉头结点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; } private E unlinkLast(Node<E> l) {//干掉尾结点l // assert l == last && l != null; final E element = l.item; final Node<E> prev = l.prev; l.item = null; l.prev = null; // help GC last = prev; if (prev == null) first = null; else prev.next = null; size--; modCount++; return element; } E unlink(Node<E> x) {//干掉一个普通结点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; } x.item = null; size--; modCount++; return element; }有了这些基本函数之后,实现其他操作就方便了。比如这些:
public void addFirst(E e) { linkFirst(e); } public void addLast(E e) { linkLast(e); } public boolean add(E e) { linkLast(e); return true; }再看这个remove方法:
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 void clear() { 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++; }下面的函数封装了索引链表位置的操作:
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; } }
public ListIterator<E> listIterator(int index) { checkPositionIndex(index); return new ListItr(index); }
private class ListItr implements ListIterator<E> private Node<E> next; ListItr(int index) { // assert isPositionIndex(index); next = (index == size) ? null : node(index); nextIndex = index; }
public Iterator<E> descendingIterator() { return new DescendingIterator(); }
【源码】LinkedList源码剖析,布布扣,bubuko.com
原文:http://blog.csdn.net/chdjj/article/details/38447015