1. 程式人生 > >Vector源碼

Vector源碼

指定 添加 array 調用 rect abstract static div time

Vector與ArrayList類似,都是使用動態數組實現 Vector是線程安全的 代碼:
public class Vector<E>
    extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{

    實際用於存儲Vector數據的數組
    protected Object[] elementData;

    Vector大小
    protected int elementCount;

    
/** * The amount by which the capacity of the vector is automatically * incremented when its size becomes greater than its capacity. If * the capacity increment is less than or equal to zero, the capacity * of the vector is doubled each time it needs to grow. * * @serial
*/ 當Vector的size大於容量時,Vector容量自動增加capacityIncrement 如果容量增加量小於等於0,則Vector容量變為2倍 protected int 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; } 定義初始容量 public Vector(int initialCapacity) { this(initialCapacity, 0); } 默認10容量大小 public Vector() { this(10); } 將集合c轉為數組 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); } 將數組對象拷貝到elementData中 public synchronized void copyInto(Object[] anArray) { System.arraycopy(elementData, 0, anArray, 0, elementCount); } 根據elementCount整理elementData數組大小 public synchronized void trimToSize() { modCount++; int oldCapacity = elementData.length; if (elementCount < oldCapacity) { elementData = Arrays.copyOf(elementData, elementCount); } } 如果最小容量>0,則擴容 public synchronized void ensureCapacity(int minCapacity) { if (minCapacity > 0) { modCount++; ensureCapacityHelper(minCapacity); } } 如果最小容量-當前容量>0,則擴容 private void ensureCapacityHelper(int minCapacity) { // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); } Vector最大數量 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; //根據minCapacity擴展數組容量, //先獲得當前數組容量oldCapacity, //如果capacityIncrement > 0則newCapacity大小=(oldCapacity+capacityIncrement),否則newCapacity大小=(oldCapacity+oldCapacity) //如果newCapacity-minCapacity<0,則設置elementData為minCapacity大小 //如果newCapacity-數組允許的最大值>0,則調用hugeCapacity方法進行擴容 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); } //如果最新容量小於0,報錯內存溢出 //否則設置最小容量>MAX_ARRAY_SIZE,則數組大小設置為Integer最大值,否則設置為(Integer最大值-8) private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; } 設置vector大小,如果newSize大於原有數組大小,則進行擴容, 如果newSize小於等於elementCount,則把elementCount-newSize剩余的索引設置為null 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; } 同步返回數組大小 public synchronized int capacity() { return elementData.length; } 同步返回vector容量 public synchronized int size() { return elementCount; } 判斷vector容量是否為0 public synchronized boolean isEmpty() { return elementCount == 0; } 數組中是否包含o對象 public boolean contains(Object o) { return indexOf(o, 0) >= 0; } 返回o對象所在的索引 public int indexOf(Object o) { return indexOf(o, 0); } 同步的返回o對象從index開始出現第一次的位置 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; } 同步的返回o對象最後出現的索引 public synchronized int lastIndexOf(Object o) { return lastIndexOf(o, elementCount-1); } 同步的返回o對象從index開始出現最後一次的位置 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; } 同步返回index索引的數據 public synchronized E elementAt(int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } return elementData(index); } 返回數組第一個數據 public synchronized E firstElement() { if (elementCount == 0) { throw new NoSuchElementException(); } return elementData(0); } 返回數組最後一個數據 public synchronized E lastElement() { if (elementCount == 0) { throw new NoSuchElementException(); } return elementData(elementCount - 1); } 在index索引的值設置為obj public synchronized void setElementAt(E obj, int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } elementData[index] = obj; } 刪除index索引的值,通過拷貝的方式 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 */ } 在index索引位置插入obj對象,通過拷貝的方式 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++; } 在數組最後添加obj對象,判斷是否需要進行擴容 public synchronized void addElement(E obj) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = obj; } 刪除是數組中第一個等於obj的值的數據 public synchronized boolean removeElement(Object obj) { modCount++; int i = indexOf(obj); if (i >= 0) { removeElementAt(i); return true; } return false; } 把數組所有值設置為null,容量設置為0 public synchronized void removeAllElements() { modCount++; // Let gc do its work for (int i = 0; i < elementCount; i++) elementData[i] = null; elementCount = 0; } 克隆方法,元素本身不會被復制,只拷貝了指針,克隆後的數組與原有數組相同 public synchronized Object clone() { try { @SuppressWarnings("unchecked") Vector<E> v = (Vector<E>) super.clone(); v.elementData = Arrays.copyOf(elementData, elementCount); v.modCount = 0; return v; } catch (CloneNotSupportedException e) { // this shouldn‘t happen, since we are Cloneable throw new InternalError(e); } } 把vector轉為數組對象 public synchronized Object[] toArray() { return Arrays.copyOf(elementData, elementCount); } 返回數組 @SuppressWarnings("unchecked") public synchronized <T> T[] toArray(T[] a) { if (a.length < elementCount) return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass()); System.arraycopy(elementData, 0, a, 0, elementCount); if (a.length > elementCount) a[elementCount] = null; return a; } 返回index索引的值 @SuppressWarnings("unchecked") E elementData(int index) { return (E) elementData[index]; } 返回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; } 刪除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; } 清空數組 public void clear() { removeAllElements(); } // Bulk Operations 調用ArrayList中的包含方法 public synchronized boolean containsAll(Collection<?> c) { return super.containsAll(c); } 把集合c添加到vector中 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; } 調用ArrayList中的刪除方法:根據集合批量刪除數據 public synchronized boolean removeAll(Collection<?> c) { return super.removeAll(c); } 調動ArrayList中的方法:根據集合批量保留數組中的數據:數組中在c集合中存在的對象 public synchronized boolean retainAll(Collection<?> c) { return super.retainAll(c); } 在指定索引添加集合c public synchronized boolean addAll(int index, Collection<? extends E> c) { modCount++; if (index < 0 || index > elementCount) throw new ArrayIndexOutOfBoundsException(index); Object[] a = c.toArray(); int numNew = a.length; ensureCapacityHelper(elementCount + numNew); int numMoved = elementCount - index; if (numMoved > 0) System.arraycopy(elementData, index, elementData, index + numNew, numMoved); System.arraycopy(a, 0, elementData, index, numNew); elementCount += numNew; return numNew != 0; } }

Vector源碼