ArrayList源码分析

源码来自jdk1.8


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

public class ArrayList<E> extends AbstractList<E>        
  implements List<E>, 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是在Java中最常用的集合之一,其本质上可以当做是一个可扩容的数组,可以添加重复的数据,也支持随...
    ShawnIsACoder阅读 583评论 4 7
  • ArrayList可以说是在Java开发中最常用的集合容器了,今天就来分析一下ArrayList的源码,可以更加深...
    xiaoyanger阅读 498评论 1 3
  • ArrayList 认识 ArrayList是最常见以及每个Java开发者最熟悉的集合类了 elementData...
    zlb阅读 161评论 0 0
  • 概述 在分析ArrayList源码之前,先看一下ArrayList在数据结构中的位置,常见的数据结构按照逻辑结构跟...
    wustor阅读 574评论 4 2
  • hi,大家好,欢迎关注我的微信公众号“码农范er”,集分享,交友,聊天为一体,总有你喜欢的、快点关注我吧~ htm...
    范小饭_阅读 1,068评论 6 10