Vector源码分析

转自:

Vector简介

Vector 是矢量队列,它是JDK1.0版本添加的类。继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口。
Vector 继承了AbstractList,实现了List;所以,它是一个队列,支持相关的添加、删除、修改、遍历等功能
Vector 实现了RandmoAccess接口,即提供了随机访问功能。RandmoAccess是java中用来被List实现,为List提供快速访问功能的。在Vector中,我们即可以通过元素的序号快速获取元素对象;这就是快速随机访问。
Vector 实现了Cloneable接口,即实现clone()函数。它能被克隆。

和ArrayList不同,Vector中的操作是线程安全的;但是,Vector不支持序列化,即没有实现java.io.Serializable接口。

 

Vector的继承关系

  1. java.lang.Object  
  2.        java.util.AbstractCollection<E>  
  3.              java.util.AbstractList<E>  
  4.                    java.util.Vector<E>  
  5.  
  6. public class Vector<E>  
  7.     extends AbstractList<E>  
  8.     implements List<E>, RandomAccess, Cloneable, java.io.Serializable {} 


Vector的构造函数

  1. Vector共有4个构造函数  
  2. // 默认构造函数  
  3. Vector()  
  4.  
  5. // capacity是Vector的默认容量大小。当由于增加数据导致容量增加时,每次容量会增加一倍。  
  6. Vector(int capacity)  
  7.  
  8. // capacity是Vector的默认容量大小,capacityIncrement是每次Vector容量增加时的增量值。  
  9. Vector(int capacity, int capacityIncrement)  
  10.  
  11. // 创建一个包含collection的Vector  
  12. Vector(Collection<? extends E> collection) 

  1. public class Vector<E>  
  2.     extends AbstractList<E>  
  3.     implements List<E>, RandomAccess, Cloneable, java.io.Serializable  
  4. {  
  5.      
  6.     // 保存Vector中数据的数组  
  7.     protected Object[] elementData;  
  8.  
  9.     // 实际数据的数量  
  10.     protected int elementCount;  
  11.  
  12.     // 容量增长系数  
  13.     protected int capacityIncrement;  
  14.  
  15.     // Vector的序列版本号  
  16.     private static final long serialVersionUID = -2767605614048989439L;  
  17.  
  18.     // Vector构造函数。默认容量是10。  
  19.     public Vector() {  
  20.         this(10);  
  21.     }  
  22.  
  23.     // 指定Vector容量大小的构造函数  
  24.     public Vector(int initialCapacity) {  
  25.         this(initialCapacity, 0);  
  26.     }  
  27.  
  28.     // 指定Vector"容量大小"和"增长系数"的构造函数  
  29.     public Vector(int initialCapacity, int capacityIncrement) {  
  30.         super();  
  31.         if (initialCapacity < 0)  
  32.             throw new IllegalArgumentException("Illegal Capacity: "+  
  33.                                                initialCapacity);  
  34.         // 新建一个数组,数组容量是initialCapacity  
  35.         this.elementData = new Object[initialCapacity];  
  36.         // 设置容量增长系数  
  37.         this.capacityIncrement = capacityIncrement;  
  38.     }  
  39.  
  40.     // 指定集合的Vector构造函数。  
  41.     public Vector(Collection<? extends E> c) {  
  42.         // 获取“集合(c)”的数组,并将其赋值给elementData  
  43.         elementData = c.toArray();  
  44.         // 设置数组长度  
  45.         elementCount = elementData.length;  
  46.         // c.toArray might (incorrectly) not return Object[] (see 6260652)  
  47.         if (elementData.getClass() != Object[].class)  
  48.             elementData = Arrays.copyOf(elementData, elementCount, Object[].class);  
  49.     }  
  50.  
  51.     // 将数组Vector的全部元素都拷贝到数组anArray中  
  52.     public synchronized void copyInto(Object[] anArray) {  
  53.         System.arraycopy(elementData, 0, anArray, 0, elementCount);  
  54.     }  
  55.  
  56.     // 将当前容量值设为 =实际元素个数  
  57.     public synchronized void trimToSize() {  
  58.         modCount++;  
  59.         int oldCapacity = elementData.length;  
  60.         if (elementCount < oldCapacity) {  
  61.             elementData = Arrays.copyOf(elementData, elementCount);  
  62.         }  
  63.     }  
  64.  
  65.     // 确认“Vector容量”的帮助函数  
  66.     private void ensureCapacityHelper(int minCapacity) {  
  67.         int oldCapacity = elementData.length;  
  68.         // 当Vector的容量不足以容纳当前的全部元素,增加容量大小。  
  69.         // 若 容量增量系数>0(即capacityIncrement>0),则将容量增大当capacityIncrement  
  70.         // 否则,将容量增大一倍。  
  71.         if (minCapacity > oldCapacity) {  
  72.             Object[] oldData = elementData;  
  73.             int newCapacity = (capacityIncrement > 0) ?  
  74.                 (oldCapacity + capacityIncrement) : (oldCapacity * 2);  
  75.             if (newCapacity < minCapacity) {  
  76.                 newCapacity = minCapacity;  
  77.             }  
  78.             elementData = Arrays.copyOf(elementData, newCapacity);  
  79.         }  
  80.     }  
  81.  
  82.     // 确定Vector的容量。  
  83.     public synchronized void ensureCapacity(int minCapacity) {  
  84.         // 将Vector的改变统计数+1  
  85.         modCount++;  
  86.         ensureCapacityHelper(minCapacity);  
  87.     }  
  88.  
  89.     // 设置容量值为 newSize  
  90.     public synchronized void setSize(int newSize) {  
  91.         modCount++;  
  92.         if (newSize > elementCount) {  
  93.             // 若 "newSize 大于 Vector容量",则调整Vector的大小。  
  94.             ensureCapacityHelper(newSize);  
  95.         } else {  
  96.             // 若 "newSize 小于/等于 Vector容量",则将newSize位置开始的元素都设置为null  
  97.             for (int i = newSize ; i < elementCount ; i++) {  
  98.                 elementData[i] = null;  
  99.             }  
  100.         }  
  101.         elementCount = newSize;  
  102.     }  
  103.  
  104.     // 返回“Vector的总的容量”  
  105.     public synchronized int capacity() {  
  106.         return elementData.length;  
  107.     }  
  108.  
  109.     // 返回“Vector的实际大小”,即Vector中元素个数  
  110.     public synchronized int size() {  
  111.         return elementCount;  
  112.     }  
  113.  
  114.     // 判断Vector是否为空  
  115.     public synchronized boolean isEmpty() {  
  116.         return elementCount == 0;  
  117.     }  
  118.  
  119.     // 返回“Vector中全部元素对应的Enumeration”  
  120.     public Enumeration<E> elements() {  
  121.         // 通过匿名类实现Enumeration  
  122.         return new Enumeration<E>() {  
  123.             int count = 0;  
  124.  
  125.             // 是否存在下一个元素  
  126.             public boolean hasMoreElements() {  
  127.                 return count < elementCount;  
  128.             }  
  129.  
  130.             // 获取下一个元素  
  131.             public E nextElement() {  
  132.                 synchronized (Vector.this) {  
  133.                     if (count < elementCount) {  
  134.                         return (E)elementData[count++];  
  135.                     }  
  136.                 }  
  137.                 throw new NoSuchElementException("Vector Enumeration");  
  138.             }  
  139.         };  
  140.     }  
  141.  
  142.     // 返回Vector中是否包含对象(o)  
  143.     public boolean contains(Object o) {  
  144.         return indexOf(o, 0) >= 0;  
  145.     }  
  146.  
  147.  
  148.     // 从index位置开始向后查找元素(o)。  
  149.     // 若找到,则返回元素的索引值;否则,返回-1  
  150.     public synchronized int indexOf(Object o, int index) {  
  151.         if (o == null) {  
  152.             // 若查找元素为null,则正向找出null元素,并返回它对应的序号  
  153.             for (int i = index ; i < elementCount ; i++)  
  154.             if (elementData[i]==null)  
  155.                 return i;  
  156.         } else {  
  157.             // 若查找元素不为null,则正向找出该元素,并返回它对应的序号  
  158.             for (int i = index ; i < elementCount ; i++)  
  159.             if (o.equals(elementData[i]))  
  160.                 return i;  
  161.         }  
  162.         return -1;  
  163.     }  
  164.  
  165.     // 查找并返回元素(o)在Vector中的索引值  
  166.     public int indexOf(Object o) {  
  167.         return indexOf(o, 0);  
  168.     }  
  169.  
  170.     // 从后向前查找元素(o)。并返回元素的索引  
  171.     public synchronized int lastIndexOf(Object o) {  
  172.         return lastIndexOf(o, elementCount-1);  
  173.     }  
  174.  
  175.     // 从后向前查找元素(o)。开始位置是从前向后的第index个数;  
  176.     // 若找到,则返回元素的“索引值”;否则,返回-1。  
  177.     public synchronized int lastIndexOf(Object o, int index) {  
  178.         if (index >= elementCount)  
  179.             throw new IndexOutOfBoundsException(index + " >= "+ elementCount);  
  180.  
  181.         if (o == null) {  
  182.             // 若查找元素为null,则反向找出null元素,并返回它对应的序号  
  183.             for (int i = index; i >= 0; i--)  
  184.             if (elementData[i]==null)  
  185.                 return i;  
  186.         } else {  
  187.             // 若查找元素不为null,则反向找出该元素,并返回它对应的序号  
  188.             for (int i = index; i >= 0; i--)  
  189.             if (o.equals(elementData[i]))  
  190.                 return i;  
  191.         }  
  192.         return -1;  
  193.     }  
  194.  
  195.     // 返回Vector中index位置的元素。  
  196.     // 若index月结,则抛出异常  
  197.     public synchronized E elementAt(int index) {  
  198.         if (index >= elementCount) {  
  199.             throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);  
  200.         }  
  201.  
  202.         return (E)elementData[index];  
  203.     }  
  204.  
  205.     // 获取Vector中的第一个元素。  
  206.     // 若失败,则抛出异常!  
  207.     public synchronized E firstElement() {  
  208.         if (elementCount == 0) {  
  209.             throw new NoSuchElementException();  
  210.         }  
  211.         return (E)elementData[0];  
  212.     }  
  213.  
  214.     // 获取Vector中的最后一个元素。  
  215.     // 若失败,则抛出异常!  
  216.     public synchronized E lastElement() {  
  217.         if (elementCount == 0) {  
  218.             throw new NoSuchElementException();  
  219.         }  
  220.         return (E)elementData[elementCount - 1];  
  221.     }  
  222.  
  223.     // 设置index位置的元素值为obj  
  224.     public synchronized void setElementAt(E obj, int index) {  
  225.         if (index >= elementCount) {  
  226.             throw new ArrayIndexOutOfBoundsException(index + " >= " +  
  227.                                  elementCount);  
  228.         }  
  229.         elementData[index] = obj;  
  230.     }  
  231.  
  232.     // 删除index位置的元素  
  233.     public synchronized void removeElementAt(int index) {  
  234.         modCount++;  
  235.         if (index >= elementCount) {  
  236.             throw new ArrayIndexOutOfBoundsException(index + " >= " +  
  237.                                  elementCount);  
  238.         } else if (index < 0) {  
  239.             throw new ArrayIndexOutOfBoundsException(index);  
  240.         }  
  241.  
  242.         int j = elementCount - index - 1;  
  243.         if (j > 0) {  
  244.             System.arraycopy(elementData, index + 1, elementData, index, j);  
  245.         }  
  246.         elementCount--;  
  247.         elementData[elementCount] = null/* to let gc do its work */ 
  248.     }  
  249.  
  250.     // 在index位置处插入元素(obj)  
  251.     public synchronized void insertElementAt(E obj, int index) {  
  252.         modCount++;  
  253.         if (index > elementCount) {  
  254.             throw new ArrayIndexOutOfBoundsException(index  
  255.                                  + " > " + elementCount);  
  256.         }  
  257.         ensureCapacityHelper(elementCount + 1);  
  258.         System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);  
  259.         elementData[index] = obj;  
  260.         elementCount++;  
  261.     }  
  262.  
  263.     // 将“元素obj”添加到Vector末尾  
  264.     public synchronized void addElement(E obj) {  
  265.         modCount++;  
  266.         ensureCapacityHelper(elementCount + 1);  
  267.         elementData[elementCount++] = obj;  
  268.     }  
  269.  
  270.     // 在Vector中查找并删除元素obj。  
  271.     // 成功的话,返回true;否则,返回false。  
  272.     public synchronized boolean removeElement(Object obj) {  
  273.         modCount++;  
  274.         int i = indexOf(obj);  
  275.         if (i >= 0) {  
  276.             removeElementAt(i);  
  277.             return true;  
  278.         }  
  279.         return false;  
  280.     }  
  281.  
  282.     // 删除Vector中的全部元素  
  283.     public synchronized void removeAllElements() {  
  284.         modCount++;  
  285.         // 将Vector中的全部元素设为null  
  286.         for (int i = 0; i < elementCount; i++)  
  287.             elementData[i] = null;  
  288.  
  289.         elementCount = 0;  
  290.     }  
  291.  
  292.     // 克隆函数  
  293.     public synchronized Object clone() {  
  294.         try {  
  295.             Vector<E> v = (Vector<E>) super.clone();  
  296.             // 将当前Vector的全部元素拷贝到v中  
  297.             v.elementData = Arrays.copyOf(elementData, elementCount);  
  298.             v.modCount = 0;  
  299.             return v;  
  300.         } catch (CloneNotSupportedException e) {  
  301.             // this shouldn't happen, since we are Cloneable  
  302.             throw new InternalError();  
  303.         }  
  304.     }  
  305.  
  306.     // 返回Object数组  
  307.     public synchronized Object[] toArray() {  
  308.         return Arrays.copyOf(elementData, elementCount);  
  309.     }  
  310.  
  311.     // 返回Vector的模板数组。所谓模板数组,即可以将T设为任意的数据类型  
  312.     public synchronized <T> T[] toArray(T[] a) {  
  313.         // 若数组a的大小 < Vector的元素个数;  
  314.         // 则新建一个T[]数组,数组大小是“Vector的元素个数”,并将“Vector”全部拷贝到新数组中  
  315.         if (a.length < elementCount)  
  316.             return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());  
  317.  
  318.         // 若数组a的大小 >= Vector的元素个数;  
  319.         // 则将Vector的全部元素都拷贝到数组a中。  
  320.     System.arraycopy(elementData, 0, a, 0, elementCount);  
  321.  
  322.         if (a.length > elementCount)  
  323.             a[elementCount] = null;  
  324.  
  325.         return a;  
  326.     }  
  327.  
  328.     // 获取index位置的元素  
  329.     public synchronized E get(int index) {  
  330.         if (index >= elementCount)  
  331.             throw new ArrayIndexOutOfBoundsException(index);  
  332.  
  333.         return (E)elementData[index];  
  334.     }  
  335.  
  336.     // 设置index位置的值为element。并返回index位置的原始值  
  337.     public synchronized E set(int index, E element) {  
  338.         if (index >= elementCount)  
  339.             throw new ArrayIndexOutOfBoundsException(index);  
  340.  
  341.         Object oldValue = elementData[index];  
  342.         elementData[index] = element;  
  343.         return (E)oldValue;  
  344.     }  
  345.  
  346.     // 将“元素e”添加到Vector最后。  
  347.     public synchronized boolean add(E e) {  
  348.         modCount++;  
  349.         ensureCapacityHelper(elementCount + 1);  
  350.         elementData[elementCount++] = e;  
  351.         return true;  
  352.     }  
  353.  
  354.     // 删除Vector中的元素o  
  355.     public boolean remove(Object o) {  
  356.         return removeElement(o);  
  357.     }  
  358.  
  359.     // 在index位置添加元素element  
  360.     public void add(int index, E element) {  
  361.         insertElementAt(element, index);  
  362.     }  
  363.  
  364.     // 删除index位置的元素,并返回index位置的原始值  
  365.     public synchronized E remove(int index) {  
  366.         modCount++;  
  367.         if (index >= elementCount)  
  368.             throw new ArrayIndexOutOfBoundsException(index);  
  369.         Object oldValue = elementData[index];  
  370.  
  371.         int numMoved = elementCount - index - 1;  
  372.         if (numMoved > 0)  
  373.             System.arraycopy(elementData, index+1, elementData, index,  
  374.                      numMoved);  
  375.         elementData[--elementCount] = null// Let gc do its work  
  376.  
  377.         return (E)oldValue;  
  378.     }  
  379.  
  380.     // 清空Vector  
  381.     public void clear() {  
  382.         removeAllElements();  
  383.     }  
  384.  
  385.     // 返回Vector是否包含集合c  
  386.     public synchronized boolean containsAll(Collection<?> c) {  
  387.         return super.containsAll(c);  
  388.     }  
  389.  
  390.     // 将集合c添加到Vector中  
  391.     public synchronized boolean addAll(Collection<? extends E> c) {  
  392.         modCount++;  
  393.         Object[] a = c.toArray();  
  394.         int numNew = a.length;  
  395.         ensureCapacityHelper(elementCount + numNew);  
  396.         // 将集合c的全部元素拷贝到数组elementData中  
  397.         System.arraycopy(a, 0, elementData, elementCount, numNew);  
  398.         elementCount += numNew;  
  399.         return numNew != 0;  
  400.     }  
  401.  
  402.     // 删除集合c的全部元素  
  403.     public synchronized boolean removeAll(Collection<?> c) {  
  404.         return super.removeAll(c);  
  405.     }  
  406.  
  407.     // 删除“非集合c中的元素”  
  408.     public synchronized boolean retainAll(Collection<?> c)  {  
  409.         return super.retainAll(c);  
  410.     }  
  411.  
  412.     // 从index位置开始,将集合c添加到Vector中  
  413.     public synchronized boolean addAll(int index, Collection<? extends E> c) {  
  414.         modCount++;  
  415.         if (index < 0 || index > elementCount)  
  416.             throw new ArrayIndexOutOfBoundsException(index);  
  417.  
  418.         Object[] a = c.toArray();  
  419.         int numNew = a.length;  
  420.         ensureCapacityHelper(elementCount + numNew);  
  421.  
  422.         int numMoved = elementCount - index;  
  423.         if (numMoved > 0)  
  424.         System.arraycopy(elementData, index, elementData, index + numNew, numMoved);  
  425.  
  426.         System.arraycopy(a, 0, elementData, index, numNew);  
  427.         elementCount += numNew;  
  428.         return numNew != 0;  
  429.     }  
  430.  
  431.     // 返回两个对象是否相等  
  432.     public synchronized boolean equals(Object o) {  
  433.         return super.equals(o);  
  434.     }  
  435.  
  436.     // 计算哈希值  
  437.     public synchronized int hashCode() {  
  438.         return super.hashCode();  
  439.     }  
  440.  
  441.     // 调用父类的toString()  
  442.     public synchronized String toString() {  
  443.         return super.toString();  
  444.     }  
  445.  
  446.     // 获取Vector中fromIndex(包括)到toIndex(不包括)的子集  
  447.     public synchronized List<E> subList(int fromIndex, int toIndex) {  
  448.         return Collections.synchronizedList(super.subList(fromIndex, toIndex), this);  
  449.     }  
  450.  
  451.     // 删除Vector中fromIndex到toIndex的元素  
  452.     protected synchronized void removeRange(int fromIndex, int toIndex) {  
  453.         modCount++;  
  454.         int numMoved = elementCount - toIndex;  
  455.         System.arraycopy(elementData, toIndex, elementData, fromIndex,  
  456.                          numMoved);  
  457.  
  458.         // Let gc do its work  
  459.         int newElementCount = elementCount - (toIndex-fromIndex);  
  460.         while (elementCount != newElementCount)  
  461.             elementData[--elementCount] = null;  
  462.     }  
  463.  
  464.     // java.io.Serializable的写入函数  
  465.     private synchronized void writeObject(java.io.ObjectOutputStream s)  
  466.         throws java.io.IOException {  
  467.         s.defaultWriteObject();  
  468.     }  

总结
(01) Vector实际上是通过一个数组去保存数据的。当我们构造Vecotr时;若使用默认构造函数,则Vector的默认容量大小是10
(02) 当Vector容量不足以容纳全部元素时,Vector的容量会增加。若容量增加系数 >0,则将容量的值增加“容量增加系数”;否则,将容量大小增加一倍。
(03) Vector的克隆函数,即是将全部元素克隆到一个数组中。


Vector支持4种遍历方式。建议使用下面的第二种去遍历Vector,因为效率问题。

(01) 第一种,通过迭代器遍历。即通过Iterator去遍历。

  1. Integer value = null;  
  2. int size = vec.size();  
  3. for (int i=0; i<size; i++) {  
  4.     value = (Integer)vec.get(i);          
  5. }  

(02) 第二种,随机访问,通过索引值去遍历。
由于Vector实现了RandomAccess接口,它支持通过索引值去随机访问元素。

  1. Integer value = null;  
  2. int size = vec.size();  
  3. for (int i=0; i<size; i++) {  
  4.     value = (Integer)vec.get(i);          

(03) 第三种,另一种for循环。如下:

  1. Integer value = null;  
  2. for (Integer integ:vec) {  
  3.     value = integ;  

(04) 第四种,Enumeration遍历。如下:

  1. Integer value = null;  
  2. Enumeration enu = vec.elements();  
  3. while (enu.hasMoreElements()) {  
  4.     value = (Integer)enu.nextElement();  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值