ArrayList源码分析

源码来自jdk1.8


ArrayList是对List接口的数组实现,所以可以随机访问数组元素(RandomAccess)。ArrayList最重要的是它的底层实现了一个Resizable array,也就是可以在操作的过程中数组可以根据需要扩大。

public class ArrayList extends AbstractList        
  implements List, RandomAccess, Cloneable, java.io.Serializable{    
    private static final long serialVersionUID = 8683452581122892189L;    
    /**     
    * Default initial capacity.     
    */    
    private static final int DEFAULT_CAPACITY = 10;    
    /**     
    * Shared empty array instance used for empty instances.         
    */    
    private static final Object[] EMPTY_ELEMENTDATA = {};    
    /**     
    * Shared empty array instance used for default sized empty instances. We    
    * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when     
    * first element is added.    
    */    
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};    
    /**     
    * The array buffer into which the elements of the ArrayList are stored.     
    * The capacity of the ArrayList is the length of this array buffer. Any     
    * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA     
    * will be expanded to DEFAULT_CAPACITY when the first element is added.     
    */    
    transient Object[] elementData; // non-private to simplify nested class access    
    /**     
    * The size of the ArrayList (the number of elements it contains).     
    * @serial     
    */    
    private int size;
    // 这里省略了方法
    }

自动扩容

private void grow(int minCapacity) {    
    // overflow-conscious code    
    int oldCapacity = elementData.length;    
    int newCapacity = oldCapacity + (oldCapacity >> 1);    
    if (newCapacity - minCapacity < 0)        
        newCapacity = minCapacity;    
    if (newCapacity - MAX_ARRAY_SIZE > 0)        
        newCapacity = hugeCapacity(minCapacity);    
    // minCapacity is usually close to size, so this is a win:      
    elementData = Arrays.copyOf(elementData, newCapacity);
}

grow函数是自动扩容的核心,每次尝试将数组扩大50%,如果不够,就直接为指定大小,如果非常的大,就修正为最大容量,最后使用Arrays.copyOf来产生新的数组。注意调用grow的ensureExplicitCapcity函数中有modCount++,也就是说,只要调用了ensure这个函数(四个add方法),无论你最终有没有调用grow生成一个新的数组,都会增加一次修改次数。

add

没有index参数的add方法将新元素添加到数组末位,有index的会先将index之后的所有元素往后移动一个位置。所以是O(n)

 public void add(int index, E element) {
        rangeCheckForAdd(index);

        ensureCapacityInternal(size + 1);  // Increments modCount!!
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);
        elementData[index] = element;
        size++;
    }

remove

remove会增加修改次数,移动元素,将多余的元素置为null,这样GC会清理这个多余的元素。

 public E remove(int index) {
        rangeCheck(index);

        modCount++;
        E oldValue = elementData(index);

        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work

        return oldValue;
    }

你可能感兴趣的:(ArrayList源码分析)