Vector特点
底层基于动态数组实现
所有操作都加上了synchronized操作,属于线程安全的容器类。
可以把Vector看成是一个线程安全的ArrayList
构造函数
//默认构造函数
public Vector() {
//给的初始容量为10
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);
//创建给定初始容量的Object数组
this.elementData = new Object[initialCapacity];
//下次扩容时增加的大小,默认是0
this.capacityIncrement = capacityIncrement;
}
add操作
public synchronized boolean add(E e) {
modCount++;
//确认是否扩容
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
private void ensureCapacityHelper(int minCapacity) {
// 超过了当前容量,扩容
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
//如果初始化设置了capacityIncrement,新的容量就是old+capacityIncrement,否则两倍容量扩容
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);
}
get操作
//直接就是数组的索引操作
public synchronized E get(int index) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
return elementData(index);
}
public int indexOf(Object o) {
return indexOf(o, 0);
}
public synchronized int indexOf(Object o, int index) {
if (o == null) {
//如果目标为空,找到第一个为null的index
for (int i = index ; i < elementCount ; i++)
if (elementData[i]==null)
return i;
} else {
//遍历整个数组调用对象的equals方法,返回index
for (int i = index ; i < elementCount ; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
remove操作
public synchronized E remove(int index) {
modCount++;
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index);
int numMoved = elementCount - index - 1;
//index后面的数组往前移动一位
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
//最后一个数组设置成Null
elementData[--elementCount] = null; // Let gc do its work
return oldValue;
}
clear操作
public void clear() {
removeAllElements();
}
public synchronized void removeAllElements() {
modCount++;
// 设置成null 让gc回收
for (int i = 0; i < elementCount; i++)
elementData[i] = null;
//数量置位0
elementCount = 0;
}
forEach
//加锁遍历整个数组
@Override
public synchronized void forEach(Consumer<? super E> action) {
Objects.requireNonNull(action);
final int expectedModCount = modCount;
@SuppressWarnings("unchecked")
final E[] elementData = (E[]) this.elementData;
final int elementCount = this.elementCount;
for (int i=0; modCount == expectedModCount && i < elementCount; i++) {
action.accept(elementData[i]);
}
//加锁期间 有其他线程修改modCount就会抛出异常
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
总结
Vector基于数组实现,意味着遍历会更快,但是增加,删除操作要对数组进行移动复制效率会更低(增加元素可能要扩容需要复制)。
允许Null元素存在。
默认扩容两倍容量。
所有操作都加了syncronized,是线程安全的容器类,但是外界调用Vector的复合操作就不是线程安全的。比如先int i = vector.indexOf(Object); vector.remove(i);