JDK Vectorソース学習


public class Vector<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable

 

         synchronized     Vector      

 

Vector     api  ArrayList    

                                             Arrays.copyOf()            System.arrayCopy()     native   

 

 

 

4     

 

public Vector() {
 this(10);
}

 

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

 

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


    protected int capacityIncrement; //           

 

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

 

          

public synchronized void ensureCapacity(int minCapacity) {
     modCount++;
     ensureCapacityHelper(minCapacity);
}

 

private void ensureCapacityHelper(int minCapacity) {
 int oldCapacity = elementData.length;
 if (minCapacity > oldCapacity) {
     Object[] oldData = elementData;
     int newCapacity = (capacityIncrement > 0) ?            // vector    capacityIncrement             
  (oldCapacity + capacityIncrement) : (oldCapacity * 2);          2 
         if (newCapacity < minCapacity) {
          newCapacity = minCapacity;
     }
            elementData = Arrays.copyOf(elementData, newCapacity);
   }
}

 

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 boolean add(E e) {
    modCount++;
    ensureCapacityHelper(elementCount + 1);
    elementData[elementCount++] = e;
    return true;
}

public synchronized int capacity() {   //        
 return elementData.length;
}

 

public synchronized int size() {  //         
 return elementCount;
}