Vector結(jié)構(gòu)圖
Vector.png
Vector主要方法
- public synchronized boolean add(E e);
- public synchronized E set(int index, E element) ;
- public synchronized E get(int index);
- public boolean remove(Object o);
Vector解讀主要方法
來(lái)看一下public synchronized boolean add(E e)源碼
//首先晌坤,需要說(shuō)明的是這個(gè)synchronized吧享,這是加鎖操作秃嗜,保證線程安全
public synchronized boolean add(E e) {
modCount++;
//確保容量
ensureCapacityHelper(elementCount + 1);
//添加元素橙垢,數(shù)量加一
elementData[elementCount++] = e;
return true;
}
//這些操作和ArrayList的一致背犯,可以參考ArrayList
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
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) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
來(lái)看一下public synchronized E set(int index, E element) 源碼
public synchronized E set(int index, E element) {
//判斷index位置是否合理
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
//獲取index位置老元素
E oldValue = elementData(index);
//替換index位置為新元素
elementData[index] = element;
return oldValue;
}
來(lái)看一下public synchronized E get(int index)源碼
public synchronized E get(int index) {
//判斷index位置是否合理
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
//獲取index位置元素
return elementData(index);
}
來(lái)看一下public boolean remove(Object o)源碼
//首先很奇怪的是這個(gè)方法沒(méi)有加鎖
public boolean remove(Object o) {
//來(lái)看一下真正的實(shí)現(xiàn)
return removeElement(o);
}
//其實(shí)還是加鎖了
public synchronized boolean removeElement(Object obj) {
//修改modCount
modCount++;
//找到待刪除元素的位置
int i = indexOf(obj);
if (i >= 0) {
//真正的刪除元素
removeElementAt(i);
return true;
}
return false;
}
//刪除index
public synchronized void removeElementAt(int index) {
modCount++;
//判斷刪除位置是否合理
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
//確定需要移動(dòng)元素個(gè)數(shù)
int j = elementCount - index - 1;
if (j > 0) {
//用index后一個(gè)元素覆蓋index玻墅,依次移動(dòng)装诡,一共移動(dòng)j個(gè)元素
System.arraycopy(elementData, index + 1, elementData, index, j);
}
//總數(shù)減一吞歼,最后一個(gè)元素置空
elementCount--;
elementData[elementCount] = null; /* to let gc do its work */
}
Vector遍歷介紹
常用的三種遍歷方式:
//one foreach 遍歷
for (Object o : list) {
System.out.println(o);
}
// two 隨機(jī)訪問(wèn)
for (int i = 0; i <list.size(); i++) {
System.out.println(list.get(i));
}
//three 迭代器的遍歷
Iterator iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
Vector其他特性介紹
- 首先圈膏,vector是用數(shù)組實(shí)現(xiàn)的,所以ArrayList的數(shù)組特性vector也是有的篙骡,其次稽坤,vector是有synchronized鎖機(jī)制的,所以他是線程安全的糯俗,最后vector也會(huì)拋出ConcurrentModificationException這個(gè)異常尿褪。這個(gè)我有點(diǎn)不明白,為啥線程安全了還會(huì)拋出這個(gè)異常呢得湘?每次當(dāng)有線程做刪除操作的時(shí)候杖玲,就不應(yīng)該有線程可以去get或者set了呀! 這個(gè)問(wèn)題TODO處理淘正,希望有網(wǎng)友可以解決我的疑惑摆马。
- vector默認(rèn)的初始值是10,private static final int DEFAULT_CAPACITY = 10;每次擴(kuò)容的容量為: int newCapacity = oldCapacity + (oldCapacity >> 1);約1.5倍