Java集合類之Vector

Vector原始碼解析

老樣子,話不多說先上一張UML類圖

Java集合類之Vector

Java集合類之Vector

Vector的初始化構造方法

無參構造(預設呼叫初始化容量的構造方法,預設容量為10)

public Vector() { //這裡會呼叫Vector帶容量引數的構造方法預設容量為10 //這裡和ArrayList不同的是ArrayList在呼叫add方法才初始化容量 this(10);}

指定初始化容量大小

public Vector(int initialCapacity) { //這裡呼叫下面指定初始化容量和增長係數的構造方法,預設增長係數為0 this(initialCapacity, 0);}

指定初始化容量和增長係數

public Vector(int initialCapacity, int capacityIncrement) { super(); //容量不能小於0 if (initialCapacity < 0) throw new IllegalArgumentException(“Illegal Capacity: ”+ initialCapacity); //為物件陣列指定初始化容量 this。elementData = new Object[initialCapacity]; //設定增長係數 this。capacityIncrement = capacityIncrement;}

使用另外一個集合構造該集合

public Vector(Collection<? extends E> c) { //將傳入集合轉成陣列 elementData = c。toArray(); //Vector的元素個數就是集合的長度 elementCount = elementData。length; // c。toArray might (incorrectly) not return Object[] (see 6260652) //判斷新增進的元素物件是否為物件陣列型別 if (elementData。getClass() != Object[]。class) //進行淺複製將型別轉化為物件陣列 elementData = Arrays。copyOf(elementData, elementCount, Object[]。class);}

add(E e)方法

public synchronized boolean add(E e) { //更新操作次數 modCount++; //確保可以儲存元素的容量,如有必要會進行擴容 ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = e; return true;}//如有必要,增加當前陣列的容量,以確保至少可以儲存minCapacity容量引數指定的元素個數private void ensureCapacityHelper(int minCapacity) { // overflow-conscious code if (minCapacity - elementData。length > 0) //Vector的擴容方法 grow(minCapacity);}//擴容方法private void grow(int minCapacity) { // overflow-conscious code //原Vector容量值 int oldCapacity = elementData。length; //如果有給capacityIncrement設定增長係數的話,就加上該係數值來擴容,否則將原先的陣列容量變為2*oldCapacity 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;}

add(int index, E element)方法(指定位置新增元素)

public void add(int index, E element) { insertElementAt(element, index);}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++;}

addAll(Collection<? extends E> c)方法

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;}

remove(int index)方法

public synchronized E remove(int index) { modCount++; //對index進行邊界檢查 if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); //獲取要刪除座標的元素 E oldValue = elementData(index); //計算指定索引與末尾元素的個數 int numMoved = elementCount - index - 1; //如果個數大於0 if (numMoved > 0) //將指定索引到末尾的元素分別往左移動一位 System。arraycopy(elementData, index+1, elementData, index, numMoved); //遞減元素個數,並將末尾元素置空 elementData[——elementCount] = null; // Let gc do its work return oldValue;}

remove(Object o)方法

public boolean remove(Object o) { //呼叫removeElement(Object obj)方法 return removeElement(o);}public synchronized boolean removeElement(Object obj) { //更新操作次數 modCount++; //得到要刪除元素的座標 int i = indexOf(obj); if (i >= 0) { //根據座標刪除元素 removeElementAt(i); return true; } return false;}public synchronized void removeElementAt(int index) { modCount++; //對index進行邊界檢查 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 */}

removeAll(Collection<?> c)方法

//刪除指定集合中的所有元素public synchronized boolean removeAll(Collection<?> c) { //呼叫AbstractCollection中的removeAll的方法 return super。removeAll(c);}

retainAll(Collection<?> c)方法

//刪除非集合c中的元素public synchronized boolean retainAll(Collection<?> c) { return super。retainAll(c);}

clear()方法

public void clear() { //清空集合方法呼叫上面removeAllElements方法 removeAllElements();}

get(int index)方法

public synchronized E get(int index) { //對index進行邊界檢查 if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); //根據下標獲取元素 return elementData(index);}E elementData(int index) { //在陣列中根據下標獲取元素 return (E) elementData[index];}

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); //將set的元素放入陣列指定下標 elementData[index] = element; return oldValue;}

總結:

Vector是使用陣列儲存資料,和ArrayList一樣

在無參的構造方法中,預設的初始容量為10,增長係數為0比ArrayList多了一個增長係數的概念

Vector類是執行緒安全的List,其底層是透過Synchronized關鍵字實現的(同步方法),ArrayList是執行緒不安全的

擴容機制:如果增長係數不位 0 那麼就是當前容量 + 增長係數,否則就是2倍,ArrayList為1。5倍左右