Detailed explanation of dynamic expansion of Arraylist

ArrayList overview

ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长。
ArrayList不是线程安全的,只能用在单线程环境下。
实现了Serializable接口,因此它支持序列化,能够通过序列化传输;
实现了RandomAccess接口,支持快速随机访问,实际上就是通过下标序号进行快速访问;
实现了Cloneable接口,能被克隆。

Dynamic expansion

an initialization

First, there are three ways to initialize:

public ArrayList();

The default constructor, which will initialize the internal array with the default size

public ArrayList(Collection<? extends E> c)

Construct with an ICollection object and add the elements of the collection to the ArrayList

public ArrayList(int initialCapacity) 

Initialize the internal array with the specified size

The latter two methods can be understood, by creating an object or specifying a size to initialize the internal data. 
Then let's focus on the implementation of the parameterless constructor:

copy code
/**
     * Constructs an empty list with an initial capacity of ten.
     */
    public ArrayList() {
      // DEFAULTCAPACITY_EMPTY_ELEMENTDATA是空数组
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
copy code

可以看出它的默认数组为长度为0。而在之前JDK1,6中,无参数构造器代码是初始长度为10。 
JDK6代码这样的:

copy code
 public ArrayList() {
    this(10);
    }
  public ArrayList(int initialCapacity) {
    super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
    this.elementData = new Object[initialCapacity];
    }
copy code

接下来,要扩容的话,肯定是在ArrayList.add 方法中。我们来看一下具体实现。

二  确保内部容量

我们以无参数构造为例, 
初始化时,数组长度为0. 
那我现在要添加数据了,数组的长度是怎么变化的?

 public boolean add(E e) {
        //确保内部容量(通过判断,如果够则不进行操作;容量不够就扩容来确保内部容量)
        ensureCapacityInternal(size + 1);  // ①Increments modCount!!
        elementData[size++] = e;//
        return true;
    }

① ensureCapacityInternal方法名的英文大致是“确保内部容量”,size表示的是执行添加之前的元素个数,并非ArrayList的容量,容量应该是数组elementData的长度。ensureCapacityInternal该方法通过将现有的元素个数数组的容量比较。看如果需要扩容,则扩容。 
②是将要添加的元素放置到相应的数组中。 
下面具体看 ensureCapacityInternal(size + 1);

copy code
  // ① 是如何判断和扩容的。
private void ensureCapacityInternal(int minCapacity) {
      //如果实际存储数组 是空数组,则最小需要容量就是默认容量
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }

        ensureExplicitCapacity(minCapacity);
    }

    private void ensureExplicitCapacity(int minCapacity) {
        modCount++;
        //如果数组(elementData)的长度小于最小需要的容量(minCapacity)就扩容
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
    /**
     * Default initial capacity.
     */
    private static final int DEFAULT_CAPACITY = 10;
copy code

以上,elementData是用来存储实际内容的数组。minExpand 是最小扩充容量。 
DEFAULTCAPACITY_EMPTY_ELEMENTDATA共享的空数组实例用于默认大小的空实例。根据传入的最小需要容量minCapacity来和数组的容量长度对比,若minCapactity大于或等于数组容量,则需要进行扩容。

三 扩容

 

copy code
  /*
    *增加容量,以确保它至少能容纳
    *由最小容量参数指定的元素数。
    * @param mincapacity所需的最小容量
    */
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        //>>位运算,右移动一位。 整体相当于newCapacity =oldCapacity + 0.5 * oldCapacity  
        // jdk1.7采用位运算比以前的计算方式更快
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
       //jdk1.7这里增加了对元素个数的最大个数判断,jdk1.7以前是没有最大值判断的,MAX_ARRAY_SIZE 为int最大值减去8(不清楚为什么用这个值做比较)
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // 最重要的复制元素方法
        elementData = Arrays.copyOf(elementData, newCapacity);
    }
copy code

 

  综上所述ArrayList相当于在没指定initialCapacity时就是会使用延迟分配对象数组空间,当第一次插入元素时才分配10(默认)个对象空间。假如有20个数据需要添加,那么会分别在第一次的时候,将ArrayList的容量变为10 (如下图一);之后扩容会按照1.5倍增长。也就是当添加第11个数据的时候,Arraylist继续扩容变为10*1.5=15(如下图二);当添加第16个数据时,继续扩容变为15 * 1.5 =22个(如下图四)。:

  向数组中添加第一个元素时,数组容量为10.

  向数组中添加到第10个元素时,数组容量仍为10. 

  向数组中添加到第11个元素时,数组容量扩为15. 

  向数组中添加到第16个元素时,数组容量扩为22.

每次扩容都是通过Arrays.copyOf(elementData, newCapacity) 这样的方式实现的。

  对比和总结:

  This article introduces the whole process of dynamic expansion of ArrayList . If it is constructed with no parameters, the initial array capacity is 0 , and when the array is actually added, the capacity is actually allocated. Each time it is expanded by a ratio of 1.5 times (bit operation) through coeOf. The implementation in JKD1.6 is that if it is constructed with no parameters, the initial array capacity is 10, and the capacity is 1.5 times the original capacity after each expansion through the copeOf method . The above is the principle of dynamic expansion. 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325643560&siteId=291194637