/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -2767605614048989439L;
/**
* Constructs an empty vector with the specified initial capacity and
* capacity increment.
*
* 使用指定的初始容量和容量增量构造一个空的向量。
* 指定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;
}
/**
* Constructs an empty vector with the specified initial capacity and
* with its capacity increment equal to zero.
*
* 使用指定的初始容量和等于零的容量增量构造一个空向量。
*/
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
/**
* Constructs an empty vector so that its internal data array
* has size {@code 10} and its standard capacity increment is
* zero.
* 构造一个空向量,使其内部数据数组的大小为 10,其标准容量增量为零。
*/
public Vector() {
this(10);
}
/**
* Constructs a vector containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* 构造一个包含指定 collection 中的元素的向量,
* 这些元素按其 collection 的迭代器返回元素的顺序排列。
*
*/
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);
}
/**
* 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}.
* 将此向量的组件复制到指定的数组中。此向量中索引 k 处的项将复制到 anArray 的组件 k 中。
* 将数组Vector的全部元素都拷贝到数组anArray中
*/
public synchronized void copyInto(Object[] anArray) {
System.arraycopy(elementData, 0, anArray, 0, elementCount);
}
/**
* 将当前容量值设为 = 实际元素个数
* 对此向量的容量进行微调,使其等于向量的当前大小。
*/
public synchronized void trimToSize() {
modCount++; //Vector的改变统计数+1
int oldCapacity = elementData.length;
if (elementCount < oldCapacity) {
elementData = Arrays.copyOf(elementData, elementCount);
}
}
/**
* 增加此向量的容量(如有必要),以确保其至少能够保存最小容量参数指定的组件数。
*
* @param minCapacity the desired minimum capacity
* minCapacity 所需的最小容量
*/
public synchronized void ensureCapacity(int minCapacity) {
if (minCapacity > 0) {
modCount++;
ensureCapacityHelper(minCapacity);//确认“Vector容量”的帮助函数
}
}
/**
* Sets the size of this vector. If the new size is greater than the
* current size, new {@code null} items are added to the end of
* the vector. If the new size is less than the current size, all
* components at index {@code newSize} and greater are discarded.
* 设置此向量的大小。如果新大小大于当前大小,则会在向量的末尾添加相应数量的 null 项。
* 如果新大小小于当前大小,则丢弃索引 newSize 处及其之后的所有项。
* @param newSize the new size of this vector
* @throws ArrayIndexOutOfBoundsException if the new size is negative 负数的话,抛出异常
*/
public synchronized void setSize(int newSize) {
modCount++;
if (newSize > elementCount) {
ensureCapacityHelper(newSize);
} else {
for (int i = newSize ; i < elementCount ; i++) {
elementData = null;
}
}
elementCount = newSize;
}
/**
* 返回此向量的当前容量。 若新初始化
* Vector<String> v = new Vector<String>();
* v.capacity 返回为10
* v.size 返回为 0
*/
public synchronized int capacity() {
return elementData.length;
}
/**
* Returns the number of components in this vector.
* 返回此向量中的组件数。 Vector中数组的元素大小!
* @return the number of components in this vector
*/
public synchronized int size() {
return elementCount;
}
/**
* Tests if this vector has no components.
* 测试此向量是否不包含组件(元素)
* 当且仅当此向量没有组件(元素)(也就是说其大小为零)时返回 true;否则返回 false。
*/
public synchronized boolean isEmpty() {
return elementCount == 0;
}
/**
* 返回此向量的组件的枚举。返回的 Enumeration 对象将生成此向量中的所有项。
* 生成的第一项为索引 0 处的项,然后是索引 1 处的项,依此类推。
* (1)
* for(Enumeration<String> elements = v.elements();elements.hasMoreElements() ;)
* System.out.printf(elements.nextElement());
* (2)
* while(elements.hasMoreElements())
* System.out.printf(elements.nextElement());
*/
public Enumeration<E> elements() {
return new Enumeration<E>() {
int count = 0;
public boolean hasMoreElements() {
return count < elementCount;
}
public E nextElement() {
synchronized (Vector.this) {
if (count < elementCount) {
return elementData(count++);
}
}
throw new NoSuchElementException("Vector Enumeration");
}
};
}
/**
* 返回此向量中第一次出现的指定元素的索引,从 index 处正向搜索,
* 如果未找到该元素,则返回 -1。更确切地讲,
* 返回满足 (i >= index && (o==null ? get(i)==null : o.equals(get(i)))) 的最低索引 i;
* 如果没有这样的索引,则返回 -1。
*/
public synchronized int indexOf(Object o, int index) {
//分为null和不为null
if (o == null) {
for (int i = index ; i < elementCount ; i++)//从index处正向搜索,默认从索引为0处开始
if (elementData==null)
return i;
} else {
for (int i = index ; i < elementCount ; i++)
if (o.equals(elementData))
return i;
}
return -1;
}
/**
* 返回此向量中最后一次出现的指定元素的索引,从 index 处逆向搜索,如果未找到该元素,则返回 -1。
* 更确切地讲,返回满足 (i <= index && (o==null ? get(i)==null : o.equals(get(i)))) 的最高索引 i;
* 如果没有这样的索引,则返回 -1。
*/
public synchronized int lastIndexOf(Object o, int index) {
if (index >= elementCount)
throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
if (o == null) {
for (int i = index; i >= 0; i--)//从index处正向搜索,默认从索引为elementCount-1处开始
if (elementData==null)
return i;
} else {
for (int i = index; i >= 0; i--)
if (o.equals(elementData))
return i;
}
return -1;
}
/**
* 返回指定索引处的组件。
* 此方法的功能与 get(int) 方法的功能完全相同(后者是 List 接口的一部分)
*/
public synchronized E elementAt(int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}
return elementData(index);//按照下标去查找元素
}
/**
* 返回此向量的第一个组件(位于索引 0) 处的项)。
*/
public synchronized E firstElement() {
if (elementCount == 0) {
throw new NoSuchElementException();
}
return elementData(0);
}
/**
* 向量的最后一个组件,即索引 size() - 1 处的组件。
*/
public synchronized E lastElement() {
if (elementCount == 0) {
throw new NoSuchElementException();
}
return elementData(elementCount - 1);
}
/**
* 将此向量指定 index 处的组件设置为指定的对象。丢弃该位置以前的组件(元素)。
* 索引必须为一个大于等于 0 且小于向量当前大小的值。
*/
public synchronized void setElementAt(E obj, int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
elementData[index] = obj;
}
/**
* 删除指定索引处的组件。此向量中的每个索引大于等于指定 index 的组件都将下移,
* 使其索引值变成比以前小 1 的值。此向量的大小将减 1。
*
* 索引必须为一个大于等于 0 且小于向量当前大小的值。
*/
public synchronized void removeElementAt(int index) {
modCount++;
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
int j = elementCount - index - 1;
if (j > 0) {
System.arraycopy(elementData, index + 1, elementData, index, j);
}
elementCount--;
elementData[elementCount] = null; /* to let gc do its work 让gc做它的工作*/
}
/**
* 将指定对象作为此向量中的组件插入到指定的 index 处。
* 此向量中的每个索引大于等于指定 index 的组件都将向上移位,使其索引值变成比以前大 1 的值
*/
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++;
}
/**
* 从此向量中移除变量的第一个(索引最小的)匹配项。
* 如果在此向量中找到该对象,那么向量中索引大于等于该对象索引的每个组件都会下移,
* 使其索引值变成比以前小 1 的值
*/
public synchronized boolean removeElement(Object obj) {
modCount++;
int i = indexOf(obj); //查询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 = null; //全部设置为 null
elementCount = 0; // elementCount大小设置为 0
}
/**
* 返回向量的一个副本。副本中将包含一个对内部数据数组副本的引用,
* 而非对此 Vector 对象的原始内部数据数组的引用。
*/
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();
}
}
/**
* Returns an array containing all of the elements in this Vector
* in the correct order.
* 返回一个数组,包含此向量中以恰当顺序存放的所有元素。
* @since 1.2
*/
public synchronized Object[] toArray() {
return Arrays.copyOf(elementData, elementCount);
}
/**
* 返回一个数组,包含此向量中以恰当顺序存放的所有元素;返回数组的运行时类型为指定数组的类型。
*/
@SuppressWarnings("unchecked")
public synchronized <T> T[] toArray(T[] a) {
if (a.length < elementCount)
return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
System.arraycopy(elementData, 0, a, 0, elementCount);
if (a.length > elementCount)
a[elementCount] = null;
/**
* 返回向量中指定位置的元素。
*/
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;
}
/**
* 在此向量的指定位置插入指定的元素。将当前位于该位置的元素(如果有)
* 及所有后续元素右移(将其索引加 1)。
*/
public void add(int index, E element) {
insertElementAt(element, index);
}
/**
* 移除此向量中指定位置的元素。将所有后续元素左移(将其索引减 1)。
* 返回此向量中移除的元素。
*/
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 void clear() {
removeAllElements();
}
// Bulk Operations
// 批量操作
/**
* 如果此向量包含指定 Collection 中的所有元素,则返回 true。
*/
public synchronized boolean containsAll(Collection<?> c) {
return super.containsAll(c);
}
/**
* 返回此 List 的部分视图,元素范围为从 fromIndex(包括)到 toIndex(不包括)。
*/
public synchronized List<E> subList(int fromIndex, int toIndex) {
return Collections.synchronizedList(super.subList(fromIndex, toIndex),
this);
}
/**
* 从此 List 中移除其索引位于 fromIndex(包括)与 toIndex(不包括)之间的所有元素
*/
protected synchronized void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = elementCount - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// Let gc do its work
int newElementCount = elementCount - (toIndex-fromIndex);
while (elementCount != newElementCount)
elementData[--elementCount] = null;
}