声明:以下内容均基于JDK1.8
Vector类图
ArrayList是java.util包下的Collection接口下的一个子类,是一个支持自动扩容的线程安全的Object数组,和ArrayList十分类似,可以看成是ArrayList的线程安全版本。
- AbstractList抽象类:List的抽象父类,实现一些List通用的方法。
- Cloneable接口:实现对象拷贝必须要实现的接口。
- Serializable接口:实现序列化必须要实现的接口。
- RandomAccess接口:用于定义数组实现随机访问的算法,ArrayList和Vector都实现了该接口。
Vector类属性
以上便是Vector的属性,下面我们一一分析:
elementData
protected Object[] elementData;
Vector实际存储数据的地方,一个Object数组
elementCount
protected int elementCount;
数组中元素的个数
capacityIncrement
protected int capacityIncrement;
每次扩容时增加的容量大小
serialVersionUID
private static final long serialVersionUID = -2767605614048989439L;
序列化ID
MAX_ARRAY_SIZE
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
可以扩容的最大容量
Vector常用API
public Vector() {
this(10);
}
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
public Vector(Collection<? extends E> c) {
elementData = c.toArray();
elementCount = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}
Vector的构造函数
- 空参:默认初始化容量为10
- 一个参数:指定初始化的容量大小
- 两个参数:指定初始化容量大小和每次扩容时增大的容量
- 首先调用父类的构造函数
- 判断输入的初始化容量是否合理,如果小于0则抛出异常
- 创建一个指定容量的Object数组,同时将初始容量和每次扩容时增大的容量属性保存
- Collection参数,将Collection集合转换为Vector集合,调用Arrays.copyOf进行转换
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
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);
}
public void add(int index, E element) {
insertElementAt(element, index);
}
public synchronized void insertElementAt(E obj, int index) {
modCount++;
if (index > elementCount) {
throw new ArrayIndexOutOfBoundsException(index
+ " > " + elementCount);
}
ensureCapacityHelper(elementCount + 1);
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
elementData[index] = obj;
elementCount++;
}
add方法:添加一个元素,默认添加在数组最后,也可以在指定位置添加一个元素。synchronize方法,线程安全的。
在数组最后添加一个元素:
- 首先modcount++,modcount代表修改次数
- 然后调用ensureCapacityHelper方法来进行容量相关的判断和处理
- 判断的方法很简单,就是用添加元素之后的数组元素个数(elementCount+1)与数组当前的容量比较
- 如果不够,则调用grow方法进行扩容,如果初始化时指定了每次扩容的大小,则按指定的值进行扩容,如果没有指定,则每次扩容10个大小。
- 最后将元素放到指定位置上,elementCount++,返回true
在指定位置添加元素:
- 调用insertElementAt方法,将要插入的元素和插入的下标传递进去
- modcount++
- 判断要插入的位置是否合理(应小于当前数组的元素个数),如果不合理,则抛出异常
- 然后调用ensureCapacityHelper方法来进行容量相关的判断和处理
- 最后调用System.arraycopy方法来对数组进行搬移,然后插入该元素,elementCount++
public synchronized E set(int index, E element) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
set方法:修改某个位置的元素。synchronize方法,线程安全的。
- 首先获取该位置之前的值
- 将新值插入并返回旧值
public synchronized E get(int index) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
return elementData(index);
}
get方法:获得指定位置的元素。synchronize方法,线程安全。
- 判断下标是否合理,不合理则抛出异常
- 返回该位置的元素
public synchronized E remove(int index) {
modCount++;
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index);
int numMoved = elementCount - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--elementCount] = null; // Let gc do its work
return oldValue;
}
public boolean remove(Object o) {
return removeElement(o);
}
public synchronized boolean removeElement(Object obj) {
modCount++;
int i = indexOf(obj);
if (i >= 0) {
removeElementAt(i);
return true;
}
return false;
}
remove方法:移除指定元素或者指定位置的元素。synchronize方法,线程安全。
移除指定位置的元素:
- 判断下标是否合理,不合理则抛出异常
- 返回该位置之前的元素
- 调用System.arraycopy方法来对数组进行搬移,由于搬移后,最后两位都是之前的最后一位,因此将最后一位置为null,方便GC
- 返回旧值
移除指定元素:
调用removeElement方法
- modcount++
- 根据Indexof方法来查找该元素,如果不存在返回-1,存在返回下标
public synchronized int capacity() {
return elementData.length;
}
capacity方法:获取当前数组的长度。synchronize方法,线程安全。
public synchronized int size() {
return elementCount;
}
size方法:获取当前数组的元素个数。synchronize方法,线程安全。
public synchronized boolean isEmpty() {
return elementCount == 0;
}
isEmpty方法:判断当前数组中是否含有元素。synchronize方法,线程安全。
public boolean contains(Object o) {
return indexOf(o, 0) >= 0;
}
contains方法:判断当前数组中是否含有元素。synchronize方法,线程安全。
public synchronized E firstElement() {
if (elementCount == 0) {
throw new NoSuchElementException();
}
return elementData(0);
}
public synchronized E lastElement() {
if (elementCount == 0) {
throw new NoSuchElementException();
}
return elementData(elementCount - 1);
}
返回数组中的第一个元素和最后一个元素
public synchronized Object clone() {
try {
@SuppressWarnings("unchecked")
Vector<E> v = (Vector<E>) super.clone();
v.elementData = Arrays.copyOf(elementData, elementCount);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError(e);
}
}
clone方法:数组克隆。
总结
- Vector和ArrayList类似,底层都是一个Object数组,Vector的初始容量默认为10,支持动态扩容,每次扩容默认增大10个,可以显式指定。
- Vector是线程安全的,是通过给方法加上synchronize锁实现的。