1.Vector
public class Vector extends AbstractList implements List, java.io.Serializable
- 實作可增長的對象數組
- 可以使用整數索引進行通路, 可根據需要增大或縮小
- 方法上加synchronized保證同步的,fail-fast。
成員變量
/**
* 存儲向量元件的數組緩沖區。
*/
protected Object[] elementData;
/**
* Vector 對象中的有效元件數。
*/
protected int elementCount;
/**
* 向量的大小大于其容量時,容量自動增加的量。
*/
protected int capacityIncrement;
構造函數
/**
*使用指定的初始容量和容量增量構造一個空的向量。
*/
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < )
throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
/**
* 使用指定的初始容量和等于零的容量增量構造一個空向量。
*/
public Vector(int initialCapacity) {
this(initialCapacity, );
}
/**
*構造一個空向量,使其内部資料數組的大小為 10,其标準容量增量為零。
*/
public Vector() {
this();
}
/**
* 構造一個包含指定 collection 中的元素的向量且按疊代器傳回元素的順序排列。
*/
public Vector(Collection<? extends E> c) {
elementData = c.toArray();
elementCount = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
//c.toArray有可能傳回的不是Object[]類型,
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
//Arrays.copyOf(elementData, elementCount, Object[].class);這個方法就是用來建立1個Object[]數組,這樣數組中就可以存放任意對象了。
}
常用方法介紹:
boolean add(E e): 将指定元素添加到此向量的末尾。
public synchronized boolean add(E e) {
//修改次數+1
modCount++;
//擴容
ensureCapacityHelper(elementCount + );
//元素放到向量末尾
elementData[elementCount++] = e;
return true;
}
//保證容量足夠
private void ensureCapacityHelper(int minCapacity) {
//擷取現有數組大小
int oldCapacity = elementData.length;
//若要求的最小容量大于現有容量,進行擴容
if (minCapacity > oldCapacity) {
Object[] oldData = elementData;
//若設定了增量則為現有容量大小+增量,否則現有容量*2
int newCapacity = (capacityIncrement > ) ?
(oldCapacity + capacityIncrement) : (oldCapacity * );
//若計算後的擴容容量都不滿足最小容量要求,本次擴容到最小容量
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
//進行擴容并數組複制
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
由源碼可知,synchronized保證新增同步,擴容方案為:先計算預期擴容大小(容量增量>0,則預期為:現有容量+容量增量,否則為:現有容量*2),計算後與要求的最小容量比較,取最大值。
之是以有計算預期擴容大小,是因為每次add時,擴容為現有容量+要新增的元素個數,如果每次都擴容到剛剛好放所有新元素,則每次都需要擴容,進行數組複制,性能會比較差。
void add(int index, E element): 在此向量的指定位置插入指定的元素。
public void add(int index, E element) {
insertElementAt(element, index);
}
public synchronized void insertElementAt(E obj, int index) {
//修改次數+1
modCount++;
//數組越界檢查
if (index > elementCount) {
throw new ArrayIndexOutOfBoundsException(index
+ " > " + elementCount);
}
//擴容
ensureCapacityHelper(elementCount + );
//向量擴容并複制
System.arraycopy(elementData, index, elementData, index + , elementCount - index);
//設定指定索引元素為新元素
elementData[index] = obj;
//容量元素數+1
elementCount++;
}
boolean addAll(Collection c) :将指定 Collection 中的所有元素添加到此向量的末尾,按照指定 collection 的疊代器所傳回的順序添加這些元素。
public synchronized boolean addAll(Collection<? extends E> c) {
//修改次數+1
modCount++;
//擷取collection數組
Object[] a = c.toArray();
int numNew = a.length;
//擴容
ensureCapacityHelper(elementCount + numNew);
//元素數組複制,擴容,并将添加到向量末尾
System.arraycopy(a, , elementData, elementCount, numNew);
//更新元素數
elementCount += numNew;
//傳回是否新增了元素
return numNew != ;
}
E get(int index):傳回向量中指定位置的元素
public synchronized E get(int index) {
//數組邊界檢查
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
return (E)elementData[index];
}
E set(int index, E element):用指定的元素替換此向量中指定位置處的元素。
public synchronized E set(int index, E element) {
//數組邊界檢查
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
//擷取對應索引原有元素值
Object oldValue = elementData[index];
//設定新的元素值
elementData[index] = element;
//傳回原有元素值
return (E)oldValue;
}
boolean remove(Object o):引用塊内容移除此向量中指定元素的第一個比對項,如果向量不包含該元素,則元素保持不變。
public boolean remove(Object o) {
return removeElement(o);
}
public synchronized boolean removeElement(Object obj) {
//修改次數+1
modCount++;
int i = indexOf(obj);
if (i >= ) {
removeElementAt(i);
return true;
}
return false;
}
/**
*傳回此向量中第一次出現的指定元素的索引,如果此向量不包含該元素,則傳回 -1。
*/
public int indexOf(Object o) {
return indexOf(o, );
}
/**
*傳回此向量中第一次出現的指定元素的索引,從 index 處正向搜尋,如果未找到該元素,則傳回 -1。
*/
public synchronized int indexOf(Object o, int index) {
if (o == null) {//元素為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 -;
}
由源碼可看成之是以判斷元素是否為NULL,是NULL則使用“==”比對,否則使用equals,是為了防止NULL也使用equals會出現空指針異常。
E remove(int index): 移除此向量中指定位置的元素。
public synchronized E remove(int index) {
//修改次數+1
modCount++;
//數組邊界檢查
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
//擷取原有數值
Object oldValue = elementData[index];
//計算要移動的元素數
int numMoved = elementCount - index - ;
if (numMoved > )
//複制元素,并将元素整體前移一位,從索引index+1開始
//此時倒數第二個元素和末尾元素相同
System.arraycopy(elementData, index+, elementData, index,
numMoved);
//設定末尾為空,等待垃圾回收
elementData[--elementCount] = null; // Let gc do its work
//傳回被移除的元素
return (E)oldValue;
}
boolean removeAll(Collection c):從此向量中移除包含在指定 Collection 中的所有元素。
public synchronized boolean removeAll(Collection<?> c) {
//調用的父類(AbstractList)的父類(AbstractCollection)
return super.removeAll(c);
}
/**
* 移除此 collection 中那些也包含在指定 collection 中的所有元素(可選操作)。
*/
public boolean removeAll(Collection<?> c) {
boolean modified = false;
Iterator<?> e = iterator();
while (e.hasNext()) {
if (c.contains(e.next())) {
e.remove();
modified = true;
}
}
return modified;
}
Iterator iterator(): 傳回以恰當順序在此清單的元素上進行疊代的疊代器。
Vector其實沒有重寫此方法,下面源碼是其父類的。
/**
*AbstractList父類的方法,具體參照前面文章
*/
public Iterator<E> iterator() {
return new Itr();
}