和ArrayList极为相似。
ArrayList:https://blog.csdn.net/dmw412724/article/details/84447772
有两点不同。
1.ArrayList自动扩容,扩容长度为原数组的一半,而Vector扩容的长度可以自己定下来。看如下构造:
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
initialCapacity是数组初始长度,capacityIncrement是每次扩容长度,如果为0,则代表扩容长度为当前数组长度,不是一半哦。
如下grow方法
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);
}
2.vector还有个方法是trimToSize()将数组容量缩小到size();