Vector
1865字约6分钟
2024-08-08
Vector
类是一个可以动态修改的数组,与 ArrayList
的最大的不同是它是线程安全的
底层数据结构
RandomAccess
是一个标志接口,表明实现这个接口的List
集合是支持快速随机访问的。在Vector
中,我们即可以通过元素的序号快速获取元素对象,这就是快速随机访问Vector
实现了Cloneable
接口 ,即覆盖了函数clone()
,能被克隆Vector
实现了java.io.Serializable
接口,这意味着Vector
支持序列化,能通过序列化去传输
public class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
/**
* The array buffer into which the components of the vector are
* stored. The capacity of the vector is the length of this array buffer,
* and is at least large enough to contain all the vector's elements.
*
* 存储 vector 中元素的数组。vector 的容量是数组的长度,数组的长度最小值为 vector 的元素个数
*
* <p>Any array elements following the last element in the Vector are null.
*
* 任何在 vector 最后一个元素之后的数组元素是null
*
* @serial
*/
protected Object[] elementData;
/**
* The number of valid components in this {@code Vector} object.
* Components {@code elementData[0]} through
* {@code elementData[elementCount-1]} are the actual items.
*
* vector 实际元素个数
*
* @serial
*/
protected int elementCount;
/**
* The amount by which the capacity of the vector is automatically
* incremented when its size becomes greater than its capacity. If
* the capacity increment is less than or equal to zero, the capacity
* of the vector is doubled each time it needs to grow.
*
* 当 vector 实际容量 elementCount 将要大于它的最大容量时,capacityIncrement 是 vecotr 自动增加的容量大小
* 如果 capacityIncrement 小于或等于 0,vector 容量每次需要增长时将会成倍增长
*
* @serial
*/
protected int capacityIncrement;
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -2767605614048989439L;
}
构造方法
Vector
提供了三种构造方法:
Vector(int initialCapacity, int capacityIncrement)
:构造一个指定初始容量为capacity
、自增容量为capacityIncrement
的空vector
Vector(int initialCapacity)
:构造一个指定容量为initialCapacity
、自增容量为0
的空vector
Vector()
:构造一个指定容量为10
、自增容量为0
的空vector
Vector(Collection<? extends E> c)
:使用指定的Collection
构造vector
Vector(int initialCapacity, int capacityIncrement)
/**
* Constructs an empty vector with the specified initial capacity and
* capacity increment.
*
* 构造一个指定初始容量和自增容量的空 Vector
*
* @param initialCapacity the initial capacity of the vector
* vecotr 的初始容量
* @param capacityIncrement the amount by which the capacity is
* increased when the vector overflows
* 当 vector 容量要溢出时所增加的容量
* @throws IllegalArgumentException if the specified initial capacity
* is negative
* 指定初始容量为负时抛出的异常
*/
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
Vector(int initialCapacity)
/**
* Constructs an empty vector with the specified initial capacity and
* with its capacity increment equal to zero.
*
* 构造一个指定初始容量和自增容量为 0 的空 vector
*
* @param initialCapacity the initial capacity of the vector
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
Vector()
/**
* Constructs an empty vector so that its internal data array
* has size {@code 10} and its standard capacity increment is
* zero.
*/
public Vector() {
this(10);
}
Vector(Collection<? extends E> c)
/**
* Constructs a vector containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* 构造一个包含指定集合元素的 vector,按照集合迭代器返回的顺序
*
* @param c the collection whose elements are to be placed into this
* vector
* 要放入 vecotr 中的集合元素
* @throws NullPointerException if the specified collection is null
* 如果指定集合为 null 则抛出 NullPointerException 异常
* @since 1.2
*/
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);
}
方法
方法名 | 时间复杂度 |
---|---|
copyInto(Object[] anArray) | |
trimToSize() | |
ensureCapacity(int minCapacity) |
copyInto(Object[] anArray)
/**
* Copies the components of this vector into the specified array.
* The item at index {@code k} in this vector is copied into
* component {@code k} of {@code anArray}.
*
* 将 vector 中的所有元素拷贝到指定数组 anArray 中
*
* @param anArray the array into which the components get copied
* 用来存放 vector 中所有元素的数组
* @throws NullPointerException if the given array is null
* 如果给定数组 anArray 为 null,则抛出 NullPointerException 异常
* @throws IndexOutOfBoundsException if the specified array is not
* large enough to hold all the components of this vector
* 如果指定数组 anArray 容量小于 vector 的元素个数,则抛出 IndexOutOfBoundsException 异常
* @throws ArrayStoreException if a component of this vector is not of
* a runtime type that can be stored in the specified array
* 如果 vector 不能被拷贝到指定的 anArray,则抛出 ArrayStoreException 异常
* @see #toArray(Object[])
*/
public synchronized void copyInto(Object[] anArray) {
System.arraycopy(elementData, 0, anArray, 0, elementCount);
}
trimToSize()
/**
* Trims the capacity of this vector to be the vector's current
* size. If the capacity of this vector is larger than its current
* size, then the capacity is changed to equal the size by replacing
* its internal data array, kept in the field {@code elementData},
* with a smaller one. An application can use this operation to
* minimize the storage of a vector.
*
* 将 vecotr 的容量缩减为实际大小
* 如果 vector 的容量大于实际大小,则将容量更改为实际大小,将较小的容量值保存到 elementData 字段中
* 应用可以使用这个操作最小化 vector 的容量
*/
public synchronized void trimToSize() {
modCount++;
int oldCapacity = elementData.length;
// 实际大小 < 底层数组长度
if (elementCount < oldCapacity) {
// 将底层数组长度调整为实际大小
elementData = Arrays.copyOf(elementData, elementCount);
}
}
ensureCapacity(int minCapacity)
/**
* Increases the capacity of this vector, if necessary, to ensure
* that it can hold at least the number of components specified by
* the minimum capacity argument.
*
* 增加 vecotr 容量,确保能够满足最少需要的容量大小
*
* <p>If the current capacity of this vector is less than
* {@code minCapacity}, then its capacity is increased by replacing its
* internal data array, kept in the field {@code elementData}, with a
* larger one. The size of the new data array will be the old size plus
* {@code capacityIncrement}, unless the value of
* {@code capacityIncrement} is less than or equal to zero, in which case
* the new capacity will be twice the old capacity; but if this new size
* is still smaller than {@code minCapacity}, then the new capacity will
* be {@code minCapacity}.
*
* 如果 vecotr 当前容量小于最少需要容量 minCapacity,则会扩容。将容量较大值设置到 elementData 字段中
* 新数组容量将在旧数组容量上增加 capacityIncrement,除非 capacityIncrement 小于或等于 0,在这种情况下容量在旧数据容来那个上翻一倍
* 扩容后的容量仍小于最小需要的容量,则将容量扩容至最小所需容量
*
* @param minCapacity the desired minimum capacity
*/
public synchronized void ensureCapacity(int minCapacity) {
if (minCapacity > 0) {
modCount++;
ensureCapacityHelper(minCapacity);
}
}
/**
* This implements the unsynchronized semantics of ensureCapacity.
* Synchronized methods in this class can internally call this
* method for ensuring capacity without incurring the cost of an
* extra synchronization.
*
* ensureCapacity() 方法的 unsynchronized 实现
* ensureCapacity() 方法是同步的,它可以调用本方法来扩容,而不用承受同步带来的消耗
*
* @see #ensureCapacity(int)
*/
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
// 如果最小所需容量大于数组容量,则进行扩容
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
/**
* The maximum size of array to allocate.
* Some VMs reserve some header words in an array.
* Attempts to allocate larger arrays may result in
* OutOfMemoryError: Requested array size exceeds VM limit
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
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);
}
/**
* 进行大容量分配
*/
private static int hugeCapacity(int minCapacity) {
// 如果 minCapacity 小于 0 则抛出异常
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
// 所需最小容量大于 MAX_ARRAY_SIZE,则分配 Integer.MAX_VALUE,否则分配 MAX_ARRAY_SIZE
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}