Java容器類源碼-Vector的最全的源碼分析(二)

三、源碼解讀

1. 繼承、實現(xiàn)

extends:AbstractList

implements:List, RandomAccess, Cloneable, java.io.Serializable

2. 全局變量

(1) 存放數(shù)據(jù)的數(shù)組

protected Object[] elementData;

(2) 存放數(shù)量

protected int elementCount;

(3) 容量增量

protected int capacityIncrement;

3. 構(gòu)造方法

(1) 不帶參數(shù)的構(gòu)造方法

public Vector() {

this(10);

}

(2) 帶初始化容量大小的構(gòu)造方法

/**

* @param initialCapacity 初始化容量大小

*/

public Vector(int initialCapacity) {

this(initialCapacity, 0);

}

(3) 帶初始化容量和容量增量的構(gòu)造方法

/**

* @param initialCapacity初始化容量大小

* @param 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;

}

(4) 帶Colleciton參數(shù)的構(gòu)造方法

/**

* @param c 將c轉(zhuǎn)為Vector

*/

public Vector(Collection 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);

}

4. 方法

(1) public synchronized void copyInto(Object[] anArray)

源碼解釋:

通過JNI調(diào)用c++庫的arraycopy方法,實現(xiàn)將anArray數(shù)組復(fù)制到當(dāng)前的Vector。

public synchronized void copyInto(Object[] anArray) {

System.arraycopy(elementData, 0, anArray, 0, elementCount);

}

(2) public synchronized void trimToSize()

源碼解釋:

用以優(yōu)化Vector的內(nèi)存,我們都知道,Vector的每次容量增量是當(dāng)前大小的2倍,但是當(dāng)我們沒法用完申請的這么多內(nèi)存時,我們可以通過調(diào)用這個方法用以將不需要的內(nèi)存釋放掉。

public synchronized void trimToSize() {

modCount++;// 修改次數(shù)增加

int oldCapacity = elementData.length;// 獲取到當(dāng)前的申請的內(nèi)存大小

if (elementCount < oldCapacity) {// 如果當(dāng)前存放的數(shù)量比申請的內(nèi)存要少

elementData = Arrays.copyOf(elementData, elementCount);// 重新為這個數(shù)組申請elementCount大小內(nèi)存,并存放這些數(shù)據(jù)

}

}

(3) public synchronized void ensureCapacity(int minCapacity)

源碼解釋:

當(dāng)要擴(kuò)容時,會調(diào)用此方法,保證當(dāng)前容量能存放得下所需要存放的元素數(shù)量。如果不夠時,會調(diào)用grow()方法進(jìn)行擴(kuò)容。

public synchronized void ensureCapacity(int minCapacity) {

if (minCapacity > 0) {

modCount++;

ensureCapacityHelper(minCapacity);

}

}

private void ensureCapacityHelper(int minCapacity) {

// overflow-conscious code

if (minCapacity - elementData.length > 0)

grow(minCapacity);// 如果當(dāng)前容量比數(shù)組的長度要小時,調(diào)用grow擴(kuò)容

}

private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

/**

* Vector的擴(kuò)容方法, 每次擴(kuò)容至2倍

* @param minCapacity

*/

private void grow(int minCapacity) {

// overflow-conscious code

int oldCapacity = elementData.length;// 獲取到數(shù)組的長度

int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); //如果沒有初始化capacityIncrement,則增加至兩倍

if (newCapacity - minCapacity < 0)

newCapacity = minCapacity;

if (newCapacity - MAX_ARRAY_SIZE > 0)// 如果擴(kuò)容后的容量超過了最大容量

newCapacity = hugeCapacity(minCapacity);// 調(diào)用hugeCapacity方法

elementData = Arrays.copyOf(elementData, newCapacity);// 將數(shù)組復(fù)制到擴(kuò)容后的新內(nèi)存中去

}

private static int hugeCapacity(int minCapacity) {

if (minCapacity < 0) // overflow

throw new OutOfMemoryError();

return (minCapacity > MAX_ARRAY_SIZE) ?

Integer.MAX_VALUE :

MAX_ARRAY_SIZE;

}

(4) public synchronized void setSize(int newSize)

源碼解釋:

修改容量,當(dāng)newSize比數(shù)組的長度要大時,將其復(fù)制到新的內(nèi)存區(qū)域,如果要小的話,則從newSize位置到數(shù)組的最后一個位置的所有元素置為空。

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;

}

(5) public synchronized int capacity()

源碼解釋:

返回數(shù)組長度,即申請內(nèi)存的長度。

public synchronized int capacity() {

return elementData.length;

}

(6) public synchronized int size()

源碼解釋:

返回數(shù)組已經(jīng)使用的長度,即存放的數(shù)據(jù)個數(shù)。

public synchronized int size() {

return elementCount;

}

(7) public synchronized boolean isEmpty()

源碼解釋:

判空,如果數(shù)量為0,即為empty。

public synchronized boolean isEmpty() {

return elementCount == 0;

}

(8) public Enumeration<E> elements()

源碼解釋:

返回一個Enumeration對象的序列。Enumeration只有兩個方法,hasMoreElements()和nextElement(),它只能從首個元素遍歷到最后一個元素,并不能根據(jù)位置拿到具體的元素。

public Enumeration elements() {

return new Enumeration() {

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");

}

};

}

(9) public boolean contains(Object o)

源碼解釋:

是否包含對象o。調(diào)用indexOf判斷是否存在。

public boolean contains(Object o) {

return indexOf(o, 0) >= 0;

}

public int indexOf(Object o) {

return indexOf(o, 0);

}

(10) public synchronized int indexOf(Object o, int index)

源碼解釋:

判斷o是否為空,如果為空,則遍歷是否存在值為空的元素;不為空,判斷是否存在和o相等的元素。

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;

}

(11) public synchronized int lastIndexOf

源碼解釋:

獲取該元素所處的最后一個位置,不存在則返回-1

public synchronized int lastIndexOf(Object o) {

return lastIndexOf(o, elementCount-1);

}

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;

}

(12) public synchronized E elementAt(int index)

源碼解釋:

返回這個位置的元素。其實就是判斷數(shù)組的index位置是否有元素

public synchronized E elementAt(int index) {

if (index >= elementCount) {

throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);

}

return elementData(index);

}

E elementData(int index) {

return (E) elementData[index];

}

(13) public synchronized E firstElement()

源碼解釋:

返回數(shù)組第0個位置的元素。

public synchronized E firstElement() {

if (elementCount == 0) {

throw new NoSuchElementException();

}

return elementData(0);

}

(14) public synchronized E lastElement()

源碼解釋:

返回數(shù)組最后一個位置的元素。

public synchronized E lastElement() {

if (elementCount == 0) {

throw new NoSuchElementException();

}

return elementData(elementCount - 1);

}

(15) public synchronized void setElementAt(E obj, int index)

源碼解釋:

修改第index位置的值為obj。其實就是數(shù)組賦值。

public synchronized void setElementAt(E obj, int index) {

if (index >= elementCount) {

throw new ArrayIndexOutOfBoundsException(index + " >= " +

elementCount);

}

elementData[index] = obj;

}

每天都在分享文章,也每天都有人想要我出來給大家分享下怎么去學(xué)習(xí)Java。大家都知道,我們是學(xué)Java全棧的,大家就肯定以為我有全套的Java系統(tǒng)教程。沒錯,我是有Java全套系統(tǒng)教程,進(jìn)扣裙【47】974【9726】所示,進(jìn)群的時候記得表明自己想要學(xué)習(xí)什么,不要用小號,這樣小編才好給你們發(fā)定向資源,今天小編就免費送!~

“我們相信人人都可以成為一個程序員,現(xiàn)在開始,找個師兄,帶你入門,學(xué)習(xí)的路上不再迷茫。這里是ja+va修真院,初學(xué)者轉(zhuǎn)行到互聯(lián)網(wǎng)行業(yè)的聚集地。"

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 童話故事《蘿卜房子》講了這么一個故事: 一位老爺爺在床底下發(fā)現(xiàn)了一顆蘿卜苗,老爺爺說:“就讓它長著吧,說不定能長出...
    我是長弓閱讀 20,267評論 12 50
  • 其實我還是蠻喜歡做服務(wù)行業(yè)的。 今天晚上繼續(xù)在學(xué)校里的便利店做做兼職。學(xué)校的小商店自然是零食和飲料為主。有句老話說...
    愚人cc閱讀 383評論 1 2
  • 早上又是“嗡嗡嗡,嗡嗡嗡”的聲音把我吵醒了。媽媽說:“咱娘倆還是快出去‘避避難’吧!這樣我可受不了!”“好...
    n寧n閱讀 572評論 0 1

友情鏈接更多精彩內(nèi)容