JAVA集合容器--Vector

来源:互联网 发布:c语言数组视频 编辑:程序博客网 时间:2024/05/01 22:10


1、Vector简介

(1)Vector也是基于数组实现的,是一个动态数组,其容量能自动增长

(2)Vector是JDK1.0引入了,它的很多实现方法都加入了同步语句,因此是线程安全的(其实也只是相对安全,有些时候还是要加入同步语句来保证线程的安全),可以用于多线程环境。
(3)Vector实现了Serializable接口,因此它支持序列化,实现了Cloneable接口,能被克隆,实现了RandomAccess接口,支持快速随机访问。 

Vector的继承关系:

[java] view plain copy print?
  1. public class Vector<E>  
  2.     extends AbstractList<E>  
  3.     implements List<E>, RandomAccess, Cloneable, java.io.Serializable  

1.Vector 是矢量队列,它是JDK1.0版本添加的类。继承于AbstractList,实现了List, RandomAccess, Cloneable,Serializable这些接口。

2.Vector 继承了AbstractList,实现了List;所以,它是一个队列,支持相关的添加、删除、修改、遍历等功能。

3.Vector 实现了RandmoAccess接口,即提供了随机访问功能。RandmoAccess是java中用来被List实现,为List提供快速访问功能的。在Vector中,我们即可以通过元素的序号快速获取元素对象;这就是快速随机访问。

4.Vector 实现了Cloneable接口,即实现clone()函数。它能被克隆。

5.和ArrayList不同,Vector中的操作是线程安全的;
2、Vector成员变量
[java] view plain copy print?
  1. // 保存Vector中数据的数组      
  2.  protected Object[] elementData;        
  3.  // 实际数据的数量      
  4.  protected int elementCount;        
  5.  // 容量增长系数      
  6.  protected int capacityIncrement;  
3、Vector构造函数
[java] view plain copy print?
  1. // Vector构造函数。默认容量是10。      
  2. public Vector() {      
  3.     this(10);      
  4. }     
  5.   
  6. // 指定Vector容量大小的构造函数      
  7. public Vector(int initialCapacity) {      
  8.     this(initialCapacity, 0);      
  9. }      
  10.   
  11. // 指定Vector"容量大小"和"增长系数"的构造函数      
  12. public Vector(int initialCapacity, int capacityIncrement) {      
  13.     super();      
  14.     if (initialCapacity < 0)      
  15.         throw new IllegalArgumentException("Illegal Capacity: "+      
  16.                                            initialCapacity);      
  17.     // 新建一个数组,数组容量是initialCapacity      
  18.     this.elementData = new Object[initialCapacity];      
  19.     // 设置容量增长系数      
  20.     this.capacityIncrement = capacityIncrement;      
  21. }      
  22.   
  23. // 指定集合的Vector构造函数。      
  24. public Vector(Collection<? extends E> c) {      
  25.     // 获取“集合(c)”的数组,并将其赋值给elementData      
  26.     elementData = c.toArray();      
  27.     // 设置数组长度      
  28.     elementCount = elementData.length;      
  29.     // c.toArray might (incorrectly) not return Object[] (see 6260652)      
  30.     if (elementData.getClass() != Object[].class)      
  31.         elementData = Arrays.copyOf(elementData, elementCount, Object[].class);      
  32. }    
Vector实际上是通过一个数组去保存数据的。当我们构造Vecotr时;若使用默认构造函数,则Vector的默认容量大小是10
4、Vector 常用方法

4.1 add方法

[java] view plain copy print?
  1. // 将“元素e”添加到Vector最后。      
  2. public synchronized boolean add(E e) {      
  3.     modCount++;      
  4.     ensureCapacityHelper(elementCount + 1);      
  5.     elementData[elementCount++] = e;      
  6.     return true;      
  7. }    
  8.   
  9. // 在index位置处插入元素(obj)      
  10. public synchronized void insertElementAt(E obj, int index) {      
  11.     modCount++;      
  12.     if (index > elementCount) {      
  13.         throw new ArrayIndexOutOfBoundsException(index      
  14.                              + " > " + elementCount);      
  15.     }      
  16.     ensureCapacityHelper(elementCount + 1);      
  17.     System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);      
  18.     elementData[index] = obj;      
  19.     elementCount++;      
  20. }   
  21.   
  22. // 将集合c添加到Vector中      
  23. public synchronized boolean addAll(Collection<? extends E> c) {      
  24.     modCount++;      
  25.     Object[] a = c.toArray();      
  26.     int numNew = a.length;      
  27.     ensureCapacityHelper(elementCount + numNew);      
  28.     // 将集合c的全部元素拷贝到数组elementData中      
  29.     System.arraycopy(a, 0, elementData, elementCount, numNew);      
  30.     elementCount += numNew;      
  31.     return numNew != 0;      
  32. }   
  33.   
  34. // 确认“Vector容量”的帮助函数      
  35. private void ensureCapacityHelper(int minCapacity) {      
  36.     int oldCapacity = elementData.length;      
  37.     // 当Vector的容量不足以容纳当前的全部元素,增加容量大小。      
  38.     // 若 容量增量系数>0(即capacityIncrement>0),则将容量增大当capacityIncrement      
  39.     // 否则,将容量增大一倍。      
  40.     if (minCapacity > oldCapacity) {      
  41.         Object[] oldData = elementData;      
  42.         int newCapacity = (capacityIncrement > 0) ?      
  43.             (oldCapacity + capacityIncrement) : (oldCapacity * 2);      
  44.         if (newCapacity < minCapacity) {      
  45.             newCapacity = minCapacity;      
  46.         }      
  47.         elementData = Arrays.copyOf(elementData, newCapacity);      
  48.     }      
  49. }    
当Vector容量不足以容纳全部元素时,Vector的容量会增加。若容量增加系数 >0,则将容量的值增加“容量增加系数”否则,将容量大小增加一倍

4.2 删除方法

[java] view plain copy print?
  1. // 在Vector中查找并删除元素obj。      
  2.  // 成功的话,返回true;否则,返回false。      
  3.  public synchronized boolean removeElement(Object obj) {      
  4.      modCount++;      
  5.      int i = indexOf(obj);      
  6.      if (i >= 0) {      
  7.          removeElementAt(i);      
  8.          return true;      
  9.      }      
  10.      return false;      
  11.  }   
  12.  // 删除index位置的元素      
  13.  public synchronized void removeElementAt(int index) {      
  14.      modCount++;      
  15.      if (index >= elementCount) {      
  16.          throw new ArrayIndexOutOfBoundsException(index + " >= " +      
  17.                               elementCount);      
  18.      } else if (index < 0) {      
  19.          throw new ArrayIndexOutOfBoundsException(index);      
  20.      }      
  21.      int j = elementCount - index - 1;      
  22.      if (j > 0) {      
  23.          System.arraycopy(elementData, index + 1, elementData, index, j);      
  24.      }      
  25.      elementCount--;      
  26.      elementData[elementCount] = null/* to let gc do its work */     
  27.  }   

4.3  查找方法

[java] view plain copy print?
  1. // 返回“Vector中全部元素对应的Enumeration”      
  2.  public Enumeration<E> elements() {      
  3.      // 通过匿名类实现Enumeration      
  4.      return new Enumeration<E>() {      
  5.          int count = 0;      
  6.   
  7.          // 是否存在下一个元素      
  8.          public boolean hasMoreElements() {      
  9.              return count < elementCount;      
  10.          }      
  11.   
  12.          // 获取下一个元素      
  13.          public E nextElement() {      
  14.              synchronized (Vector.this) {      
  15.                  if (count < elementCount) {      
  16.                      return (E)elementData[count++];      
  17.                  }      
  18.              }      
  19.              throw new NoSuchElementException("Vector Enumeration");      
  20.          }      
  21.      };      
  22.  }   
  23.   
  24.  // 从index位置开始向后查找元素(o)。      
  25.  // 若找到,则返回元素的索引值;否则,返回-1      
  26.  public synchronized int indexOf(Object o, int index) {      
  27.      if (o == null) {      
  28.          // 若查找元素为null,则正向找出null元素,并返回它对应的序号      
  29.          for (int i = index ; i < elementCount ; i++)      
  30.          if (elementData[i]==null)      
  31.              return i;      
  32.      } else {      
  33.          // 若查找元素不为null,则正向找出该元素,并返回它对应的序号      
  34.          for (int i = index ; i < elementCount ; i++)      
  35.          if (o.equals(elementData[i]))      
  36.              return i;      
  37.      }      
  38.      return -1;      
  39.  }    
  40.   
  41.  // 返回Vector中index位置的元素。      
  42.  // 若index月结,则抛出异常      
  43.  public synchronized E elementAt(int index) {      
  44.      if (index >= elementCount) {      
  45.          throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);      
  46.      }      
  47.   
  48.      return (E)elementData[index];      
  49.  }    
4.4 修改方法

[java] view plain copy print?
  1. // 设置index位置的元素值为obj      
  2. public synchronized void setElementAt(E obj, int index) {      
  3.     if (index >= elementCount) {      
  4.         throw new ArrayIndexOutOfBoundsException(index + " >= " +      
  5.                              elementCount);      
  6.     }      
  7.     elementData[index] = obj;      
  8. }    
  9.   
  10. // 设置index位置的值为element。并返回index位置的原始值      
  11. public synchronized E set(int index, E element) {      
  12.     if (index >= elementCount)      
  13.         throw new ArrayIndexOutOfBoundsException(index);      
  14.   
  15.     Object oldValue = elementData[index];      
  16.     elementData[index] = element;      
  17.     return (E)oldValue;      
  18. }  

4.5 clone()

[java] view plain copy print?
  1. // 克隆函数      
  2. public synchronized Object clone() {      
  3.     try {      
  4.         Vector<E> v = (Vector<E>) super.clone();      
  5.         // 将当前Vector的全部元素拷贝到v中      
  6.         v.elementData = Arrays.copyOf(elementData, elementCount);      
  7.         v.modCount = 0;      
  8.         return v;      
  9.     } catch (CloneNotSupportedException e) {      
  10.         // this shouldn't happen, since we are Cloneable      
  11.         throw new InternalError();      
  12.     }      
  13. }   
Vector的克隆函数,即是将全部元素克隆到一个数组中。

5、Vector遍历方式

Vector支持4种遍历方式:

第一种:通过迭代器遍历。即通过Iterator去遍历。

[java] view plain copy print?
  1. Integer value = null;  
  2. int size = vec.size();  
  3. for (int i=0; i<size; i++) {  
  4.     value = (Integer)vec.get(i);          
  5. }  
第二种:随机访问,通过索引值去遍历。由于Vector实现了RandomAccess接口,它支持通过索引值去随机访问元素。
[java] view plain copy print?
  1. Integer value = null;  
  2. int size = vec.size();  
  3. for (int i=0; i<size; i++) {  
  4.     value = (Integer)vec.get(i);          
  5. }  
第三种:另一种for循环。如下:
[java] view plain copy print?
  1. Integer value = null;  
  2. for (Integer integ:vec) {  
  3.     value = integ;  
  4. }  

第四种:Enumeration遍历。如下:

[java] view plain copy print?
  1. Integer value = null;  
  2. Enumeration enu = vec.elements();  
  3. while (enu.hasMoreElements()) {  
  4.     value = (Integer)enu.nextElement();  
  5. }  
6、总结

1、Vector有四个不同的构造方法。无参构造方法的容量为默认值10,仅包含容量的构造方法则将容量增长量(从源码中可以看出容量增长量的作用,第二点也会对容量增长量详细说)设置为0。

2、注意扩充容量的方法ensureCapacityHelper。与ArrayList相同,Vector在每次增加元素(可能是1个,也可能是一组)时,都要调用该方法来确保足够的容量。当容量不足以容纳当前的元素个数时,就先看构造方法中传入的容量增长量参数CapacityIncrement是否为0,如果不为0,就设置新的容量为旧容量加上容量增长量如果为0,就设置新的容量为旧的容量的2倍,如果设置后的新容量还不够,则直接新容量设置为传入的参数(也就是所需的容量),而后同样用Arrays.copyof()方法将元素拷贝到新的数组。

3、很多方法都加入了synchronized同步语句,来保证线程安全

4、同样在查找给定元素索引值等的方法中,源码都将该元素的值分为null和不为null两种情况处理,Vector中也允许元素为null


5、其他很多地方都与ArrayList实现大同小异,Vector现在已经基本不再使用。

0 0