源碼學習之Vector

Vector源碼分析學習

  1. 同樣,首先是Vector的定義

    //繼承AbstractList抽線類,實現了List、RandomAccess、Cloneable和Serializable接口
    public class Vector<E>
        extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
  2. Vector源碼開頭定義的幾個成員變量

    //這個就是實際存儲數據的數組,從這個可以看出Vector是數組實現的
    protected Object[] elementData;
    //Vector內有效元素的數量
    protected int elementCount;
    //Vector容量增量,當capacityIncrement>0時,需要擴容時,增加的的是capacityIncrement,否則增加的容量是oldCapacity
    protected int capacityIncrement;
  3. 構造函數,Vector一共提供了三個構造函數。

        //構造函數,initialCapacity初始化容量,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;
        }
         //構造函數,initialCapacity初始化容量,capacityIncrement設置爲0
        public Vector(int initialCapacity) {
            this(initialCapacity, 0);
        }
         //構造函數,initialCapacity設置爲10,capacityIncrement設置爲0
        public Vector() {
            this(10);
        }
         //構造函數,從另外一個集合初始化Vector
        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);
        }
  4. Vector擴容機制

      //保證容量
        public synchronized void ensureCapacity(int minCapacity) {
            if (minCapacity > 0) {
                modCount++;
                ensureCapacityHelper(minCapacity);
            }
        }
      //保證容量,如果minCapacity大於Vector的容量,那麼需要擴容
        private void ensureCapacityHelper(int minCapacity) {
            // overflow-conscious code
            if (minCapacity - elementData.length > 0)
                grow(minCapacity);
        }
      private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
    
        private void grow(int minCapacity) {
            // overflow-conscious code
            //如果capacityIncrement大於0的時候,擴容capacityIncrement,否則擴容oldCapacity
            int oldCapacity = elementData.length;
            int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                             capacityIncrement : oldCapacity);
            //如果新容量不能滿足最小容量要求,那麼新容量設置成minCapacity
            if (newCapacity - minCapacity < 0)
                newCapacity = minCapacity;
          //如果新容量大於MAX_ARRAY_SIZE,那麼新容量設置成Integer.MAX_VALUE
            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;
        }
  5. Vector常用操作

     //返回Vector的size()
        public synchronized int size() {
            return elementCount;
        }
     //判斷Vector是否是空
        public synchronized boolean isEmpty() {
            return elementCount == 0;
        }
     //判斷是否包含o元素
     public boolean contains(Object o) {
            return indexOf(o, 0) >= 0;
        }
     //返回第一個出現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;
        }
    //獲取index下標的元素值
        public synchronized E get(int index) {
            if (index >= elementCount)
                throw new ArrayIndexOutOfBoundsException(index);
    
            return elementData(index);
        }   
    //設置index,下標的元素值爲element
        public synchronized E set(int index, E element) {
            if (index >= elementCount)
                throw new ArrayIndexOutOfBoundsException(index);
    
            E oldValue = elementData(index);
            elementData[index] = element;
            return oldValue;
        }
    //增加一個元素e
        public synchronized boolean add(E e) {
            modCount++;
            //檢查是否需要擴容
            ensureCapacityHelper(elementCount + 1);
            elementData[elementCount++] = e;
            return true;
        }
     //移除vector中等於o的元素
        public boolean remove(Object o) {
            return removeElement(o);
        }
     //在下標index處增加了一個element
        public void add(int index, E element) {
            insertElementAt(element, index);
        }
       //移除下標爲index的元素
        public synchronized E remove(int index) {
            modCount++;
            if (index >= elementCount)
                throw new ArrayIndexOutOfBoundsException(index);
            E oldValue = elementData(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;
        }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章