java interview guide 2019-9-16 (arraylist)

ArrayList source code analysis

System.arraycopy () and Arrays.copyOf () method

  By source above, we found that the two methods achieve an array copy is widely used in many places and are particularly clever. For example, the following add (int index, E element) method is very cleverly used the arraycopy () method allows the array to realize his own copy so that all the members of the index after the start of a shift position:

/ ** 
     * specified position in the list of the specified element is inserted. 
     * First call on the index rangeCheckForAdd perform limit checks; then call ensureCapacityInternal way to ensure capacity is large enough; 
     * then will index all the members after the start of a shift position; the element inserted into the index position; the final size plus one. 
     * / 
    Public  void the Add ( int index, E Element) { 
        rangeCheckForAdd (index); 

        ensureCapacityInternal (size +. 1);   // Increments ModCount !!
         // The arraycopy () method implementation replicate their own array
         // elementData of: the source array; index : the start position of the source array; elementData of: a target array; index + 1: the starting position of the target array; size - index: the number of array elements to be copied; 
        System.arraycopy (elementData of, index, elementData of, index + . 1, size - index); 
        elementData of [index]= element;
        size++;
    }

 Another example toArray () method uses copyOf () method

/ ** 
     * in the proper order (from the first to the last element) Returns a list of all of the elements comprises an array. 
     * Returned array will be "safe", because the list does not retain a reference to it. (In other words, this method must allocate a new array). 
     * Therefore, the caller is free to modify the returned array. This method acts as a bridge between the array and based on the set-based API. 
     * / 
    Public Object [] toArray () {
     // elementData of: an array to be copied; size: length to copy 
        return Arrays.copyOf (elementData of, size); 
    }

 

Both connections and differences

Contact: look at both the source code can be found copyOf()internally calls the System.arraycopy()difference method:

  1. arraycopy () needs of the target array, the original array to array copy your own definition, the starting point and can select the location and length of the copy, and into a new array

  2. copyOf () is the system automatically creates a new array within, and returns the array.

Hair core expansion method, each 1.5 times the original volume expansion:

 / ** 
     * ArrayList expansion of the core methods. 
     * / 
    Private  void Grow ( int minCapacity) {
         // oldCapacity old capacity, newCapacity new capacity 
        int oldCapacity = elementData.length;
         // will oldCapacity right one bit, the effect is equivalent oldCapacity / 2,
         // we know bits much faster than computing integer division, sentence expression result is updated to the new capacity of 1.5 times the capacity of the old, 
        int newCapacity = oldCapacity + (oldCapacity >>. 1 );
         // then checks whether the new capacity is greater than the minimum required the new capacity of the capacity, or if less than a minimum capacity is required, then put as a minimum required capacity of the array, 
        IF (newCapacity - be minCapacity <0 ) 
            newCapacity = be minCapacity;
         //Then check whether the new capacity exceeds the maximum capacity of the ArrayList defined
         // if exceeded, then call hugeCapacity () to compare minCapacity and MAX_ARRAY_SIZE,
         // if minCapacity greater than MAX_ARRAY_SIZE, the new capacity was Interger.MAX_VALUE, otherwise, new capacity size was MAX_ARRAY_SIZE. 
        IF (newCapacity - MAX_ARRAY_SIZE> 0 ) 
            newCapacity = hugeCapacity (be minCapacity);
         // be minCapacity IS usually Close to size, the this SO IS A win: 
        elementData of = Arrays.copyOf (elementData of, newCapacity); 
    }

 

Guess you like

Origin www.cnblogs.com/ustc-anmin/p/11531097.html