java 源碼閱讀(四)Vector

java 源碼閱讀(四)Vector

Vector是一種變長集合類,基於數組實現。ArrayList允許空值和重複元素。當往Vector中添加的元素數量超過底層數量時,會進行擴容。Vector實現了RandomAccess接口,所以可以保證在O(1)複雜度下完成隨機查找操作。是一個線程安全類。

繼承/實現

在這裏插入圖片描述

構造函數

protected Object[] elementData;
protected int elementCount;
//擴容時增加的數組長度,默認爲0
protected int capacityIncrement;
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

Vector()

public Vector() {
    this(10);
}

Vector(int)

public Vector(int initialCapacity) {
    this(initialCapacity, 0);
}

Vector(int,int)

public Vector(int initialCapacity, int capacityIncrement) {
    super();
    if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal Capacity: "+
                                           initialCapacity);
    this.elementData = new Object[initialCapacity];
    this.capacityIncrement = capacityIncrement;
}

構造函數比較簡單,不再贅述。

方法源碼解讀

add(E)

public synchronized boolean add(E e) {
    modCount++;
    ensureCapacityHelper(elementCount + 1);
    elementData[elementCount++] = e;
    return true;
}
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;
  	//如果capacityIncrement沒有主動賦值,默認爲0.擴容爲10。
    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,E)

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

基本流程與arrayList一致。

get(int)

public synchronized E get(int index) {
    if (index >= elementCount)
        throw new ArrayIndexOutOfBoundsException(index);

    return elementData(index);
}
E elementData(int index) {
    return (E) elementData[index];
}

代碼很簡單。

remove(int)

public synchronized E remove(int index) {
    modCount++;
  	//驗證index是否超出邊界
    if (index >= elementCount)
        throw new ArrayIndexOutOfBoundsException(index);
    E oldValue = elementData(index);
	//取出index位置的數據,然後拷貝數組,將末尾數組元素置爲空
    int numMoved = elementCount - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--elementCount] = null; // Let gc do its work

    return oldValue;
}

remove(Object)

public boolean remove(Object o) {
    return removeElement(o);
}
public synchronized boolean removeElement(Object obj) {
    modCount++;
  	//確定obj的index
    int i = indexOf(obj);
    if (i >= 0) {
        removeElementAt(i);
        return true;
    }
    return false;
}
 public int indexOf(Object o) {
        return indexOf(o, 0);
  }
public synchronized int indexOf(Object o, int index) {
  	//對null進行了處理,然後循環遍歷。直到匹配到o爲止。
    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;
}
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一致。

看完源碼,發現vector保證線程安全是靠着Synchronized關鍵字。

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