Vector 是 Java 提供的可以自动增长的动态数组,同时是线程安全的。
Java的继承体系:
- 成员变量
// 存放元素的数组
protected Object[] elementData;
// 元素个数
protected int elementCount;
//容器自动增大值
protected int capacityIncrement;
- 构造函数
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
// 初始数组,长度为initialCapacity
this.elementData = new Object[initialCapacity];
// 设置自动增长值
this.capacityIncrement = capacityIncrement;
}
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
// 默认大小为10
public Vector() {
this(10);
}
// 通过集合来初始化数组
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的数组复制到指定的数组中
public synchronized void copyInto(Object[] anArray) {
System.arraycopy(elementData, 0, anArray, 0, elementCount);
}
// Trims the capacity of this vector to be the vector's current size.
// 把Vector的长度作为容器大小
public synchronized void trimToSize() {
// modCount表示修改的次数,在迭代器中使用
modCount++;
int oldCapacity = elementData.length;
if (elementCount < oldCapacity) {
elementData = Arrays.copyOf(elementData, elementCount);
}
}
// 设置数组长度
public synchronized void ensureCapacity(int minCapacity) {
if (minCapacity > 0) {
modCount++;
ensureCapacityHelper(minCapacity);
}
}
// 如果设置值大于数组长度,则增加数组长度到设置值
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
// 设置数组元素个数
public synchronized void setSize(int newSize) {
modCount++;
if (newSize > elementCount) {
ensureCapacityHelper(newSize);
} else {
for (int i = newSize ; i < elementCount ; i++) {
elementData[i] = null;
}
}
elementCount = newSize;
}
// 获取数组长度
public synchronized int capacity() {
return elementData.length;
}
// 获取数组元素个数
public synchronized int size() {
return elementCount;
}
// 判断数组是否为空
public synchronized boolean isEmpty() {
return elementCount == 0;
}
// 是否包含对象
public boolean contains(Object o) {
return indexOf(o, 0) >= 0;
}
// 添加元素
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
// 按索引获取元素
public synchronized E get(int index) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
//
return elementData(index);
}
// 替代某个位置的元素
public synchronized E set(int index, E element) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
// 移除第一次出现的对象元素
public synchronized boolean removeElement(Object obj) {
modCount++;
int i = indexOf(obj);
if (i >= 0) {
removeElementAt(i);
return true;
}
return false;
}
// 移除全部元素
public synchronized void removeAllElements() {
modCount++;
// Let gc do its work
for (int i = 0; i < elementCount; i++)
elementData[i] = null;
elementCount = 0;
}
// 克隆该对象
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);
}
}