JAVA集合容器--LinkedList



1、LinkedList简介

(1)LinkedList是基于双向循环链表(从源码中可以很容易看出)实现的,除了可以当做链表来操作外,它还可以当做栈、队列和双端队列来使用。

(2)LinkedList同样是非线程安全的,只在单线程下适合使用。

(3)LinkedList实现了Serializable接口,因此它支持序列化,能够通过序列化传输,实现了Cloneable接口,能被克隆

LinkedList的继承关系:

  1. public class LinkedList<E>  
  2.     extends AbstractSequentialList<E>  
  3.     implements List<E>, Deque<E>, Cloneable, java.io.Serializable  
public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
LinkedList继承自AbstractSequenceList、实现了List及Deque接口。Deque接口是Queue的子接口,它代表一个双向队列.因此LinedList的功能十分强大,兼具双向队列,栈和List集合的用法。其实AbstractSequenceList已经实现了List接口,这里标注出List只是更加清晰而已。AbstractSequenceList提供了List接口骨干性的实现以减少实现List接口的复杂度。

2、LinkedList成员变量

  1. // 链表的表头,表头不包含任何数据。Entry是个链表类数据结构。      
  2.  private transient Entry<E> header = new Entry<E>(nullnullnull);      
  3.   
  4.  // LinkedList中元素个数      
  5.  private transient int size = 0;      
  6.   
  7.  // 双向链表的节点所对应的数据结构。      
  8.  // 包含3部分:上一节点,下一节点,当前节点值。      
  9.  private static class Entry<E> {      
  10.      // 当前节点所包含的值      
  11.      E element;      
  12.      // 下一个节点      
  13.      Entry<E> next;      
  14.      // 上一个节点      
  15.      Entry<E> previous;      
  16.   
  17.      /**    
  18.       * 链表节点的构造函数。    
  19.       * 参数说明:    
  20.       *   element  —— 节点所包含的数据    
  21.       *   next      —— 下一个节点    
  22.       *   previous —— 上一个节点    
  23.       */     
  24.      Entry(E element, Entry<E> next, Entry<E> previous) {      
  25.          this.element = element;      
  26.          this.next = next;      
  27.          this.previous = previous;      
  28.      }      
  29.  }   
   // 链表的表头,表头不包含任何数据。Entry是个链表类数据结构。    
    private transient Entry<E> header = new Entry<E>(null, null, null);    
   
    // LinkedList中元素个数    
    private transient int size = 0;    

    // 双向链表的节点所对应的数据结构。    
    // 包含3部分:上一节点,下一节点,当前节点值。    
    private static class Entry<E> {    
        // 当前节点所包含的值    
        E element;    
        // 下一个节点    
        Entry<E> next;    
        // 上一个节点    
        Entry<E> previous;    
   
        /**   
         * 链表节点的构造函数。   
         * 参数说明:   
         *   element  —— 节点所包含的数据   
         *   next      —— 下一个节点   
         *   previous —— 上一个节点   
         */   
        Entry(E element, Entry<E> next, Entry<E> previous) {    
            this.element = element;    
            this.next = next;    
            this.previous = previous;    
        }    
    } 

3、LinkedList构造函数

  1. // 默认构造函数:创建一个空的链表      
  2.  public LinkedList() {      
  3.      header.next = header.previous = header;      
  4.  }      
  5.   
  6.  // 包含“集合”的构造函数:创建一个包含“集合”的LinkedList      
  7.  public LinkedList(Collection<? extends E> c) {      
  8.      this();      
  9.      addAll(c);      
  10.  }   
   // 默认构造函数:创建一个空的链表    
    public LinkedList() {    
        header.next = header.previous = header;    
    }    
   
    // 包含“集合”的构造函数:创建一个包含“集合”的LinkedList    
    public LinkedList(Collection<? extends E> c) {    
        this();    
        addAll(c);    
    } 
 LinkedList提供了两个构造方法。第一个构造方法不接受参数,只是将header节点的前一节点和后一节点都设置为自身(注意,这个是一个双向循环链表,如果不是循环链表,空链表的情况应该是header节点的前一节点和后一节点均为null),这样整个链表其实就只有header一个节点,用于表示一个空的链表。第二个构造方法接收一个Collection参数c,调用第一个构造方法构造一个空的链表,之后通过addAll将c中的元素全部添加到链表中。

4、LinkedList常用方法

4.1 add方法

  1. // 将元素(E)添加到LinkedList中      
  2. public boolean add(E e) {      
  3.     // 将节点(节点数据是e)添加到表头(header)之前。      
  4.     // 即,将节点添加到双向链表的末端。      
  5.     addBefore(e, header);      
  6.     return true;      
  7. }   
  8.   
  9. // 在index前添加节点,且节点的值为element      
  10. public void add(int index, E element) {      
  11.     addBefore(element, (index==size ? header : entry(index)));      
  12. }     
  13.   
  14.   
  15. // 将节点(节点数据是e)添加到entry节点之前。      
  16. private Entry<E> addBefore(E e, Entry<E> entry) {      
  17.     // 新建节点newEntry,将newEntry插入到节点e之前;并且设置newEntry的数据是e      
  18.     Entry<E> newEntry = new Entry<E>(e, entry, entry.previous);      
  19.     newEntry.previous.next = newEntry;      
  20.     newEntry.next.previous = newEntry;      
  21.     // 修改LinkedList大小      
  22.     size++;      
  23.     // 修改LinkedList的修改统计数:用来实现fail-fast机制。      
  24.     modCount++;      
  25.     return newEntry;      
  26. }    
  27.   
  28. // 将“集合(c)”添加到LinkedList中。      
  29. // 实际上,是从双向链表的末尾开始,将“集合(c)”添加到双向链表中。      
  30. public boolean addAll(Collection<? extends E> c) {      
  31.     return addAll(size, c);      
  32. }      
  33.   
  34. // 从双向链表的index开始,将“集合(c)”添加到双向链表中。      
  35. public boolean addAll(int index, Collection<? extends E> c) {      
  36.     if (index < 0 || index > size)      
  37.         throw new IndexOutOfBoundsException("Index: "+index+      
  38.                                             ", Size: "+size);      
  39.     Object[] a = c.toArray();      
  40.     // 获取集合的长度      
  41.     int numNew = a.length;      
  42.     if (numNew==0)      
  43.         return false;      
  44.     modCount++;      
  45.   
  46.     // 设置“当前要插入节点的后一个节点”      
  47.     Entry<E> successor = (index==size ? header : entry(index));      
  48.     // 设置“当前要插入节点的前一个节点”      
  49.     Entry<E> predecessor = successor.previous;      
  50.     // 将集合(c)全部插入双向链表中      
  51.     for (int i=0; i<numNew; i++) {      
  52.         Entry<E> e = new Entry<E>((E)a[i], successor, predecessor);      
  53.         predecessor.next = e;      
  54.         predecessor = e;      
  55.     }      
  56.     successor.previous = predecessor;      
  57.   
  58.     // 调整LinkedList的实际大小      
  59.     size += numNew;      
  60.     return true;      
  61. }     
    // 将元素(E)添加到LinkedList中    
    public boolean add(E e) {    
        // 将节点(节点数据是e)添加到表头(header)之前。    
        // 即,将节点添加到双向链表的末端。    
        addBefore(e, header);    
        return true;    
    } 

    // 在index前添加节点,且节点的值为element    
    public void add(int index, E element) {    
        addBefore(element, (index==size ? header : entry(index)));    
    }   


    // 将节点(节点数据是e)添加到entry节点之前。    
    private Entry<E> addBefore(E e, Entry<E> entry) {    
        // 新建节点newEntry,将newEntry插入到节点e之前;并且设置newEntry的数据是e    
        Entry<E> newEntry = new Entry<E>(e, entry, entry.previous);    
        newEntry.previous.next = newEntry;    
        newEntry.next.previous = newEntry;    
        // 修改LinkedList大小    
        size++;    
        // 修改LinkedList的修改统计数:用来实现fail-fast机制。    
        modCount++;    
        return newEntry;    
    }  

    // 将“集合(c)”添加到LinkedList中。    
    // 实际上,是从双向链表的末尾开始,将“集合(c)”添加到双向链表中。    
    public boolean addAll(Collection<? extends E> c) {    
        return addAll(size, c);    
    }    
   
    // 从双向链表的index开始,将“集合(c)”添加到双向链表中。    
    public boolean addAll(int index, Collection<? extends E> c) {    
        if (index < 0 || index > size)    
            throw new IndexOutOfBoundsException("Index: "+index+    
                                                ", Size: "+size);    
        Object[] a = c.toArray();    
        // 获取集合的长度    
        int numNew = a.length;    
        if (numNew==0)    
            return false;    
        modCount++;    
   
        // 设置“当前要插入节点的后一个节点”    
        Entry<E> successor = (index==size ? header : entry(index));    
        // 设置“当前要插入节点的前一个节点”    
        Entry<E> predecessor = successor.previous;    
        // 将集合(c)全部插入双向链表中    
        for (int i=0; i<numNew; i++) {    
            Entry<E> e = new Entry<E>((E)a[i], successor, predecessor);    
            predecessor.next = e;    
            predecessor = e;    
        }    
        successor.previous = predecessor;    
   
        // 调整LinkedList的实际大小    
        size += numNew;    
        return true;    
    }   
addBefore(E e,Entry<E> entry)实现在entry之前插入由e构造的新节点。而add(E e)实现在header节点之前插入由e构造的新节点 

4.2 删除方法

  1. // 从LinkedList中删除元素(o)      
  2. // 从链表开始查找,如存在元素(o)则删除该元素并返回true;      
  3. // 否则,返回false。      
  4. public boolean remove(Object o) {      
  5.     if (o==null) {      
  6.         // 若o为null的删除情况      
  7.         for (Entry<E> e = header.next; e != header; e = e.next) {      
  8.             if (e.element==null) {      
  9.                 remove(e);      
  10.                 return true;      
  11.             }      
  12.         }      
  13.     } else {      
  14.         // 若o不为null的删除情况      
  15.         for (Entry<E> e = header.next; e != header; e = e.next) {      
  16.             if (o.equals(e.element)) {      
  17.                 remove(e);      
  18.                 return true;      
  19.             }      
  20.         }      
  21.     }      
  22.     return false;      
  23. }    
  24.   
  25.   
  26. // 删除index位置的节点      
  27. public E remove(int index) {      
  28.     return remove(entry(index));      
  29. }     
  30.   
  31.   
  32. // 将节点从链表中删除      
  33. private E remove(Entry<E> e) {      
  34.     if (e == header)      
  35.         throw new NoSuchElementException();      
  36.   
  37.     E result = e.element;      
  38.     e.previous.next = e.next;      
  39.     e.next.previous = e.previous;      
  40.     e.next = e.previous = null;      
  41.     e.element = null;      
  42.     size--;      
  43.     modCount++;      
  44.     return result;      
  45. }    
    // 从LinkedList中删除元素(o)    
    // 从链表开始查找,如存在元素(o)则删除该元素并返回true;    
    // 否则,返回false。    
    public boolean remove(Object o) {    
        if (o==null) {    
            // 若o为null的删除情况    
            for (Entry<E> e = header.next; e != header; e = e.next) {    
                if (e.element==null) {    
                    remove(e);    
                    return true;    
                }    
            }    
        } else {    
            // 若o不为null的删除情况    
            for (Entry<E> e = header.next; e != header; e = e.next) {    
                if (o.equals(e.element)) {    
                    remove(e);    
                    return true;    
                }    
            }    
        }    
        return false;    
    }  


    // 删除index位置的节点    
    public E remove(int index) {    
        return remove(entry(index));    
    }   
   

    // 将节点从链表中删除    
    private E remove(Entry<E> e) {    
        if (e == header)    
            throw new NoSuchElementException();    
   
        E result = e.element;    
        e.previous.next = e.next;    
        e.next.previous = e.previous;    
        e.next = e.previous = null;    
        e.element = null;    
        size--;    
        modCount++;    
        return result;    
    }  
4.3  查找方法
  1. // 返回LinkedList指定位置的元素      
  2. public E get(int index) {      
  3.     return entry(index).element;      
  4. }      
  5.   
  6. // 获取双向链表中指定位置的节点      
  7. private Entry<E> entry(int index) {      
  8.     if (index < 0 || index >= size)      
  9.         throw new IndexOutOfBoundsException("Index: "+index+      
  10.                                             ", Size: "+size);      
  11.     Entry<E> e = header;      
  12.     // 获取index处的节点。      
  13.     // 若index < 双向链表长度的1/2,则从前先后查找;      
  14.     // 否则,从后向前查找。      
  15.     if (index < (size >> 1)) {      
  16.         for (int i = 0; i <= index; i++)      
  17.             e = e.next;      
  18.     } else {      
  19.         for (int i = size; i > index; i--)      
  20.             e = e.previous;      
  21.     }      
  22.     return e;      
  23. }     
  24.   
  25. // 从前向后查找,返回“值为对象(o)的节点对应的索引”      
  26. // 不存在就返回-1      
  27. public int indexOf(Object o) {      
  28.     int index = 0;      
  29.     if (o==null) {      
  30.         for (Entry e = header.next; e != header; e = e.next) {      
  31.             if (e.element==null)      
  32.                 return index;      
  33.             index++;      
  34.         }      
  35.     } else {      
  36.         for (Entry e = header.next; e != header; e = e.next) {      
  37.             if (o.equals(e.element))      
  38.                 return index;      
  39.             index++;      
  40.         }      
  41.     }      
  42.     return -1;      
  43. }      
  44.   
  45. // 从后向前查找,返回“值为对象(o)的节点对应的索引”      
  46. // 不存在就返回-1      
  47. public int lastIndexOf(Object o) {      
  48.     int index = size;      
  49.     if (o==null) {      
  50.         for (Entry e = header.previous; e != header; e = e.previous) {      
  51.             index--;      
  52.             if (e.element==null)      
  53.                 return index;      
  54.         }      
  55.     } else {      
  56.         for (Entry e = header.previous; e != header; e = e.previous) {      
  57.             index--;      
  58.             if (o.equals(e.element))      
  59.                 return index;      
  60.         }      
  61.     }      
  62.     return -1;      
  63. }      
    // 返回LinkedList指定位置的元素    
    public E get(int index) {    
        return entry(index).element;    
    }    

    // 获取双向链表中指定位置的节点    
    private Entry<E> entry(int index) {    
        if (index < 0 || index >= size)    
            throw new IndexOutOfBoundsException("Index: "+index+    
                                                ", Size: "+size);    
        Entry<E> e = header;    
        // 获取index处的节点。    
        // 若index < 双向链表长度的1/2,则从前先后查找;    
        // 否则,从后向前查找。    
        if (index < (size >> 1)) {    
            for (int i = 0; i <= index; i++)    
                e = e.next;    
        } else {    
            for (int i = size; i > index; i--)    
                e = e.previous;    
        }    
        return e;    
    }   

    // 从前向后查找,返回“值为对象(o)的节点对应的索引”    
    // 不存在就返回-1    
    public int indexOf(Object o) {    
        int index = 0;    
        if (o==null) {    
            for (Entry e = header.next; e != header; e = e.next) {    
                if (e.element==null)    
                    return index;    
                index++;    
            }    
        } else {    
            for (Entry e = header.next; e != header; e = e.next) {    
                if (o.equals(e.element))    
                    return index;    
                index++;    
            }    
        }    
        return -1;    
    }    
   
    // 从后向前查找,返回“值为对象(o)的节点对应的索引”    
    // 不存在就返回-1    
    public int lastIndexOf(Object o) {    
        int index = size;    
        if (o==null) {    
            for (Entry e = header.previous; e != header; e = e.previous) {    
                index--;    
                if (e.element==null)    
                    return index;    
            }    
        } else {    
            for (Entry e = header.previous; e != header; e = e.previous) {    
                index--;    
                if (o.equals(e.element))    
                    return index;    
            }    
        }    
        return -1;    
    }    
4.4 修改方法

  1. // 设置index位置对应的节点的值为element      
  2. public E set(int index, E element) {      
  3.     Entry<E> e = entry(index);      
  4.     E oldVal = e.element;      
  5.     e.element = element;      
  6.     return oldVal;      
  7. }   
    // 设置index位置对应的节点的值为element    
    public E set(int index, E element) {    
        Entry<E> e = entry(index);    
        E oldVal = e.element;    
        e.element = element;    
        return oldVal;    
    } 
4.5 clone()

  1. // 克隆函数。返回LinkedList的克隆对象。      
  2. public Object clone() {      
  3.     LinkedList<E> clone = null;      
  4.     // 克隆一个LinkedList克隆对象      
  5.     try {      
  6.         clone = (LinkedList<E>) super.clone();      
  7.     } catch (CloneNotSupportedException e) {      
  8.         throw new InternalError();      
  9.     }      
  10.   
  11.     // 新建LinkedList表头节点      
  12.     clone.header = new Entry<E>(nullnullnull);      
  13.     clone.header.next = clone.header.previous = clone.header;      
  14.     clone.size = 0;      
  15.     clone.modCount = 0;      
  16.   
  17.     // 将链表中所有节点的数据都添加到克隆对象中      
  18.     for (Entry<E> e = header.next; e != header; e = e.next)      
  19.         clone.add(e.element);      
  20.   
  21.     return clone;      
  22. }    
    // 克隆函数。返回LinkedList的克隆对象。    
    public Object clone() {    
        LinkedList<E> clone = null;    
        // 克隆一个LinkedList克隆对象    
        try {    
            clone = (LinkedList<E>) super.clone();    
        } catch (CloneNotSupportedException e) {    
            throw new InternalError();    
        }    
   
        // 新建LinkedList表头节点    
        clone.header = new Entry<E>(null, null, null);    
        clone.header.next = clone.header.previous = clone.header;    
        clone.size = 0;    
        clone.modCount = 0;    
   
        // 将链表中所有节点的数据都添加到克隆对象中    
        for (Entry<E> e = header.next; e != header; e = e.next)    
            clone.add(e.element);    
   
        return clone;    
    }  
调用父类的clone()方法初始化对象链表clone,将clone构造成一个空的双向循环链表,之后将header的下一个节点开始将逐个节点添加到clone中。最后返回克隆的clone对象。
4.6 toArray()

  1. // 返回LinkedList的Object[]数组      
  2. public Object[] toArray() {      
  3. // 新建Object[]数组      
  4. Object[] result = new Object[size];      
  5.     int i = 0;      
  6.     // 将链表中所有节点的数据都添加到Object[]数组中      
  7.     for (Entry<E> e = header.next; e != header; e = e.next)      
  8.         result[i++] = e.element;      
  9. return result;      
  10. }      
  11.   
  12. // 返回LinkedList的模板数组。所谓模板数组,即可以将T设为任意的数据类型      
  13. public <T> T[] toArray(T[] a) {      
  14.     // 若数组a的大小 < LinkedList的元素个数(意味着数组a不能容纳LinkedList中全部元素)      
  15.     // 则新建一个T[]数组,T[]的大小为LinkedList大小,并将该T[]赋值给a。      
  16.     if (a.length < size)      
  17.         a = (T[])java.lang.reflect.Array.newInstance(      
  18.                             a.getClass().getComponentType(), size);      
  19.     // 将链表中所有节点的数据都添加到数组a中      
  20.     int i = 0;      
  21.     Object[] result = a;      
  22.     for (Entry<E> e = header.next; e != header; e = e.next)      
  23.         result[i++] = e.element;      
  24.   
  25.     if (a.length > size)      
  26.         a[size] = null;      
  27.   
  28.     return a;      
  29. }     
    // 返回LinkedList的Object[]数组    
    public Object[] toArray() {    
    // 新建Object[]数组    
    Object[] result = new Object[size];    
        int i = 0;    
        // 将链表中所有节点的数据都添加到Object[]数组中    
        for (Entry<E> e = header.next; e != header; e = e.next)    
            result[i++] = e.element;    
    return result;    
    }    
   
    // 返回LinkedList的模板数组。所谓模板数组,即可以将T设为任意的数据类型    
    public <T> T[] toArray(T[] a) {    
        // 若数组a的大小 < LinkedList的元素个数(意味着数组a不能容纳LinkedList中全部元素)    
        // 则新建一个T[]数组,T[]的大小为LinkedList大小,并将该T[]赋值给a。    
        if (a.length < size)    
            a = (T[])java.lang.reflect.Array.newInstance(    
                                a.getClass().getComponentType(), size);    
        // 将链表中所有节点的数据都添加到数组a中    
        int i = 0;    
        Object[] result = a;    
        for (Entry<E> e = header.next; e != header; e = e.next)    
            result[i++] = e.element;    
   
        if (a.length > size)    
            a[size] = null;    
   
        return a;    
    }   
toArray()  创建大小和LinkedList相等的数组result,遍历链表,将每个节点的元素element复制到数组中,返回数组。

toArray(T[] a) 先判断出入的数组a的大小是否足够,若大小不够则拓展。这里用到了反射的方法,重新实例化了一个大小为size的数组。之后将数组a赋值给数组result,遍历链表向result中添加的元素。最后判断数组a的长度是否大于size,若大于则将size位置的内容设置为null。返回a。

从代码中可以看出,数组a的length小于等于size时,a中所有元素被覆盖,被拓展来的空间存储的内容都是null;若数组a的length的length大于size,则0至size-1位置的内容被覆盖,size位置的元素被设置为null,size之后的元素不变。

5、LinkedList的Iterator

除了Entry,LinkedList还有一个内部类:ListItr。ListItr实现了ListIterator接口,可知它是一个迭代器,通过它可以遍历修改LinkedList。在LinkedList中提供了获取ListItr对象的方法:listIterator(int index)。

  1. public ListIterator<E> listIterator(int index) {  
  2.      return new ListItr(index);  
  3.  }  
public ListIterator<E> listIterator(int index) {
     return new ListItr(index);
 }
该方法只是简单的返回了一个ListItr对象。LinkedList中还有通过集成获得的listIterator()方法,该方法只是调用了listIterator(int index)并且传入0。
下面详细分析ListItr。

  1. // List迭代器      
  2. private class ListItr implements ListIterator<E> {      
  3.     // 上一次返回的节点      
  4.     private Entry<E> lastReturned = header;      
  5.     // 下一个节点      
  6.     private Entry<E> next;      
  7.     // 下一个节点对应的索引值      
  8.     private int nextIndex;      
  9.     // 期望的改变计数。用来实现fail-fast机制。      
  10.     private int expectedModCount = modCount;      
  11.   
  12.     // 构造函数。      
  13.     // 从index位置开始进行迭代      
  14.     ListItr(int index) {      
  15.         // index的有效性处理      
  16.         if (index < 0 || index > size)      
  17.             throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size);      
  18.         // 若 “index 小于 ‘双向链表长度的一半’”,则从第一个元素开始往后查找;      
  19.         // 否则,从最后一个元素往前查找。      
  20.         if (index < (size >> 1)) {      
  21.             next = header.next;      
  22.             for (nextIndex=0; nextIndex<index; nextIndex++)      
  23.                 next = next.next;      
  24.         } else {      
  25.             next = header;      
  26.             for (nextIndex=size; nextIndex>index; nextIndex--)      
  27.                 next = next.previous;      
  28.         }      
  29.     }      
  30.   
  31.     // 是否存在下一个元素      
  32.     public boolean hasNext() {      
  33.         // 通过元素索引是否等于“双向链表大小”来判断是否达到最后。      
  34.         return nextIndex != size;      
  35.     }      
  36.   
  37.     // 获取下一个元素      
  38.     public E next() {      
  39.         checkForComodification();      
  40.         if (nextIndex == size)      
  41.             throw new NoSuchElementException();      
  42.   
  43.         lastReturned = next;      
  44.         // next指向链表的下一个元素      
  45.         next = next.next;      
  46.         nextIndex++;      
  47.         return lastReturned.element;      
  48.     }      
  49.   
  50.     // 是否存在上一个元素      
  51.     public boolean hasPrevious() {      
  52.         // 通过元素索引是否等于0,来判断是否达到开头。      
  53.         return nextIndex != 0;      
  54.     }      
  55.   
  56.     // 获取上一个元素      
  57.     public E previous() {      
  58.         if (nextIndex == 0)      
  59.         throw new NoSuchElementException();      
  60.   
  61.         // next指向链表的上一个元素      
  62.         lastReturned = next = next.previous;      
  63.         nextIndex--;      
  64.         checkForComodification();      
  65.         return lastReturned.element;      
  66.     }      
  67.   
  68.     // 获取下一个元素的索引      
  69.     public int nextIndex() {      
  70.         return nextIndex;      
  71.     }      
  72.   
  73.     // 获取上一个元素的索引      
  74.     public int previousIndex() {      
  75.         return nextIndex-1;      
  76.     }      
  77.   
  78.     // 删除当前元素。      
  79.     // 删除双向链表中的当前节点      
  80.     public void remove() {      
  81.         checkForComodification();      
  82.         Entry<E> lastNext = lastReturned.next;      
  83.         try {      
  84.             LinkedList.this.remove(lastReturned);      
  85.         } catch (NoSuchElementException e) {      
  86.             throw new IllegalStateException();      
  87.         }      
  88.         if (next==lastReturned)      
  89.             next = lastNext;      
  90.         else     
  91.             nextIndex--;      
  92.         lastReturned = header;      
  93.         expectedModCount++;      
  94.     }      
  95.   
  96.     // 设置当前节点为e      
  97.     public void set(E e) {      
  98.         if (lastReturned == header)      
  99.             throw new IllegalStateException();      
  100.         checkForComodification();      
  101.         lastReturned.element = e;      
  102.     }      
  103.   
  104.     // 将e添加到当前节点的前面      
  105.     public void add(E e) {      
  106.         checkForComodification();      
  107.         lastReturned = header;      
  108.         addBefore(e, next);      
  109.         nextIndex++;      
  110.         expectedModCount++;      
  111.     }      
  112.   
  113.     // 判断 “modCount和expectedModCount是否相等”,依次来实现fail-fast机制。      
  114.     final void checkForComodification() {      
  115.         if (modCount != expectedModCount)      
  116.         throw new ConcurrentModificationException();      
  117.     }      
  118. }   
    // List迭代器    
    private class ListItr implements ListIterator<E> {    
        // 上一次返回的节点    
        private Entry<E> lastReturned = header;    
        // 下一个节点    
        private Entry<E> next;    
        // 下一个节点对应的索引值    
        private int nextIndex;    
        // 期望的改变计数。用来实现fail-fast机制。    
        private int expectedModCount = modCount;    
   
        // 构造函数。    
        // 从index位置开始进行迭代    
        ListItr(int index) {    
            // index的有效性处理    
            if (index < 0 || index > size)    
                throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size);    
            // 若 “index 小于 ‘双向链表长度的一半’”,则从第一个元素开始往后查找;    
            // 否则,从最后一个元素往前查找。    
            if (index < (size >> 1)) {    
                next = header.next;    
                for (nextIndex=0; nextIndex<index; nextIndex++)    
                    next = next.next;    
            } else {    
                next = header;    
                for (nextIndex=size; nextIndex>index; nextIndex--)    
                    next = next.previous;    
            }    
        }    
   
        // 是否存在下一个元素    
        public boolean hasNext() {    
            // 通过元素索引是否等于“双向链表大小”来判断是否达到最后。    
            return nextIndex != size;    
        }    
   
        // 获取下一个元素    
        public E next() {    
            checkForComodification();    
            if (nextIndex == size)    
                throw new NoSuchElementException();    
   
            lastReturned = next;    
            // next指向链表的下一个元素    
            next = next.next;    
            nextIndex++;    
            return lastReturned.element;    
        }    
   
        // 是否存在上一个元素    
        public boolean hasPrevious() {    
            // 通过元素索引是否等于0,来判断是否达到开头。    
            return nextIndex != 0;    
        }    
   
        // 获取上一个元素    
        public E previous() {    
            if (nextIndex == 0)    
            throw new NoSuchElementException();    
   
            // next指向链表的上一个元素    
            lastReturned = next = next.previous;    
            nextIndex--;    
            checkForComodification();    
            return lastReturned.element;    
        }    
   
        // 获取下一个元素的索引    
        public int nextIndex() {    
            return nextIndex;    
        }    
   
        // 获取上一个元素的索引    
        public int previousIndex() {    
            return nextIndex-1;    
        }    
   
        // 删除当前元素。    
        // 删除双向链表中的当前节点    
        public void remove() {    
            checkForComodification();    
            Entry<E> lastNext = lastReturned.next;    
            try {    
                LinkedList.this.remove(lastReturned);    
            } catch (NoSuchElementException e) {    
                throw new IllegalStateException();    
            }    
            if (next==lastReturned)    
                next = lastNext;    
            else   
                nextIndex--;    
            lastReturned = header;    
            expectedModCount++;    
        }    
   
        // 设置当前节点为e    
        public void set(E e) {    
            if (lastReturned == header)    
                throw new IllegalStateException();    
            checkForComodification();    
            lastReturned.element = e;    
        }    
   
        // 将e添加到当前节点的前面    
        public void add(E e) {    
            checkForComodification();    
            lastReturned = header;    
            addBefore(e, next);    
            nextIndex++;    
            expectedModCount++;    
        }    
   
        // 判断 “modCount和expectedModCount是否相等”,依次来实现fail-fast机制。    
        final void checkForComodification() {    
            if (modCount != expectedModCount)    
            throw new ConcurrentModificationException();    
        }    
    } 
 LinkedList还有一个提供Iterator的方法:descendingIterator()。该方法返回一个DescendingIterator对象。DescendingIterator是LinkedList的一个内部类。

  1. // 反向迭代器      
  2. public Iterator<E> descendingIterator() {      
  3.     return new DescendingIterator();      
  4. }      
  5.   
  6. // 反向迭代器实现类。      
  7. private class DescendingIterator implements Iterator {      
  8.     final ListItr itr = new ListItr(size());      
  9.     // 反向迭代器是否下一个元素。      
  10.     // 实际上是判断双向链表的当前节点是否达到开头      
  11.     public boolean hasNext() {      
  12.         return itr.hasPrevious();      
  13.     }      
  14.     // 反向迭代器获取下一个元素。      
  15.     // 实际上是获取双向链表的前一个节点      
  16.     public E next() {      
  17.         return itr.previous();      
  18.     }      
  19.     // 删除当前节点      
  20.     public void remove() {      
  21.         itr.remove();      
  22.     }      
  23. }   
    // 反向迭代器    
    public Iterator<E> descendingIterator() {    
        return new DescendingIterator();    
    }    
   
    // 反向迭代器实现类。    
    private class DescendingIterator implements Iterator {    
        final ListItr itr = new ListItr(size());    
        // 反向迭代器是否下一个元素。    
        // 实际上是判断双向链表的当前节点是否达到开头    
        public boolean hasNext() {    
            return itr.hasPrevious();    
        }    
        // 反向迭代器获取下一个元素。    
        // 实际上是获取双向链表的前一个节点    
        public E next() {    
            return itr.previous();    
        }    
        // 删除当前节点    
        public void remove() {    
            itr.remove();    
        }    
    } 

6、总结

1、从源码中很明显可以看出,LinkedList的实现是基于双向循环链表的,且头结点中不存放数据,如下图;


2、注意两个不同的构造方法。无参构造方法直接建立一个仅包含head节点的空链表,包含Collection的构造方法,先调用无参构造方法建立一个空链表,而后将Collection中的数据加入到链表的尾部后面。

3、在查找和删除某元素时,源码中都划分为该元素为null和不为null两种情况来处理LinkedList中允许元素为null

4、LinkedList是基于链表实现的,因此不存在容量不足的问题,所以这里没有扩容的方法

5、注意源码中的Entry<E> entry(int index)方法。该方法返回双向链表中指定位置处的节点,而链表中是没有下标索引的,要指定位置出的元素,就要遍历该链表,从源码的实现中,我们看到这里有一个加速动作。源码中先将index与长度size的一半比较,如果index<size/2,就只从位置0往后遍历到位置index处,而如果index>size/2,就只从位置size往前遍历到位置index处。这样可以减少一部分不必要的遍历,从而提高一定的效率(实际上效率还是很低)

6、注意链表类对应的数据结构Entry。


7、LinkedList是基于链表实现的,因此插入删除效率高,查找效率低(虽然有一个加速动作)。


8、要注意源码中还实现了栈和队列的操作方法,因此也可以作为栈、队列和双端队列来使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值