类定义:这是一个具体类,可以被实例化
public class Vector<E> extends AbstractList<E> implements List<E>,RandomAccess,Cloneable,java,io.Serializable
构造方法
//初始化向量,给出初始荣来那个以及capacityIncrement(控制着向量需要增长时所要增长的量)
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
//初始化向量,默认初始capactityIncrement是0
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
//初始化向量,默认容量是10
public Vector() {
this(10);
}
//用一个集合初始化向量,初始容量刚好是c的大小,元素个数也是c的元素个数
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);
}
域:
protected Object[] elementData; //Vector存储元素使用数组,Vector的存储能力等于数组长度,数组长度至少要达到能包含Vector的所有元素
protected int elementCount; // vector包含的元素个数(少于等于数组长度的)
//当向量的大小(size)要大于存储能力capacity时,向量的capacity将会自动增大。如果capacityincrement小于等于0,那么每当需要增长时,容量增加一倍。该变量控制每次增长的大小
protected int capacityIncrement;
//最大数组容量为:最大整数-8
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
公共方法
//将向量元素拷贝到一个数组里,向量数组中的第k个元素拷到anArray对应的索引k处
public synchronized void copyInto(Object[] anArray) {
System.arraycopy(elementData, 0, anArray, 0, elementCount);
}
//将向量容量变为刚好等于大小。(删除多余的存储空间)
public synchronized void trimToSize() {
modCount++;
int oldCapacity = elementData.length;
if (elementCount < oldCapacity) {
elementData = Arrays.copyOf(elementData, elementCount);
}
}
//确保向量容量为最小容量,如果当前的向量容量小于minCapacity,则会将存储数组扩容到向量大小加capacityIncrement.即新的存储数组长度为:size+capacityIncrement.但是如果capacityIncrement小于等于0,那么扩充的容量直接是扩充前的两倍。如果新的存储数组长度还是小雨minCpacity,则将数组长度扩展到minCapacity
public synchronized void ensureCapacity(int minCapacity) {
if (minCapacity > 0) {
modCount++;
ensureCapacityHelper(minCapacity);
}
}
//设置向量容量,如果新的向量容量大于元素个数,则扩充数组到newSize. 否则,保留数组前newSize-1个元素,返回newSize
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;
}
//枚举向量元素,从0~elementCount
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");
}
};
}
//判断是否包含元素o
public boolean contains(Object o) {
return indexOf(o, 0) >= 0;
}
//返回元素o的位置
public int indexOf(Object o) {
return indexOf(o, 0);
}
//从index开始往后寻找,找到第一个等于o的元素并返回其索引,若没有则返回-1
public synchronized int indexOf(Object o, int index) {
if (o == null) {
for (int i = index ; i < elementCount ; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = index ; i < elementCount ; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
//返回元素o在向量中最后出现的位置
public synchronized int lastIndexOf(Object o) {
return lastIndexOf(o, elementCount-1);
}
//从index开始往前查找,如果找到o,则返回其索引,否则返回-1.该方法是查找元素o在向量中最后出现的位置
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--)
if (elementData[i]==null)
return i;
} else {
for (int i = index; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
//返回索引index处的元素。当给出的索引大于等于元素个数时,抛出数组越界异常。否则返回index处的值
public synchronized E elementAt(int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}
return elementData(index);
}
//返回向量第一个元素
public synchronized E firstElement() {
if (elementCount == 0) {
throw new NoSuchElementException();
}
return elementData(0);
}
//返回向量最后一个元素
public synchronized E lastElement() {
if (elementCount == 0) {
throw new NoSuchElementException();
}
return elementData(elementCount - 1);
}
//设置index处的值
public synchronized void setElementAt(E obj, int index) {
if (index >= elementCount) { //判断Index是否合法
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
elementData[index] = obj;
}
//删除index处的元素。首先判断index的合法性,然后要将index之后的元素索引统一往前移一位,先计算要移动的元素个数,然后调用System.arraycopy(elementData, index + 1, elementData, index, j)将index+1之后的元素移动往前移一位。
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--; //元素个数减1
elementData[elementCount] = null; /* to let gc do its work */将最后一个置空
}
//在指定位置插入元素
public synchronized void insertElementAt(E obj, int index) {
modCount++;
if (index > elementCount) {
throw new ArrayIndexOutOfBoundsException(index
+ " > " + elementCount);
}
ensureCapacityHelper(elementCount + 1); //确保最小容量为elementCount+1
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
elementData[index] = obj;
elementCount++;
}
//添加一个元素到向量末尾
public synchronized void addElement(E obj) {
modCount++;
ensureCapacityHelper(elementCount + 1); //遇到添加元素时都要考虑容量大小
elementData[elementCount++] = obj;
}
//删除向量中第一个等于obj的元素,如果有删除,返回true,否则返回false
public synchronized boolean removeElement(Object obj) {
modCount++;
int i = indexOf(obj);
if (i >= 0) {
removeElementAt(i);
return true;
}
return false;
}
//删除所有元素,将数组每个元素都置空,剩下就被垃圾回器回收。然后置elementCount=0
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();
}
}
//返回向量的数组(只包含元素的那部分)
public synchronized Object[] toArray() {
return Arrays.copyOf(elementData, elementCount);
}
//将向量所有元素类型转换为T的a数组
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;
return a;
}
//返回index处的元素
public synchronized E get(int index) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
return elementData(index);
}
//设置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;
}
//添加一个元素e
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
//删除元素o
public boolean remove(Object o) {
return removeElement(o);
}
//在指定位置插入元素
public void add(int index, E element) {
insertElementAt(element, index);
}
//删除指定位置的元素,并返回该元素
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();
}
//判断是否包含集合c
public synchronized boolean containsAll(Collection<?> c) {
return super.containsAll(c);
}
//添加集合c,如果有添加返回true,否则返回false。
public synchronized boolean addAll(Collection<? extends E> c) {
modCount++;
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityHelper(elementCount + numNew);
System.arraycopy(a, 0, elementData, elementCount, numNew);
elementCount += numNew;
return numNew != 0;
}
//删除在向量中同时也在c中的元素
public synchronized boolean removeAll(Collection<?> c) {
return super.removeAll(c);
}
//删除向量中不被c包含的元素
public synchronized boolean retainAll(Collection<?> c) {
return super.retainAll(c);
}
//将集合C插入向量中指定位置。先判断index的合法性,然后确保向量容量足够,然后将从index开始的元素向后移动numNew位,然后将集合c的数组拷贝到向量数组的index位置开始的空间.
public synchronized boolean addAll(int index, Collection<? extends E> c) {
modCount++;
if (index < 0 || index > elementCount)
throw new ArrayIndexOutOfBoundsException(index);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityHelper(elementCount + numNew);
int numMoved = elementCount - index;
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);
System.arraycopy(a, 0, elementData, index, numNew);
elementCount += numNew;
return numNew != 0;
}
//判断向量是否等于o
public synchronized boolean equals(Object o) {
return super.equals(o);
}
public synchronized int hashCode() {
return super.hashCode();
}
public synchronized String toString() {
return super.toString();
}
//返回子集合
public synchronized List<E> subList(int fromIndex, int toIndex) {
return Collections.synchronizedList(super.subList(fromIndex, toIndex),
this);
}
//删除指定范围内的集合,先计算要移动的个数,然后将toIndex之后的元素移动到fromIndex开始的位置,然后将剩余空间置null
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;
}
//返回集合的ListIterator,该ListIterator从index开始遍历
public synchronized ListIterator<E> listIterator(int index) {
if (index < 0 || index > elementCount)
throw new IndexOutOfBoundsException("Index: "+index);
return new ListItr(index);
}
//返回集合的ListIterator,该ListIterator从0开始遍历
public synchronized ListIterator<E> listIterator() {
return new ListItr(0);
}
//返回集合的迭代器
public synchronized Iterator<E> iterator() {
return new Itr();
}
私有方法
//如果最小容量大于数组当前的大小,则扩充数组
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
//扩展数组,主要是计算新数组的大小。如果capacityIncrement大于0,则新数组大小等于原数组大小加capacityIncrement,否则新数组大小是原来数组大小的两倍。当新数组大小大于最大数组容量时,调用hugeCapacity
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);
}
//当最小容量小于0时,抛出异常,当最小容量大于最大数组容量时,返回最大整数,否则返回最大数组容量
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
//Save the state of the {@code Vector} instance to a stream
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
final java.io.ObjectOutputStream.PutField fields = s.putFields();
final Object[] data;
synchronized (this) {
fields.put("capacityIncrement", capacityIncrement);
fields.put("elementCount", elementCount);
data = elementData.clone();
}
fields.put("elementData", data);
s.writeFields();
}
私有类
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
public boolean hasNext() {
// Racy but within spec, since modifications are checked
// within or after synchronization in next/previous
return cursor != elementCount;
}
public E next() {
synchronized (Vector.this) {
checkForComodification();
int i = cursor;
if (i >= elementCount)
throw new NoSuchElementException();
cursor = i + 1;
return elementData(lastRet = i);
}
}
//删除当前迭代到的元素
public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
synchronized (Vector.this) {
checkForComodification();
Vector.this.remove(lastRet);
expectedModCount = modCount;
}
cursor = lastRet; //删除之后cursor就不能向前走了。
lastRet = -1; //lastRet=-1确保不能重复删除
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
final class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
super();
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
public E previous() {
synchronized (Vector.this) {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
cursor = i;
return elementData(lastRet = i);
}
}
public void set(E e) {
if (lastRet == -1)
throw new IllegalStateException();
synchronized (Vector.this) {
checkForComodification();
Vector.this.set(lastRet, e);
}
}
public void add(E e) {
int i = cursor;
synchronized (Vector.this) {
checkForComodification();
Vector.this.add(i, e);
expectedModCount = modCount;
}
cursor = i + 1;
lastRet = -1;
}
}