類定義:這是一個具體類,可以被實例化
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;
}
}