LinkedList源码浅析

原文:http://blog.csdn.net/moreevan/article/details/6783801

LinkedList源码简单分析

 

LinkedList的声明

  1. public class LinkedList<E>  
  2.     extends AbstractSequentialList<E>  
  3.     implements List<E>, Deque<E>/*这是双端队列接口,这个接口扩展了Queue接口,提供了更多的方法,比如push,pop等*/, Cloneable, java.io.Serializable  

所以LinkedList可以被用作Stack,Queue和Deque

 

来看一下链表结点的 定义

  1. private static class Entry<E> {  
  2.     E element;  
  3.     Entry<E> next;  
  4.     Entry<E> previous; //由此可以看出LinkedList是一个双向链表  
  5.    
  6.     Entry(E element, Entry<E> next, Entry<E> previous) {  
  7.         this.element = element;  
  8.         this.next = next;  
  9.         this.previous = previous;  
  10.     }  

LinkedList中声明了下面两个实例变量:

//头结点,起标记作用,并不记录元素

private transient Entry<E> header = new Entry<E>(null,null, null);

//链表的大小 ,即链表中元素的个数

    private transient int size = 0;

 

我们可以看到这两个 就是都被声明成了transient,所以在序列化的过程中会忽略它们,但是LinkedList提供的序列化方法writeObject(java.io.ObjectOutputStream s)中却序列化了size,并且将除header之外的所有结点都 写到序列化文件中了,那为什么要把size声明成transient呢,不解。。求解释。。

 

几个重要的方法:

  1. /** 
  2.      * Returns the indexed entry. 
  3.     根据给定的索引值离表头近还是离表尾近决定从头还是从尾开始遍历 
  4.      */  
  5.     private Entry<E> entry(int index) {  
  6.         if (index < 0 || index >= size)  
  7.             throw new IndexOutOfBoundsException("Index: "+index+  
  8.                                                 ", Size: "+size);  
  9.         Entry<E> e = header;  
  10.         if (index < (size >> 1)) { //如果较靠近有表头  
  11.             for (int i = 0; i <= index; i++)  
  12.                 e = e.next;  
  13.         } else { //较靠近表尾  
  14.             for (int i = size; i > index; i--)  
  15.                 e = e.previous;  
  16.         }  
  17.         return e;  
  18. }  

  1. /**  
  2. *将元素e添加到entry结点之前  
  3. */  
  4. private Entry<E> addBefore(E e, Entry<E> entry) {  
  5.     Entry<E> newEntry = new Entry<E>(e, entry, entry.previous);  
  6.     newEntry.previous.next = newEntry; //将新结点与前后结点相连接   
  7.     newEntry.next.previous = newEntry;  
  8.     size++;  
  9.     modCount++;  
  10.     return newEntry;  
  11.     }  
  12.   
  13.   
  14. /** 
  15. *删除给定的结点e 
  16. */  
  17.     private E remove(Entry<E> e) {  
  18.     if (e == header)  
  19.         throw new NoSuchElementException();  
  20.   
  21.         E result = e.element;  
  22.     e.previous.next = e.next;  
  23.     e.next.previous = e.previous;  
  24.         e.next = e.previous = null;  
  25.         e.element = null;  
  26.     size--;  
  27.     modCount++;  
  28.         return result;  
  29.     }  
  30.   
  31.   
  32.   
  33. /**  
  34. *从表头开始遍历,返回此元素在表中的第一个位置 
  35.      */  
  36.     public int indexOf(Object o) {  
  37.         int index = 0;  
  38.         if (o==null) { //如果传入的元素是null,则不能调用 eqauls方法进行比较  
  39.             for (Entry e = header.next; e != header; e = e.next) {  
  40.                 if (e.element==null)  
  41.                     return index;  
  42.                 index++;  
  43.             }  
  44.         } else {  
  45.             for (Entry e = header.next; e != header; e = e.next) {  
  46.                 if (o.equals(e.element))  
  47.                     return index;  
  48.                 index++;  
  49.             }  
  50.         }  
  51.         return -1;  
  52.     }  
  53.   
  54. /** 
  55. *默认的添加动作,可以看到这个方法是把新元素添加 到表尾 
  56. */  
  57. public boolean add(E e) {  
  58.     addBefore(e, header); //加到头结点之前 ,即表尾  
  59.         return true;  
  60.     }  
  61.   
  62. /** 
  63. *默认的删除动作是删除链表的第一个元素,所以说在默认情况下,LinkedList其实扮*演的是一个队列的角色 
  64. */  
  65. public E remove() {  
  66.         return removeFirst();  
  67.     }  
  68.   
  69. /** 
  70. *返回第一个元素 
  71. */  
  72. public E peek() {  
  73.         if (size==0)  
  74.             return null;  
  75.         return getFirst();  
  76.     }  

可以看出,如果表为空的时候 ,这个方法并不会抛出异常,而是返回null,而传统的(在Collections中声明的)方法则会抛出异常。相似的方法还有:poll,但请注意pop方法在表空的时候 会抛出异常。

 

还有一点请注意,如果先返回list的Iterator,之后 又对链表进行了添加删除修改操作,那么如果再使用返回的那个Iterator就会抛出ConcurrentModificationException。

 

 

最后看一下LinkedList是如何序列化和反序列化的,如果对这两个反序列化中用到的这两个回调方法有疑问的,可以看我的这篇博客 http://blog.csdn.net/moreevan/article/details/6697777

  1. /** 
  2.      * Save the state of this <tt>LinkedList</tt> instance to a stream (that 
  3.      * is, serialize it). 
  4.      * 
  5.      * @serialData The size of the list (the number of elements it 
  6.      *             contains) is emitted (int), followed by all of its 
  7.      *             elements (each an Object) in the proper order. 
  8.      */  
  9.     private void writeObject(java.io.ObjectOutputStream s)  
  10.         throws java.io.IOException {  
  11.     // Write out any hidden serialization magic  
  12.     s.defaultWriteObject();  
  13.   
  14.         // Write out size  
  15.         s.writeInt(size);  
  16.   
  17.     // Write out all elements in the proper order.  
  18.         for (Entry e = header.next; e != header; e = e.next)  
  19.             s.writeObject(e.element);  
  20.     }  
  21.   
  22.     /** 
  23.      * Reconstitute this <tt>LinkedList</tt> instance from a stream (that is 
  24.      * deserialize it). 
  25.      */  
  26.     private void readObject(java.io.ObjectInputStream s)  
  27.         throws java.io.IOException, ClassNotFoundException {  
  28.     // Read in any hidden serialization magic  
  29.     s.defaultReadObject();  
  30.   
  31.         // Read in size  
  32.         int size = s.readInt();  
  33.   
  34.         // Initialize header  
  35.         header = new Entry<E>(nullnullnull);  
  36.         header.next = header.previous = header;  
  37.   
  38.     // Read in all elements in the proper order.  
  39.     for (int i=0; i<size; i++)  
  40.             addBefore((E)s.readObject(), header);  
  41.     }  

综上 ,我们可以看出。LinkedList是一个双向链表,它可以被当成栈,队列或双端队列来使用。它也提供 了随机访问元素的方法,不过这个访问的时间复杂度并不像ArrayList是O(1),而是O(n)。它删除给定位置的元素

的效率其实并不比ArrayList高。但它删除特定元素的效率 要比ArrayList高。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值