【小笨鸟看JDK1.7集合源码之四】Vector源码剖析

Vector 简介

(1)Vector类也是以数组结构为基础,可以直接使用数组索引进行访问,但是它具有可自由增长的特性;

(2)实现了RandomAccess支持随机访问,Cloneable可以实现克隆,Serializable能够被序列化;

(3)Vector其实与ArrayList功能类似,但是加入了很多同步语句,因此是线程安全的,适用于多线程环境;但是因为加了同步之后效率会相应降低。

JDK1.7-LinkedList源码详细分析

  1 public class Vector<E>
  2     extends AbstractList<E>
  3     implements List<E>, RandomAccess, Cloneable, java.io.Serializable
  4 {
  5     /**
  6      * 数组缓冲区,用于存放vector中的元素
  7      */
  8     protected Object[] elementData;
  9 
 10     /**
 11      *元素个数,Vector 对象中的有效组件数。从 elementData[0] 到 elementData[elementCount-1] 
 12      的组件均为实际项。 
 13      */
 14     protected int elementCount;
 15 
 16     /**
 17      *向量的大小大于其容量时,容量自动增加的量。
 18      如果容量的增量小于等于零,则每次需要增大容量时,向量的容量将增大一倍,
 19      代表的是一个变化的增量,是在扩容时计算;
 20      */
 21     protected int capacityIncrement;
 22 
 23     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 24     private static final long serialVersionUID = -2767605614048989439L;
 25     
 26     /**
 27     构造函数
 28     initialCapacity:初始容量
 29     capacityIncrement:容量的增量
 30     */
 31     public Vector(int initialCapacity, int capacityIncrement) {
 32         super();
 33         if (initialCapacity < 0)
 34             throw new IllegalArgumentException("Illegal Capacity: "+
 35                                                initialCapacity);
 36         this.elementData = new Object[initialCapacity];//初始化
 37         this.capacityIncrement = capacityIncrement;
 38     }
 39     //不带增量的构造函数
 40     public Vector(int initialCapacity) {
 41         this(initialCapacity, 0);
 42     }
 43     //无参构造函数,默认初始化容量为10,标准容量增量为0
 44     public Vector() {
 45         this(10);
 46     }
 47     /**构造一个包含指定collection中的元素的向量vector*/
 48     public Vector(Collection<? extends E> c) {
 49         elementData = c.toArray();//获取元素数组
 50         elementCount = elementData.length;//数组长度
 51         // c.toArray might (incorrectly) not return Object[] (see 6260652)
 52         //可能返回的是非数组的对象
 53         if (elementData.getClass() != Object[].class)//如果非数组类型的对象
 54             //重新复制构造一个新的数组
 55             elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
 56     }
 57     /**将向量复制到指定的参数数组中,此方法是同步的
 58         复制默认从第一个元素开始,长度为vector中的元素个数,全部复制到参数anArray中;
 59         如果参数数组为空或者容量不够、存储类型异常,会抛出相应异常
 60     */
 61     public synchronized void copyInto(Object[] anArray) {
 62         System.arraycopy(elementData, 0, anArray, 0, elementCount);
 63     }
 64     /**
 65      * 调整向量的实际容量,如果当前向量容量大于其当前大小,则通过复制数组的方式将
 66      容量更改为当前的实际大小。其实就是根据当前容量与向量元素的实际个数不等时作出相应的
 67      调整;
 68      */
 69     public synchronized void trimToSize() {
 70         modCount++;
 71         int oldCapacity = elementData.length;
 72         if (elementCount < oldCapacity) {
 73             elementData = Arrays.copyOf(elementData, elementCount);
 74         }
 75     }
 76     /**
 77      * 确认容量,是否能容纳指定参数大小的容量
 78      */
 79     public synchronized void ensureCapacity(int minCapacity) {
 80         if (minCapacity > 0) {
 81             modCount++;
 82             ensureCapacityHelper(minCapacity);
 83         }
 84     }
 85     /**
 86      * 确认是否要扩容,如果指定参数大于当前的实际容量,则直接扩容,以参数为默认大小。
 87      */
 88     private void ensureCapacityHelper(int minCapacity) {
 89         // overflow-conscious code
 90         if (minCapacity - elementData.length > 0)
 91             grow(minCapacity);
 92     }
 93     /**
 94      默认的最大的数组容量为Integer.MAX_VALUE - 8,不能超过此容量,不然JVM会抛出OutOfMemoryError;
 95      那8个字节主要是VM保留的字节,用于表示数组的长度,在JVM解析中。
 96      */
 97     private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
 98     
 99     //扩容
100     private void grow(int minCapacity) {
101         // overflow-conscious code
102         int oldCapacity = elementData.length;//当前数组的长度
103         //如果指定了增量且大于0,则直接以增量大小扩容,否则增量取当前数组的长度,即直接增加到原来的两倍
104         int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
105                                          capacityIncrement : oldCapacity);
106         if (newCapacity - minCapacity < 0)//如果扩容后长度还是小于指定的最小参数minCapacity
107             newCapacity = minCapacity;//则以minCapacity参数大小扩容
108         if (newCapacity - MAX_ARRAY_SIZE > 0)//主要是为了防止内存溢出
109             newCapacity = hugeCapacity(minCapacity);
110         elementData = Arrays.copyOf(elementData, newCapacity);
111     }
112     //防止超出最大的数组范围,如果超出默认取最大值
113     private static int hugeCapacity(int minCapacity) {
114         if (minCapacity < 0) // overflow
115             throw new OutOfMemoryError();
116         return (minCapacity > MAX_ARRAY_SIZE) ?
117             Integer.MAX_VALUE :
118             MAX_ARRAY_SIZE;
119     }
120     /**
121      * 设置向量的大小
122      (1)如果设置的参数值大于向量的大小,则先扩容,再在后面补齐相应数量的null项
123      (2)如果设置的参数值小于向量的大小,则直接将多余的长度丢弃,后面的项也直接设置为null
124      */
125     public synchronized void setSize(int newSize) {
126         modCount++;
127         if (newSize > elementCount) {
128             ensureCapacityHelper(newSize);
129         } else {
130             for (int i = newSize ; i < elementCount ; i++) {
131                 elementData[i] = null;
132             }
133         }
134         elementCount = newSize;//更新长度,后面的null值项直接被回收
135     }
136     /**
137      *返回此向量的当前容量,也就是实际数组的长度
138      */
139     public synchronized int capacity() {
140         return elementData.length;
141     }
142     /**
143      * 返回此向量中的组件数
144      */
145     public synchronized int size() {
146         return elementCount;
147     }
148     /**
149      * 是否为空
150      */
151     public synchronized boolean isEmpty() {
152         return elementCount == 0;
153     }
154     /**
155      * 返回此向量的组件的枚举。返回的 Enumeration 对象将生成此向量中的所有项。
156      生成的第一项为索引 0 处的项,然后是索引 1 处的项,依此类推。 
157      */
158     public Enumeration<E> elements() {
159         return new Enumeration<E>() {
160             int count = 0;
161 
162             public boolean hasMoreElements() {
163                 return count < elementCount;
164             }
165 
166             public E nextElement() {
167                 synchronized (Vector.this) {
168                     if (count < elementCount) {
169                         return elementData(count++);
170                     }
171                 }
172                 throw new NoSuchElementException("Vector Enumeration");
173             }
174         };
175     }
176     /**
177      * 是否包含某个指定元素,此元素可以是null值
178      */
179     public boolean contains(Object o) {
180         return indexOf(o, 0) >= 0;
181     }
182     /**
183      * 返回指定元素第一次出现的索引,如果不包含返回-1
184      */
185     public int indexOf(Object o) {
186         return indexOf(o, 0);
187     }
188     /**
189      * 返回指定元素第一次出现的索引,从指定索引index开始查找,如果不包含返回-1;
190      注意一下,null值与普通值也是分开查找的
191      */
192     public synchronized int indexOf(Object o, int index) {
193         if (o == null) {
194             for (int i = index ; i < elementCount ; i++)
195                 if (elementData[i]==null)
196                     return i;
197         } else {
198             for (int i = index ; i < elementCount ; i++)
199                 if (o.equals(elementData[i]))
200                     return i;
201         }
202         return -1;
203     }
204     /**
205      *反向查找指定元素出现的第一次索引位置
206      */
207     public synchronized int lastIndexOf(Object o) {
208         return lastIndexOf(o, elementCount-1);//默认参数是最后一个元素的索引
209     }
210     /**
211      * 反向查找指定元素出现的第一次索引位置,从指定索引index处开始
212      */
213     public synchronized int lastIndexOf(Object o, int index) {
214         if (index >= elementCount)
215             throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
216         //也是将null值与普通元素分开查询
217         if (o == null) {
218             for (int i = index; i >= 0; i--)
219                 if (elementData[i]==null)
220                     return i;
221         } else {
222             for (int i = index; i >= 0; i--)
223                 if (o.equals(elementData[i]))
224                     return i;
225         }
226         return -1;
227     }
228     /**
229      * 返回指定索引处的元素
230      */
231     public synchronized E elementAt(int index) {
232         if (index >= elementCount) {
233             throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
234         }
235 
236         return elementData(index);
237     }
238     /**
239      * 返回第一个元素组件
240      */
241     public synchronized E firstElement() {
242         if (elementCount == 0) {
243             throw new NoSuchElementException();
244         }
245         return elementData(0);
246     }
247     /**
248      * 返回最后一个元素组件
249      */
250     public synchronized E lastElement() {
251         if (elementCount == 0) {
252             throw new NoSuchElementException();
253         }
254         return elementData(elementCount - 1);
255     }
256 
257     /**
258      * 在指定的索引位置index处设置指定的新的值obj
259      */
260     public synchronized void setElementAt(E obj, int index) {
261         if (index >= elementCount) {
262             throw new ArrayIndexOutOfBoundsException(index + " >= " +
263                                                      elementCount);
264         }
265         elementData[index] = obj;
266     }
267 
268     /**
269      * 删除指定索引位置的组件。
270      */
271     public synchronized void removeElementAt(int index) {
272         modCount++;
273         if (index >= elementCount) {
274             throw new ArrayIndexOutOfBoundsException(index + " >= " +
275                                                      elementCount);
276         }
277         else if (index < 0) {
278             throw new ArrayIndexOutOfBoundsException(index);
279         }
280         int j = elementCount - index - 1;
281         if (j > 0) {//执行数组复制移位覆盖
282         /*假设数组元素为1-2-3-4-5,index=2,即从索引下标2移除,则elementCount=5,j=2
283         (1)elementData从index+1处开始取j个元素,即4-5
284         (2)从elementData指定索引index=2处开始覆盖,即1-2-4-5-5
285         (3)将最后的元素设置为null,被GC回收
286         */
287             System.arraycopy(elementData, index + 1, elementData, index, j);
288         }
289         elementCount--;//更新长度
290         elementData[elementCount] = null; /* to let gc do its work */
291     }
292 
293     /**
294      * 在指定索引处插入一个指定obj组件
295      */
296     public synchronized void insertElementAt(E obj, int index) {
297         modCount++;
298         if (index > elementCount) {
299             throw new ArrayIndexOutOfBoundsException(index
300                                                      + " > " + elementCount);
301         }
302         //先扩容,增量为1
303         ensureCapacityHelper(elementCount + 1);
304         //执行数组复制移位覆盖,index处为null值
305         System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
306         elementData[index] = obj;//给index处赋值
307         elementCount++;//更新长度
308     }
309 
310     /**
311      * 在末尾添加指定的组件
312      */
313     public synchronized void addElement(E obj) {
314         modCount++;
315         ensureCapacityHelper(elementCount + 1);//先扩容,增量为1
316         elementData[elementCount++] = obj;//直接在末尾添加一个元素,并且赋值
317     }
318 
319     /**
320      * 删除指定值的组件
321      */
322     public synchronized boolean removeElement(Object obj) {
323         modCount++;
324         int i = indexOf(obj);//先找到指定值的索引位置
325         if (i >= 0) {//如果存在,则直接调用删除指定索引位置方法删除
326             removeElementAt(i);
327             return true;
328         }
329         return false;
330     }
331 
332     /**
333      * 移除所有的组件
334      */
335     public synchronized void removeAllElements() {
336         modCount++;
337         // Let gc do its work,也就是将所有的元素都设置为null,之后由GC回收掉
338         for (int i = 0; i < elementCount; i++)
339             elementData[i] = null;
340 
341         elementCount = 0;
342     }
343 
344     /**
345      * 返回向量的一个副本。副本中将包含一个对内部数据数组副本的引用,
346      而非对此 Vector 对象的原始内部数据数组的引用。
347      */
348     public synchronized Object clone() {
349         try {
350             @SuppressWarnings("unchecked")
351                 Vector<E> v = (Vector<E>) super.clone();//产生一个新的vector对象
352                 //内部实际上是复制产生一个新的数组,将新的对象指向这个数组
353             v.elementData = Arrays.copyOf(elementData, elementCount);
354             v.modCount = 0;
355             return v;
356         } catch (CloneNotSupportedException e) {
357             // this shouldn't happen, since we are Cloneable
358             throw new InternalError();
359         }
360     }
361 
362     /**
363      * 返回一个数组,包含顺序存放的所有元素
364      */
365     public synchronized Object[] toArray() {
366         return Arrays.copyOf(elementData, elementCount);
367     }
368 
369     /**
370      * 运行时指定返回的数组类型
371      1、如果给定的数组长度小于向量中元素的个数,则返回一个复制的数组,类型为指定的类型,数组后面用null值填充;
372      2、如果给定的数组长度大于向量中元素的个数,则直接全部复制到数组中;
373      */
374     @SuppressWarnings("unchecked")
375     public synchronized <T> T[] toArray(T[] a) {
376         if (a.length < elementCount)//1
377             return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
378 
379         System.arraycopy(elementData, 0, a, 0, elementCount);//2
380 
381         if (a.length > elementCount)//填充null值
382             a[elementCount] = null;
383 
384         return a;
385     }
386 
387     // 返回指定位置的组件,其实与get(int)方法功能完全相同
388     @SuppressWarnings("unchecked")
389     E elementData(int index) {
390         return (E) elementData[index];
391     }
392 
393     /**
394      * 返回指定位置的元素
395      */
396     public synchronized E get(int index) {
397         if (index >= elementCount)
398             throw new ArrayIndexOutOfBoundsException(index);
399 
400         return elementData(index);
401     }
402 
403     /**
404      * 给指定索引index处设置指定值element
405      */
406     public synchronized E set(int index, E element) {
407         if (index >= elementCount)
408             throw new ArrayIndexOutOfBoundsException(index);
409 
410         E oldValue = elementData(index);
411         elementData[index] = element;//覆盖旧的值
412         return oldValue;//返回旧的值
413     }
414 
415     /**
416      * 在向量尾部添加一个指定元素e
417      */
418     public synchronized boolean add(E e) {
419         modCount++;
420         ensureCapacityHelper(elementCount + 1);//先扩容,增量为1
421         elementData[elementCount++] = e;//赋值
422         return true;
423     }
424 
425     /**
426      * 从向量头部开始移除匹配到的第一个指定元素o
427      */
428     public boolean remove(Object o) {
429         return removeElement(o);
430     }
431 
432     /**
433      * 在指定的索引位置index处添加指定新的值element
434      */
435     public void add(int index, E element) {
436         insertElementAt(element, index);
437     }
438 
439     /**
440      * 移除指定索引index处的元素,并且返回此元素
441      */
442     public synchronized E remove(int index) {
443         modCount++;
444         if (index >= elementCount)
445             throw new ArrayIndexOutOfBoundsException(index);
446         E oldValue = elementData(index);
447 
448         int numMoved = elementCount - index - 1;
449         if (numMoved > 0)
450             //实际上就是将index的后面元素集体往前面移动一个位置覆盖
451             System.arraycopy(elementData, index+1, elementData, index,
452                              numMoved);
453         elementData[--elementCount] = null; //再将最后一个元素值null回收掉
454 
455         return oldValue;
456     }
457 
458     /**
459      * 移除向量中所有的元素
460      */
461     public void clear() {
462         removeAllElements();
463     }
464 
465     /**
466      * 此向量是否包含指定组件的所有元素
467      */
468     public synchronized boolean containsAll(Collection<?> c) {
469         return super.containsAll(c);
470     }
471 
472     /**
473      * 将指定容器中的元素添加到此向量的尾部
474      */
475     public synchronized boolean addAll(Collection<? extends E> c) {
476         modCount++;
477         Object[] a = c.toArray();
478         int numNew = a.length;
479         ensureCapacityHelper(elementCount + numNew);//以参数中的元素长度为增量扩容
480         System.arraycopy(a, 0, elementData, elementCount, numNew);//产生新的数组
481         elementCount += numNew;//更新向量中元素的个数
482         return numNew != 0;
483     }
484 
485     /**
486      *移除向量中所有的元素
487      */
488     public synchronized boolean removeAll(Collection<?> c) {
489         return super.removeAll(c);
490     }
491 
492     /**
493      *只保留指定容器中包含的元素
494      */
495     public synchronized boolean retainAll(Collection<?> c) {
496         return super.retainAll(c);
497     }
498 
499     /**
500      *从指定的索引index处,将指定容器c中的所有元素插入到此向量中
501      */
502     public synchronized boolean addAll(int index, Collection<? extends E> c) {
503         modCount++;
504         if (index < 0 || index > elementCount)
505             throw new ArrayIndexOutOfBoundsException(index);
506 
507         Object[] a = c.toArray();
508         int numNew = a.length;
509         ensureCapacityHelper(elementCount + numNew);//先扩容,增量为指定容器中元素的长度
510 
511         int numMoved = elementCount - index;
512         if (numMoved > 0)//如果插入位置没有超过向量元素索引下标
513             System.arraycopy(elementData, index, elementData, index + numNew,
514                              numMoved);
515         //如果插入位置刚好在尾部
516         System.arraycopy(a, 0, elementData, index, numNew);
517         elementCount += numNew;
518         return numNew != 0;
519     }
520 
521     /**
522      * 比较指定对象与此向量的相等性。当且仅当指定的对象也是一个 List、两个 List 大小相同,
523      并且其中所有对应的元素对都相等 时才返回 true。(如果 (e1==null ? e2==null : e1.equals(e2)),
524      则两个元素 e1 和 e2 相等)。
525      换句话说,如果两个 List 包含相同顺序的相同元素,则这两个 List 就定义为相等。
526      */
527     public synchronized boolean equals(Object o) {
528         return super.equals(o);
529     }
530 
531     /**
532      * 返回此向量的hash值
533      */
534     public synchronized int hashCode() {
535         return super.hashCode();
536     }
537 
538     /**
539      * 返回此向量的字符串表示形式,其中包含每个元素的String表示形式
540      */
541     public synchronized String toString() {
542         return super.toString();
543     }
544 
545     /**
546      *返回此 List 的部分视图,元素范围为从 fromIndex(包括)到 toIndex(不包括)。
547      */
548     public synchronized List<E> subList(int fromIndex, int toIndex) {
549         return Collections.synchronizedList(super.subList(fromIndex, toIndex),
550                                             this);
551     }
552 
553     /**
554      * 移除向量中指定参数范围内的所有元素,元素范围为从 fromIndex(包括)到 toIndex(不包括)
555      */
556     protected synchronized void removeRange(int fromIndex, int toIndex) {
557         modCount++;
558         int numMoved = elementCount - toIndex;//如果numMoved小于0,会直接抛出异常
559         /**还是实际例子说明:向量中初始元素为 1,2,3,4,5,6 elementCount=6,
560         参数假设fromIndex=2,toIndex=4 则numMoved = 2;
561         (1)从toIndex=4(包含)开始取长度为numMoved = 2数组,即5,6
562         (2)从fromIndex=2(包含)开始用(1)中的数组覆盖,即1,2,5,6,5,6
563         (3)将最后的5,6设置为null,回收,实现了范围删除
564         */
565         System.arraycopy(elementData, toIndex, elementData, fromIndex,
566                          numMoved);
567 
568         // Let gc do its work
569         int newElementCount = elementCount - (toIndex-fromIndex);
570         while (elementCount != newElementCount)
571             elementData[--elementCount] = null;//将最后的元素设置为null,被GC回收
572     }
573 
574     /**
575      * 序列化写入函数
576      */
577     private void writeObject(java.io.ObjectOutputStream s)
578             throws java.io.IOException {
579         final java.io.ObjectOutputStream.PutField fields = s.putFields();
580         final Object[] data;
581         synchronized (this) {
582             fields.put("capacityIncrement", capacityIncrement);
583             fields.put("elementCount", elementCount);
584             data = elementData.clone();
585         }
586         fields.put("elementData", data);
587         s.writeFields();
588     }
589 
590     /**
591      * 迭代
592      */
593     public synchronized ListIterator<E> listIterator(int index) {
594         if (index < 0 || index > elementCount)
595             throw new IndexOutOfBoundsException("Index: "+index);
596         return new ListItr(index);
597     }
598 
599     /**
600      * Returns a list iterator over the elements in this list (in proper
601      * sequence).
602      *
603      * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
604      *
605      * @see #listIterator(int)
606      */
607     public synchronized ListIterator<E> listIterator() {
608         return new ListItr(0);
609     }
610 
611     public synchronized Iterator<E> iterator() {
612         return new Itr();
613     }
614 
615     private class Itr implements Iterator<E> {
616         int cursor;       // 下一个要迭代元素的索引
617         int lastRet = -1; // 上一个元素的索引位置,lastRet永远比cursor小1,cursor位于开始位置0时,lastRet初始化为-1
618         int expectedModCount = modCount;
619 
620         public boolean hasNext() {
621             // 向量中后序是否还有元素可以迭代
622             return cursor != elementCount;
623         }
624 
625         public E next() {//同步的迭代下一个元素
626             synchronized (Vector.this) {
627                 checkForComodification();
628                 int i = cursor;
629                 if (i >= elementCount)
630                     throw new NoSuchElementException();
631                 cursor = i + 1;
632                 return elementData(lastRet = i);//每次迭代后更新前一个索引
633             }
634         }
635 
636         public void remove() {//移除前一个元素
637             if (lastRet == -1)
638                 throw new IllegalStateException();
639             synchronized (Vector.this) {
640                 checkForComodification();
641                 Vector.this.remove(lastRet);
642                 expectedModCount = modCount;
643             }
644             cursor = lastRet;
645             lastRet = -1;
646         }
647 
648         final void checkForComodification() {
649             if (modCount != expectedModCount)
650                 throw new ConcurrentModificationException();
651         }
652     }
653 
654     /**
655      * An optimized version of AbstractList.ListItr
656      */
657     final class ListItr extends Itr implements ListIterator<E> {
658         ListItr(int index) {
659             super();
660             cursor = index;//int cursor 下一个要迭代元素的索引
661         }
662 
663         public boolean hasPrevious() {//当前迭代的元素前面是否有元素,即是否为第一个元素
664             return cursor != 0;
665         }
666 
667         public int nextIndex() {//下一个要迭代元素的索引
668             return cursor;
669         }
670 
671         public int previousIndex() {//返回当前迭代元素的前一个元素的索引
672             return cursor - 1;
673         }
674 
675         public E previous() {//返回当前迭代元素的前一个元素
676             synchronized (Vector.this) {
677                 checkForComodification();
678                 int i = cursor - 1;
679                 if (i < 0)
680                     throw new NoSuchElementException();
681                 cursor = i;
682                 return elementData(lastRet = i);
683             }
684         }
685 
686         public void set(E e) {//在当前迭代的前面索引处设置指定元素值
687             if (lastRet == -1)
688                 throw new IllegalStateException();
689             synchronized (Vector.this) {
690                 checkForComodification();
691                 Vector.this.set(lastRet, e);//实际上内部调用的是设置元素,在指定位置
692             }
693         }
694 
695         public void add(E e) {//添加当前迭代的位置插入指定元素
696             int i = cursor;
697             synchronized (Vector.this) {
698                 checkForComodification();
699                 Vector.this.add(i, e);//实际上内部调用的是插入元素,在i位置
700                 expectedModCount = modCount;
701             }
702             cursor = i + 1;
703             lastRet = -1;
704         }
705     }
706 }

总结以下:

1、无参构造函数,默认初始化容量为10,标准容量增量为0;

2、执行扩容时,如果指定了增量且大于0,则直接以增量大小扩容,否则增量取当前数组的长度,即直接增加到原来的两倍;

3、默认的最大的数组容量为Integer.MAX_VALUE - 8,不能超过此容量,不然JVM会抛出OutOfMemoryError;那8个字节主要是VM保留的字节,用于表示数组的长度,在JVM解析中。

4、设置向量的大小时:

(1)如果设置的参数值大于向量的大小,则先扩容,再在后面补齐相应数量的null项;
(2)如果设置的参数值小于向量的大小,则直接将多余的长度丢弃,后面的项也直接设置为null。

5、在执行查找时,null值元素与非空元素也是分开遍历查找;

 

posted @ 2016-04-30 17:00  CfoolishbirdC  阅读(204)  评论(0编辑  收藏  举报