Vector詳解及源碼分析

1.繼承與實現體系:

在這裏插入圖片描述

2. vector簡單介紹

vector與ArrayList一樣,同屬於AbstractLists的子類,但與ArrayList不同的是,它對於內部對元素的操作方法都是同步的,即線程安全的,但隨之而來的就是效率問題,它的操作效率會略低於ArrayList

3 vector的主要屬性介紹:

    //vector內部維護的數組,用於儲存元素
    protected Object[] elementData;

   //存儲元素的長度大小
    protected int elementCount;

    //擴容增長量,默認是0,當是0時,每次擴容按原量的1倍擴;若大於0,就按該量來擴容
    protected int capacityIncrement;

4.vector的構造方法:

//確定初始容量的大小,及擴容增長量
 public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
        this.capacityIncrement = capacityIncrement;
    }

   //確定初始容量大小,並設置擴容增長量爲0,這種情況下,每次擴容會擴容至原來的2倍
    public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }

   //無參構造方法,設置初容量大小爲10
    public Vector() {
        this(10);
    }

	//將集合複製給elementData
	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);
    }

5.Vector的增刪方法介紹:

add(E e);

 public synchronized boolean add(E e) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = e;
        return true;
    }
  1. 首先,操作次數加一
  2. 調用ensureCapacityHelper(int minCapacity),參數爲當前元素的數量加一
private void ensureCapacityHelper(int minCapacity) {
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

該方法是判斷是否需要擴容,如果增長之後的元素數量大於數組的大小,即需要擴容,調用grow(int 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);
    }

該方法就是擴容的方法,這裏就用到了之前構造函數指定的擴容增長量:①capacityIncrement 。如果該量大於0,就增加該量的大小;如果該量小於等於0,則擴容至原量的2倍
②如果擴容之後的量還是小於添加元素之後的容量大小,就擴容至增加元素之後的容量大小
③複製數組

  1. 將元素賦到數組中

remove(Object o)

public boolean remove(Object o) {
        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++;
       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 */
   }

跟ArrayList一樣,最重要的就是理解擴容機制。

刪除方法跟增加方法差不多,都是通過複製數組的方式對元素進行操作,而這裏就是ArrayList、vector與LinkedList對於增刪操作效率差異的原因了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章