一、顶部注释分析
Vector是一个 growable 的数组,它的大小可以根据需要增加或减少 Vector is synchronized. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector,即Vector 和 ArrayList 的最大的不同是 Vector 是线程安全的 ,但是如果不需要线程安全,还是推荐使用 ArrayList 提高效率
二、源码分析
2.1 定义
public class Vector < E> extends AbstractList < E>
implements List < E> , RandomAccess, Cloneable, java. io. Serializable
与 ArrayList 相同,继承自 AbstractList,实现 List 接口,可随机访问,可 clone,可序列化
2.2 字段
protected Object[ ] elementData;
protected int elementCount;
protected int capacityIncrement;
private static final int MAX_ARRAY_SIZE = Integer. MAX_VALUE - 8 ;
2.3 构造方法
Vector(int initialCapacity, int capacityIncrement)
:构造一个指定容量为capacity、自增容量为capacityIncrement 的空vectorVector(int initialCapacity)
:构造一个指定容量为initialCapacity、自增容量为0的空vectorVector()
:调用方法2 构造一个指定容量为10、自增容量为0的空vector,即 Vector(10)
Vector(Collection<? extends E> c)
:使用指定的 Collection 构造vector
2.4 常用方法
Vector的常用方法实现与 ArrayList 有很多相似之处,如 add、set、get、remove等,最大的区别在于 Vector 中大多数方法都是线程安全的,即使用了 synchronized 关键字,以 add 为例:
public synchronized boolean add ( E e)
{
modCount++ ;
ensureCapacityHelper ( elementCount + 1 ) ;
elementData[ elementCount++ ] = e;
return true ;
}
2.5 扩容
该方法的实现和 ArrayList 中大致相同,不同的是在第一次扩容时,vector的逻辑是:
newCapacity = oldCapacity + ( ( capacityIncrement > 0 ) ? capacityIncrement : oldCapacity) ;
即如果 capacityIncrement > 0,就加 capacityIncrement,如果不是就增加一倍 ; ArrayList则是增加现有的一半
private void grow ( int minCapacity)
{
int oldCapacity = elementData. length;
int newCapacity = oldCapacity + ( ( capacityIncrement > 0 ) ?
capacityIncrement : oldCapacity) ;
if ( newCapacity - minCapacity < 0 )
newCapacity = minCapacity;
if ( newCapacity - MAX_ARRAY_SIZE > 0 )
newCapacity = hugeCapacity ( minCapacity) ;
elementData = Arrays. copyOf ( elementData, newCapacity) ;
}