List集合底层实现

1.List:

List实现Collection接口,它的数据结构是有序可以重复的结合,该结合的体系有索引;它有三个实现类:ArrayList、LinkList、Vector三个实现类;

三个实现类的区别:

ArrayList:底层数据结构使数组结构,查询速度快,增删改慢,

LinkList:底层使用链表结构,增删速度快,查询稍慢;

Vector:底层是数组结构,线程同步ArrayList是线程不同步;

可变长度数组不断new数组:

ArrayList当初始化容量超过10时,会new一个50%de ,把原来的东西放入这150%中;

Vector:当容量超过10时,会new一个100%的浪费内存;

List接口对Collection进行了简单的扩充,它的具体实现类常用的有ArrayListLinkedList。你可以将任何东西放到一个List容器中,并在需要时从中取出。ArrayList从其命名中可以看出它是一种类似数组的形式进行存储,因此它的随机访问速度极快,而LinkedList的内部实现是链表,它适合于在链表中间需要频繁进行插入和删除操作。在具体应用时可以根据需要自由选择。前面说的Iterator只能对容器进行向前遍历,而ListIterator则继承了Iterator的思想,并提供了对List进行双向遍历的方法。

2.ArrayList

1. ArrayList概述:

   ArrayList是List接口的可变数组的实现。实现了所有可选列表操作,并允许包括 null 在内的所有元素。除了实现 List 接口外,此类还提供一些方法来操作内部用来存储列表的数组的大小。
   每个ArrayList实例都有一个容量,该容量是指用来存储列表元素的数组的大小。它总是至少等于列表的大小。随着向ArrayList中不断添加元素,其容量也自动增长。自动增长会带来数据向新数组的重新拷贝,因此,如果可预知数据量的多少,可在构造ArrayList时指定其容量。在添加大量元素前,应用程序也可以使用ensureCapacity操作来增加ArrayList实例的容量,这可以减少递增式再分配的数量。 
   注意,此实现不是同步的。如果多个线程同时访问一个ArrayList实例,而其中至少一个线程从结构上修改了列表,那么它必须保持外部同步。

2. ArrayList的实现:

   对于ArrayList而言,它实现List接口、底层使用数组保存所有元素。其操作基本上是对数组的操作。下面我们来分析ArrayList的源代码:底层使用数组实现

Java代码  

private  transient Object[] elementData;

 2)构造方法: 
   ArrayList提供了三种方式的构造器,可以构造一个默认初始容量为10的空列表、构造一个指定初始容量的空列表以及构造一个包含指定collection的元素的列表,这些元素按照该collection的迭代器返回它们的顺序排列的。

Java代码  

1.     public ArrayList() {  

2.         this(10);  

3.     }  

4.       

5.     public ArrayList(int initialCapacity) {  

6.         super();  

7.         if (initialCapacity < 0)  

8.             throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);  

9.         this.elementData = new Object[initialCapacity];  

10.  }  

11.    

12.  public ArrayList(Collection<? extends E> c) {  

13.      elementData = c.toArray();  

14.      size = elementData.length;  

15.      // c.toArray might (incorrectly) not return Object[] (see 6260652)  

16.      if (elementData.getClass() != Object[].class)  

17.          elementData = Arrays.copyOf(elementData, size, Object[].class);  

18.  }  


3)存储: 
   ArrayList提供了set(int index, E element)、add(E e)、add(int index, E element)、addAll(Collection<?extends E> c)、addAll(int index, Collection<?extends E> c)这些添加元素的方法。下面我们一一讲解:

Java代码  

1.     // 用指定的元素替代此列表中指定位置上的元素,并返回以前位于该位置上的元素。  

2.     public E set(int index, E element) {  

3.         RangeCheck(index);  

4.       

5.         E oldValue = (E) elementData[index];  

6.         elementData[index] = element;  

7.         return oldValue;  

8.     }  


Java代码  

1.     // 将指定的元素添加到此列表的尾部。  

2.     public boolean add(E e) {  

3.         ensureCapacity(size + 1);   

4.         elementData[size++] = e;  

5.         return true;  

6.     }  


Java代码  

1.     // 将指定的元素插入此列表中的指定位置。  

2.     // 如果当前位置有元素,则向右移动当前位于该位置的元素以及所有后续元素(将其索引加1)。  

3.     public void add(int index, E element) {  

4.         if (index > size || index < 0)  

5.             throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);  

6.         // 如果数组长度不足,将进行扩容。  

7.         ensureCapacity(size+1);  // Increments modCount!!  

8.         // 将 elementData中从Index位置开始、长度为size-index的元素,  

9.         // 拷贝到从下标为index+1位置开始的新的elementData数组中。  

10.      // 即将当前位于该位置的元素以及所有后续元素右移一个位置。  

11.      System.arraycopy(elementData, index, elementData, index + 1, size - index);  

12.      elementData[index] = element;  

13.      size++;  

14.  }  

Java代码  

1.     // 按照指定collection的迭代器所返回的元素顺序,将该collection中的所有元素添加到此列表的尾部。  

2.     public boolean addAll(Collection<? extends E> c) {  

3.         Object[] a = c.toArray();  

4.         int numNew = a.length;  

5.         ensureCapacity(size + numNew);  // Increments modCount  

6.         System.arraycopy(a, 0, elementData, size, numNew);  

7.         size += numNew;  

8.         return numNew != 0;  

9.     }  

Java代码  

1.     // 从指定的位置开始,将指定collection中的所有元素插入到此列表中。  

2.     public boolean addAll(int index, Collection<? extends E> c) {  

3.         if (index > size || index < 0)  

4.             throw new IndexOutOfBoundsException(  

5.                 "Index: " + index + ", Size: " + size);  

6.       

7.         Object[] a = c.toArray();  

8.         int numNew = a.length;  

9.         ensureCapacity(size + numNew);  // Increments modCount  

10.    

11.      int numMoved = size - index;  

12.      if (numMoved > 0)  

13.          System.arraycopy(elementData, index, elementData, index + numNew, numMoved);  

14.    

15.      System.arraycopy(a, 0, elementData, index, numNew);  

16.      size += numNew;  

17.      return numNew != 0;  

}   


读取

Java代码  

1.     // 返回此列表中指定位置上的元素。  

2.     public E get(int index) {  

3.         RangeCheck(index);  

4.       

5.         return (E) elementData[index];  

6.     }  

    5) 删除: 
   ArrayList提供了根据下标或者指定对象两种方式的删除功能。如下:

Java代码  

1.     // 移除此列表中指定位置上的元素。  

2.     public E remove(int index) {  

3.         RangeCheck(index);  

4.       

5.         modCount++;  

6.         E oldValue = (E) elementData[index];  

7.       

8.         int numMoved = size - index - 1;  

9.         if (numMoved > 0)  

10.          System.arraycopy(elementData, index+1, elementData, index, numMoved);  

11.      elementData[--size] = null; // Let gc do its work  

12.    

13.      return oldValue;  

14.  }  

Java代码  

1.     // 移除此列表中首次出现的指定元素(如果存在)。这是应为ArrayList中允许存放重复的元素。  

2.     public boolean remove(Object o) {  

3.         // 由于ArrayList中允许存放null,因此下面通过两种情况来分别处理。  

4.         if (o == null) {  

5.             for (int index = 0; index < size; index++)  

6.                 if (elementData[index] == null) {  

7.                     // 类似remove(int index),移除列表中指定位置上的元素。  

8.                     fastRemove(index);  

9.                     return true;  

10.              }  

11.  } else {  

12.      for (int index = 0; index < size; index++)  

13.          if (o.equals(elementData[index])) {  

14.              fastRemove(index);  

15.              return true;  

16.          }  

17.      }  

18.      return false;  

19.  }  

    注意:从数组中移除元素的操作,也会导致被移除的元素以后的所有元素的向左移动一个位置。
   6) 调整数组容量: 
   从上面介绍的向ArrayList中存储元素的代码中,我们看到,每当向数组中添加元素时,都要去检查添加后元素的个数是否会超出当前数组的长度,如果超出,数组将会进行扩容,以满足添加数据的需求。数组扩容通过一个公开的方法ensureCapacity(int minCapacity)来实现。在实际添加大量元素前,我也可以使用ensureCapacity来手动增加ArrayList实例的容量,以减少递增式再分配的数量。


Java代码  

1.     public void ensureCapacity(int minCapacity) {  

2.         modCount++;  

3.         int oldCapacity = elementData.length;  

4.         if (minCapacity > oldCapacity) {  

5.             Object oldData[] = elementData;  

6.             int newCapacity = (oldCapacity * 3)/2 + 1;  

7.                 if (newCapacity < minCapacity)  

8.                     newCapacity = minCapacity;  

9.           // minCapacity is usually close to size, so this is a win:  

10.        elementData = Arrays.copyOf(elementData, newCapacity);  

11.      }  

12.  }  

   从上述代码中可以看出,数组进行扩容时,会将老数组中的元素重新拷贝一份到新的数组中,每次数组容量的增长大约是其原容量的1.5倍。这种操作的代价是很高的,因此在实际使用时,我们应该尽量避免数组容量的扩张。当我们可预知要保存的元素的多少时,要在构造ArrayList实例时,就指定其容量,以避免数组扩容的发生。或者根据实际需求,通过调用ensureCapacity方法来手动增加ArrayList实例的容量。
   ArrayList还给我们提供了将底层数组的容量调整为当前列表保存的实际元素的大小的功能。它可以通过trimToSize方法来实现。代码如下:

Java代码  

1.     public void trimToSize() {  

2.         modCount++;  

3.         int oldCapacity = elementData.length;  

4.         if (size < oldCapacity) {  

5.             elementData = Arrays.copyOf(elementData, size);  

6.         }  

7.     }  

   7) Fail-Fast机制: 
ArrayList也采用了快速失败的机制,通过记录modCount参数来实现。在面对并发的修改时,迭代器很快就会完全失败,而不是冒着在将来某个不确定时间发生任意不确定行为的风险。具体介绍请参考我之前的文章深入Java集合学习系列:HashMap的实现原理 中的Fail-Fast机制。
   8) 关于其他 的一些方法的实现都很简单易懂,读者可参照API文档和源代码,一看便知,这里就不再多说。

ArrayList和Vector的区别:

1.Vector是线程同步的,所以它也是线程安全的。而ArratList是线程异步的,不安全。如果不考虑安全因素,一般用Arralist效率比较高,查看JDK文档,给出提示:

  如果要实现Arraylist线程同步,可以通过下面方式:

如果多个线程同时访问一个 ArrayList 实例,而其中至少一个线程从结构上修改了列表,那么它必须 保持外部同步。(结构上的修改是指任何添加或删除一个或多个元素的操作,或者显式调整底层数组的大小;仅仅设置元素的值不是结构上的修改。)这一般通过对自然封装该列表的对象进行同步操作来完成。如果不存在这样的对象,则应该使用Collections.synchronizedList 方法将该列表“包装”起来。这最好在创建时完成,以防止意外对列表进行不同步的访问:

        List list = Collections.synchronizedList(new ArrayList(...)); 

2.如果集合中的元素数量大于当前集合数组的长度时,Vector的增长率是目前数组长度的100%,而ArryaList增长率为目前数组长度的50%。所以,如果集合中使用数据量比较大的数据,用Vector有一定优势。

3.linkList

LinkedList与Collection关系如下图:

LinkedList的本质是双向链表。

(01) LinkedList继承于AbstractSequentialList,并且实现了Dequeue接口。

(02) LinkedList包含两个重要的成员:header 和 size。

  header是双向链表的表头,它是双向链表节点所对应的类Entry的实例。Entry中包含成员变量: previous, next, element。其中,previous是该节点的上一个节点,next是该节点的下一个节点,element是该节点所包含的值。

  size是双向链表中节点的个数。

 

第3部分 LinkedList源码解析(基于JDK1.6.0_45)

为了更了解LinkedList的原理,下面对LinkedList源码代码作出分析。

 

在阅读源码之前,我们先对LinkedList的整体实现进行大致说明:

    LinkedList实际上是通过双向链表去实现的。既然是双向链表,那么它的顺序访问会非常高效,而随机访问效率比较低。

    既然LinkedList是通过双向链表的,但是它也实现了List接口{也就是说,它实现了get(int location)、remove(int location)等“根据索引值来获取、删除节点的函数”}。LinkedList是如何实现List的这些接口的,如何将“双向链表和索引值联系起来的”?

    实际原理非常简单,它就是通过一个计数索引值来实现的。例如,当我们调用get(int location)时,首先会比较“location”和“双向链表长度的1/2”;若前者大,则从链表头开始往后查找,直到location位置;否则,从链表末尾开始先前查找,直到location位置。

   这就是“双线链表和索引值联系起来”的方法。

 

好了,接下来开始阅读源码(只要理解双向链表,那么LinkedList的源码很容易理解的)。

 

复制代码

  1 package java.util;

  2

  3 public class LinkedList<E>

  4    extends AbstractSequentialList<E>

  5    implements List<E>, Deque<E>, Cloneable,java.io.Serializable

  6 {

  7    // 链表的表头,表头不包含任何数据。Entry是个链表类数据结构。

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

  9

 10    // LinkedList中元素个数

 11    private transient int size = 0;

 12

 13    // 默认构造函数:创建一个空的链表

 14    public LinkedList() {

 15        header.next = header.previous = header;

 16     }

 17

 18    // 包含“集合”的构造函数:创建一个包含“集合”的LinkedList

 19    public LinkedList(Collection<? extends E> c) {

 20        this();

 21        addAll(c);

 22     }

 23

 24    // 获取LinkedList的第一个元素

 25    public E getFirst() {

 26        if (size==0)

 27            throw newNoSuchElementException();

 28

 29        // 链表的表头header中不包含数据。

 30        // 这里返回header所指下一个节点所包含的数据。

 31        return header.next.element;

 32     }

 33

 34    // 获取LinkedList的最后一个元素

 35    public E getLast()  {

 36        if (size==0)

 37            throw new NoSuchElementException();

 38

 39        // 由于LinkedList是双向链表;而表头header不包含数据。

 40        // 因而,这里返回表头header的前一个节点所包含的数据。

 41        return header.previous.element;

 42     }

 43

 44    // 删除LinkedList的第一个元素

 45    public E removeFirst() {

 46        return remove(header.next);

 47     }

 48

 49    // 删除LinkedList的最后一个元素

 50    public E removeLast() {

 51        return remove(header.previous);

 52     }

 53

 54    // 将元素添加到LinkedList的起始位置

 55    public void addFirst(E e) {

 56        addBefore(e, header.next);

 57     }

 58

 59    // 将元素添加到LinkedList的结束位置

 60    public void addLast(E e) {

 61        addBefore(e, header);

 62     }

 63

 64    // 判断LinkedList是否包含元素(o)

 65    public boolean contains(Object o) {

 66        return indexOf(o) != -1;

 67     }

 68

 69    // 返回LinkedList的大小

 70    public int size() {

 71        return size;

 72     }

 73

 74    // 将元素(E)添加到LinkedList中

 75    public boolean add(E e) {

 76        // 将节点(节点数据是e)添加到表头(header)之前。

 77        // 即,将节点添加到双向链表的末端。

 78        addBefore(e, header);

 79        return true;

 80     }

 81

 82    // 从LinkedList中删除元素(o)

 83    // 从链表开始查找,如存在元素(o)则删除该元素并返回true;

 84    // 否则,返回false。

 85    public boolean remove(Object o) {

 86        if (o==null) {

 87            // 若o为null的删除情况

 88            for (Entry<E> e = header.next; e != header; e = e.next) {

 89                 if (e.element==null) {

 90                    remove(e);

 91                     return true;

 92                 }

 93            }

 94        } else {

 95            // 若o不为null的删除情况

 96            for (Entry<E> e = header.next; e != header; e = e.next) {

 97                 if (o.equals(e.element)) {

 98                     remove(e);

 99                     return true;

100                 }

101             }

102         }

103         return false;

104     }

105

106     // 将“集合(c)”添加到LinkedList中。

107     // 实际上,是从双向链表的末尾开始,将“集合(c)”添加到双向链表中。

108     public boolean addAll(Collection<?extends E> c) {

109         return addAll(size, c);

110     }

111

112     // 从双向链表的index开始,将“集合(c)”添加到双向链表中。

113     public boolean addAll(int index,Collection<? extends E> c) {

114         if (index < 0 || index > size)

115             throw newIndexOutOfBoundsException("Index: "+index+

116                                                ", Size: "+size);

117         Object[] a = c.toArray();

118         // 获取集合的长度

119         int numNew = a.length;

120         if (numNew==0)

121             return false;

122         modCount++;

123

124         // 设置“当前要插入节点的后一个节点”

125         Entry<E> successor =(index==size ? header : entry(index));

126         // 设置“当前要插入节点的前一个节点”

127         Entry<E> predecessor =successor.previous;

128         // 将集合(c)全部插入双向链表中

129         for (int i=0; i<numNew; i++) {

130             Entry<E> e = newEntry<E>((E)a[i], successor, predecessor);

131             predecessor.next = e;

132             predecessor = e;

133         }

134         successor.previous = predecessor;

135

136         // 调整LinkedList的实际大小

137         size += numNew;

138         return true;

139     }

140

141     // 清空双向链表

142     public void clear() {

143         Entry<E> e = header.next;

144         // 从表头开始,逐个向后遍历;对遍历到的节点执行一下操作:

145         // (01) 设置前一个节点为null

146         // (02) 设置当前节点的内容为null

147         // (03) 设置后一个节点为“新的当前节点”

148         while (e != header) {

149             Entry<E> next = e.next;

150             e.next = e.previous = null;

151             e.element = null;

152             e = next;

153         }

154         header.next = header.previous =header;

155         // 设置大小为0

156         size = 0;

157         modCount++;

158     }

159

160     // 返回LinkedList指定位置的元素

161     public E get(int index) {

162         return entry(index).element;

163     }

164

165     // 设置index位置对应的节点的值为element

166     public E set(int index, E element) {

167         Entry<E> e = entry(index);

168         E oldVal = e.element;

169         e.element = element;

170         return oldVal;

171     }

172 

173     // 在index前添加节点,且节点的值为element

174     public void add(int index, E element) {

175         addBefore(element, (index==size ?header : entry(index)));

176     }

177

178     // 删除index位置的节点

179     public E remove(int index) {

180         return remove(entry(index));

181     }

182

183     // 获取双向链表中指定位置的节点

184     private Entry<E> entry(int index) {

185         if (index < 0 || index >= size)

186             throw newIndexOutOfBoundsException("Index: "+index+

187                                                ", Size: "+size);

188         Entry<E> e = header;

189         // 获取index处的节点。

190         // 若index < 双向链表长度的1/2,则从前先后查找;

191         // 否则,从后向前查找。

192         if (index < (size >> 1)) {

193             for (int i = 0; i <= index;i++)

194                 e = e.next;

195         } else {

196             for (int i = size; i > index;i--)

197                 e = e.previous;

198         }

199         return e;

200     }

201

202     // 从前向后查找,返回“值为对象(o)的节点对应的索引”

203     // 不存在就返回-1

204     public int indexOf(Object o) {

205         int index = 0;

206         if (o==null) {

207             for (Entry e = header.next; e !=header; e = e.next) {

208                 if (e.element==null)

209                     return index;

210                 index++;

211             }

212         } else {

213             for (Entry e = header.next; e !=header; e = e.next) {

214                 if (o.equals(e.element))

215                     return index;

216                 index++;

217             }

218         }

219         return -1;

220     }

221

222     // 从后向前查找,返回“值为对象(o)的节点对应的索引”

223     // 不存在就返回-1

224     public int lastIndexOf(Object o) {

225         int index = size;

226         if (o==null) {

227             for (Entry e = header.previous; e!= header; e = e.previous) {

228                 index--;

229                 if (e.element==null)

230                     return index;

231             }

232         } else {

233             for (Entry e = header.previous; e !=header; e = e.previous) {

234                 index--;

235                 if (o.equals(e.element))

236                     return index;

237             }

238         }

239         return -1;

240     }

241

242     // 返回第一个节点

243     // 若LinkedList的大小为0,则返回null

244     public E peek() {

245         if (size==0)

246             return null;

247         return getFirst();

248     }

249

250     // 返回第一个节点

251     // 若LinkedList的大小为0,则抛出异常

252     public E element() {

253         return getFirst();

254     }

255

256     // 删除并返回第一个节点

257     // 若LinkedList的大小为0,则返回null

258     public E poll() {

259         if (size==0)

260             return null;

261         return removeFirst();

262     }

263

264     // 将e添加双向链表末尾

265     public boolean offer(E e) {

266         return add(e);

267     }

268

269     // 将e添加双向链表开头

270     public boolean offerFirst(E e) {

271         addFirst(e);

272         return true;

273     }

274

275     // 将e添加双向链表末尾

276     public boolean offerLast(E e) {

277         addLast(e);

278         return true;

279     }

280

281     // 返回第一个节点

282     // 若LinkedList的大小为0,则返回null

283     public E peekFirst() {

284         if (size==0)

285             return null;

286         return getFirst();

287     }

288

289     // 返回最后一个节点

290     // 若LinkedList的大小为0,则返回null

291     public E peekLast() {

292         if (size==0)

293             return null;

294         return getLast();

295     }

296

297     // 删除并返回第一个节点

298     // 若LinkedList的大小为0,则返回null

299     public E pollFirst() {

300         if (size==0)

301             return null;

302         return removeFirst();

303     }

304

305     // 删除并返回最后一个节点

306     // 若LinkedList的大小为0,则返回null

307     public E pollLast() {

308         if (size==0)

309             return null;

310         return removeLast();

311     }

312

313     // 将e插入到双向链表开头

314     public void push(E e) {

315         addFirst(e);

316     }

317

318     // 删除并返回第一个节点

319     public E pop() {

320         return removeFirst();

321     }

322

323     // 从LinkedList开始向后查找,删除第一个值为元素(o)的节点

324     // 从链表开始查找,如存在节点的值为元素(o)的节点,则删除该节点

325     public booleanremoveFirstOccurrence(Object o) {

326         return remove(o);

327     }

328

329     // 从LinkedList末尾向前查找,删除第一个值为元素(o)的节点

330     // 从链表开始查找,如存在节点的值为元素(o)的节点,则删除该节点

331     public boolean removeLastOccurrence(Objecto) {

332         if (o==null) {

333             for (Entry<E> e =header.previous; e != header; e = e.previous) {

334                 if (e.element==null) {

335                     remove(e);

336                     return true;

337                 }

338             }

339         } else {

340             for (Entry<E> e =header.previous; e != header; e = e.previous) {

341                 if (o.equals(e.element)) {

342                     remove(e);

343                     return true;

344                 }

345             }

346         }

347         return false;

348     }

349

350     // 返回“index到末尾的全部节点”对应的ListIterator对象(List迭代器)

351     public ListIterator<E> listIterator(intindex) {

352         return new ListItr(index);

353     }

354

355     // List迭代器

356     private class ListItr implementsListIterator<E> {

357         // 上一次返回的节点

358         private Entry<E> lastReturned =header;

359         // 下一个节点

360         private Entry<E> next;

361         // 下一个节点对应的索引值

362         private int nextIndex;

363         // 期望的改变计数。用来实现fail-fast机制。

364         private int expectedModCount =modCount;

365

366         // 构造函数。

367         // 从index位置开始进行迭代

368         ListItr(int index) {

369             // index的有效性处理

370             if (index < 0 || index >size)

371                 throw newIndexOutOfBoundsException("Index: "+index+ ", Size:"+size);

372             // 若“index 小于‘双向链表长度的一半’”,则从第一个元素开始往后查找;

373             // 否则,从最后一个元素往前查找。

374             if (index < (size >> 1)){

375                 next = header.next;

376                 for (nextIndex=0;nextIndex<index; nextIndex++)

377                     next = next.next;

378             } else {

379                 next = header;

380                 for (nextIndex=size;nextIndex>index; nextIndex--)

381                     next = next.previous;

382             }

383         }

384

385         // 是否存在下一个元素

386         public boolean hasNext() {

387             // 通过元素索引是否等于“双向链表大小”来判断是否达到最后。

388             return nextIndex != size;

389         }

390

391         // 获取下一个元素

392         public E next() {

393             checkForComodification();

394             if (nextIndex == size)

395                 throw newNoSuchElementException();

396

397             lastReturned = next;

398             // next指向链表的下一个元素

399             next = next.next;

400             nextIndex++;

401             return lastReturned.element;

402         }

403

404         // 是否存在上一个元素

405         public boolean hasPrevious() {

406             // 通过元素索引是否等于0,来判断是否达到开头。

407             return nextIndex != 0;

408         }

409

410         // 获取上一个元素

411         public E previous() {

412             if (nextIndex == 0)

413             throw newNoSuchElementException();

414

415             // next指向链表的上一个元素

416             lastReturned = next =next.previous;

417             nextIndex--;

418             checkForComodification();

419             return lastReturned.element;

420         }

421

422         // 获取下一个元素的索引

423         public int nextIndex() {

424             return nextIndex;

425         }

426

427         // 获取上一个元素的索引

428         public int previousIndex() {

429             return nextIndex-1;

430         }

431

432         // 删除当前元素。

433         // 删除双向链表中的当前节点

434         public void remove() {

435             checkForComodification();

436             Entry<E> lastNext =lastReturned.next;

437             try {

438                LinkedList.this.remove(lastReturned);

439             } catch (NoSuchElementException e){

440                 throw newIllegalStateException();

441             }

442             if (next==lastReturned)

443                 next = lastNext;

444             else

445                 nextIndex--;

446             lastReturned = header;

447             expectedModCount++;

448         }

449

450         // 设置当前节点为e

451         public void set(E e) {

452             if (lastReturned == header)

453                 throw newIllegalStateException();

454             checkForComodification();

455             lastReturned.element = e;

456         }

457

458         // 将e添加到当前节点的前面

459         public void add(E e) {

460             checkForComodification();

461             lastReturned = header;

462             addBefore(e, next);

463             nextIndex++;

464             expectedModCount++;

465         }

466

467         // 判断“modCount和expectedModCount是否相等”,依次来实现fail-fast机制。

468         final void checkForComodification() {

469             if (modCount != expectedModCount)

470             throw newConcurrentModificationException();

471         }

472     }

473

474     // 双向链表的节点所对应的数据结构。

475     // 包含3部分:上一节点,下一节点,当前节点值。

476     private static class Entry<E> {

477         // 当前节点所包含的值

478         E element;

479         // 下一个节点

480         Entry<E> next;

481         // 上一个节点

482         Entry<E> previous;

483

484         /**

485          * 链表节点的构造函数。

486          * 参数说明:

487          *  element  —— 节点所包含的数据

488          *  next      —— 下一个节点

489          *  previous —— 上一个节点

490          */

491         Entry(E element, Entry<E> next,Entry<E> previous) {

492             this.element = element;

493             this.next = next;

494             this.previous = previous;

495         }

496     }

497

498     // 将节点(节点数据是e)添加到entry节点之前。

499     private Entry<E> addBefore(E e,Entry<E> entry) {

500         // 新建节点newEntry,将newEntry插入到节点e之前;并且设置newEntry的数据是e

501         Entry<E> newEntry = newEntry<E>(e, entry, entry.previous);

502         newEntry.previous.next = newEntry;

503         newEntry.next.previous = newEntry;

504         // 修改LinkedList大小

505         size++;

506         // 修改LinkedList的修改统计数:用来实现fail-fast机制。

507         modCount++;

508         return newEntry;

509     }

510

511     // 将节点从链表中删除

512     private E remove(Entry<E> e) {

513         if (e == header)

514             throw newNoSuchElementException();

515

516         E result = e.element;

517         e.previous.next = e.next;

518         e.next.previous = e.previous;

519         e.next = e.previous = null;

520         e.element = null;

521         size--;

522         modCount++;

523         return result;

524     }

525

526     // 反向迭代器

527     public Iterator<E>descendingIterator() {

528         return new DescendingIterator();

529     }

530

531     // 反向迭代器实现类。

532     private class DescendingIteratorimplements Iterator {

533         final ListItr itr = newListItr(size());

534         // 反向迭代器是否下一个元素。

535         // 实际上是判断双向链表的当前节点是否达到开头

536         public boolean hasNext() {

537             return itr.hasPrevious();

538         }

539         // 反向迭代器获取下一个元素。

540         // 实际上是获取双向链表的前一个节点

541         public E next() {

542             return itr.previous();

543         }

544         // 删除当前节点

545         public void remove() {

546             itr.remove();

547         }

548     }

549

550

551     // 返回LinkedList的Object[]数组

552     public Object[] toArray() {

553     // 新建Object[]数组

554     Object[] result = new Object[size];

555         int i = 0;

556         // 将链表中所有节点的数据都添加到Object[]数组中

557         for (Entry<E> e = header.next; e!= header; e = e.next)

558             result[i++] = e.element;

559     return result;

560     }

561

562     // 返回LinkedList的模板数组。所谓模板数组,即可以将T设为任意的数据类型

563     public <T> T[] toArray(T[] a) {

564         // 若数组a的大小 < LinkedList的元素个数(意味着数组a不能容纳LinkedList中全部元素)

565         // 则新建一个T[]数组,T[]的大小为LinkedList大小,并将该T[]赋值给a。

566         if (a.length < size)

567             a =(T[])java.lang.reflect.Array.newInstance(

568                                 a.getClass().getComponentType(),size);

569         // 将链表中所有节点的数据都添加到数组a中

570         int i = 0;

571         Object[] result = a;

572         for (Entry<E> e = header.next; e!= header; e = e.next)

573             result[i++] = e.element;

574

575         if (a.length > size)

576             a[size] = null;

577

578         return a;

579     }

580

581

582     // 克隆函数。返回LinkedList的克隆对象。

583     public Object clone() {

584         LinkedList<E> clone = null;

585         // 克隆一个LinkedList克隆对象

586         try {

587             clone = (LinkedList<E>)super.clone();

588         } catch (CloneNotSupportedException e){

589             throw new InternalError();

590         }

591

592         // 新建LinkedList表头节点

593         clone.header = new Entry<E>(null,null, null);

594         clone.header.next =clone.header.previous = clone.header;

595         clone.size = 0;

596         clone.modCount = 0;

597

598         // 将链表中所有节点的数据都添加到克隆对象中

599         for (Entry<E> e = header.next; e!= header; e = e.next)

600             clone.add(e.element);

601

602         return clone;

603     }

604

605     // java.io.Serializable的写入函数

606     // 将LinkedList的“容量,所有的元素值”都写入到输出流中

607     private voidwriteObject(java.io.ObjectOutputStream s)

608         throws java.io.IOException {

609         // Write out any hidden serializationmagic

610         s.defaultWriteObject();

611

612         // 写入“容量”

613         s.writeInt(size);

614

615         // 将链表中所有节点的数据都写入到输出流中

616         for (Entry e = header.next; e != header;e = e.next)

617             s.writeObject(e.element);

618     }

619

620     // java.io.Serializable的读取函数:根据写入方式反向读出

621     // 先将LinkedList的“容量”读出,然后将“所有的元素值”读出

622     private voidreadObject(java.io.ObjectInputStream s)

623         throws java.io.IOException,ClassNotFoundException {

624         // Read in any hidden serializationmagic

625         s.defaultReadObject();

626

627         // 从输入流中读取“容量”

628         int size = s.readInt();

629

630         // 新建链表表头节点

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

632         header.next = header.previous =header;

633

634         // 从输入流中将“所有的元素值”并逐个添加到链表中

635         for (int i=0; i<size; i++)

636             addBefore((E)s.readObject(),header);

637     }

638

639 }

复制代码

 

总结:

(01) LinkedList 实际上是通过双向链表去实现的。

        它包含一个非常重要的内部类:Entry。Entry是双向链表节点所对应的数据结构,它包括的属性有:当前节点所包含的值,上一个节点,下一个节点。

(02) 从LinkedList的实现方式中可以发现,它不存在LinkedList容量不足的问题。

(03) LinkedList的克隆函数,即是将全部元素克隆到一个新的LinkedList对象中。

(04) LinkedList实现java.io.Serializable。当写入到输出流时,先写入“容量”,再依次写入“每一个节点保护的值”;当读出输入流时,先读取“容量”,再依次读取“每一个元素”。

(05) 由于LinkedList实现了Deque,而Deque接口定义了在双端队列两端访问元素的方法。提供插入、移除和检查元素的方法。每种方法都存在两种形式:一种形式在操作失败时抛出异常,另一种形式返回一个特殊值(null 或 false,具体取决于操作)。

 

总结起来如下表格:

 

        第一个元素(头部)                最后一个元素(尾部)

        抛出异常        特殊值           抛出异常          特殊值

插入   addFirst(e)    offerFirst(e)    addLast(e)        offerLast(e)

移除   removeFirst()  pollFirst()      removeLast()      pollLast()

检查   getFirst()     peekFirst()      getLast()         peekLast()

(06) LinkedList可以作为FIFO(先进先出)的队列,作为FIFO的队列时,下表的方法等价:

 

复制代码

队列方法      等效方法

add(e)        addLast(e)

offer(e)      offerLast(e)

remove()      removeFirst()

poll()        pollFirst()

element()     getFirst()

peek()        peekFirst()

复制代码

(07) LinkedList可以作为LIFO(后进先出)的栈,作为LIFO的栈时,下表的方法等价:

 

栈方法       等效方法

push(e)      addFirst(e)

pop()        removeFirst()

peek()       peekFirst()

 

无论如何,千万不要通过随机访问去遍历LinkedList!

3、Vector

相对于ArrayList来说,Vector线程是安全的,也就是说是同步的

创建了一个向量类的对象后,可以往其中随意地插入不同的类的对象,既不需顾及类型也不需预先选定向量的容量,并可方便地进行查找。对于预先不知或不愿预先定义数组大小,并需频繁进行查找、插入和删除工作的情况,可以考虑使用向量类。向量类提供了三种构造方法

public vector()

public vector(intinitialcapacity,int capacityIncrement)

public vector(intinitialcapacity)

使用第一种方法,系统会自动对向量对象进行管理。若使用后两种方法,则系统将根据参数initialcapacity设定向量对象的容量(即向量对象可存储数据的大小),当真正存放的数据个数超过容量时,系统会扩充向量对象的储存容量

参数capacityIncrement给定了每次扩充的扩充值。当capacityIncrement为0时,则每次扩充一倍。利用这个功能可以优化存储。在Vector类中提供了各种方法方便用户使用:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值