ArrayList expansion and Vector

    public boolean add(E e) {
        ensureCapacityInternal(size + 1); // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
Private  void  ensureCapacityInternal ( int minCapacity) {
         // if it is empty array 
        IF (== elementData of EMPTY_ELEMENTDATA) {
             // get larger and minCapacity 10 
            minCapacity = Math.max (DEFAULT_CAPACITY, minCapacity);
        }
        // if the array has a data 
        ensureExplicitCapacity (minCapacity);
    }

    // ensure that the array larger than the number of elements in ArrayList 
    Private  void  ensureExplicitCapacity ( int be minCapacity) {
        modCount ++; // will "revise statistics" +1

        // If the actual data capacity larger than the capacity of the array, the array expansion give 
        IF (be minCapacity - elementData.length> 0 )
             Grow (be minCapacity);
    }
// increase spatial array 
    Private  void  Grow ( int be minCapacity) {
         // overflow-Conscious code 
        int oldCapacity = elementData.length;
         int newCapacity = oldCapacity + (oldCapacity >>. 1); // add capacity on the basis of the original
                                                             / / oldCapacity / 2 
        IF (newCapacity - be minCapacity <0 )
            newCapacity = minCapacity; // get a minimum guaranteed capacity and minCapacity as 
        IF (newCapacity - MAX_ARRAY_SIZE> 0 )
            newCapacity = hugeCapacity (be minCapacity); // can not exceed the maximum capacity
         // be minCapacity IS usually Close to size, the this SO IS A win: 
        elementData of = Arrays.copyOf (elementData of, newCapacity);
    }

1, ArraList underlying data is stored in an Object [] elementData array inside

2, after the JDK, only the default elementData add method in which the size of 10

3, there is an array of ArrayList, entered, and add elements:

        If the actual capacity of the array is greater than + 1 when the storage capacity of the array, the expansion starts, each expansion 1.5

 

Vector is thread safe (for syncronized keyword) , but the performance is lower than the ArrayList.

ArrayList, Vector is the main difference between the following points:

( 1): the Vector is thread-safe, there are a lot of synchronized source code can be seen, rather than ArrayList. Vector and ArrayList result in efficiency can not be compared;

( 2): the ArrayList and Vector are continuous linear memory space, when the storage space is insufficient, increasing the ArrayList original default 50%, Vector is increased to twice the original defaults (* 2);

( 3): You can set the Vector capacityIncrement, but not ArrayList, from the literal meaning is the capacity capacity, Increment increased capacity growth parameters.

        

 

 

     

      

Guess you like

Origin www.cnblogs.com/pickKnow/p/11166147.html
Recommended