JDK1.8--ArrayList

一、ArrayList的属性

二、ArrayList的构造方法

三、ArrayList的核心函数

1. 新增元素

2. 删除元素

3. 修改元素

4. 查找元素

5. 其他方法

6. 内部类及调用内部类的方法

ArrayList继承AbstractList抽象类,实现List, RandomAccess, Cloneable, java.io.Serializable接口。

  • List接口:制定了List的操作规范
  • RandomAccess接口:RandomAccess接口的作用测试
  • Cloneable接口:可以克隆对象(浅拷贝)
  • Serializable接口: 对象序列化

1. ArrayList的属性

1.1ArrayList包含两个重要对象:elementData 和 size。

1.1.1 elementData 是transient类型(Transient关键字详解)

它保存了添加到ArrayList中的元素。实际上,elementData是个动态数组,我们能通过构造函数 ArrayList(int initialCapacity)来执行它的初始容量为initialCapacity;

    //构造一个具有指定初始容量的空列表。 参数:initialCapacity - 列表的初始容量
    public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }

如果通过不含参数的构造函数ArrayList()来创建 ArrayList,则elementData的容量默认是10。elementData数组的大小会根据ArrayList容量的增长而动态的增长,具 体的增长方式,请参考源码分析中的ensureCapacity()函数。

1.1.2 size 则是动态数组的实际大小。

实际元素大小,默认为0

1.1.3 还有一个从父类继承的属性 protected transient int modCount

从父类AbstractList继承而来 已从结构上修改 此列表的次数。从结构上修改是指更改列表的大小,或者打乱列表,从而使正在进行的迭代产生错误的结果。此字段由 iterator 和 listIterator 方法返回的迭代器和列表迭代器实现使用。 如果意外更改了此字段中的值,则迭代器(或列表迭代器)将抛出 ConcurrentModificationException 来响应 next、remove、previous、set 或 add 操作。在迭代期间面临并发修改时,它提供了快速失败 行为,而不是非确定性行为。即fail-fast机制

1.2ArrayList包含的静态常量

    // 版本号
    private static final long serialVersionUID = 8683452581122892189L;
    // 缺省容量
    private static final int DEFAULT_CAPACITY = 10;
    // 空对象数组
    private static final Object[] EMPTY_ELEMENTDATA = {};
    // 缺省空对象数组
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
    /**
     * 最大数组容量
     * 其中Integer.MAX_VALUE表示long int的最大值为:0x7fffffff
     */ 
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

2.ArrayList的构造方法

    /**
     * 用指定的数设置初始化容量的构造函数,负数会抛出异常
     */
    public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }

    /**
     * 默认构造器,此处并没有初始化,jdk 1.8之后是在进行add操作后初始化 
     */
    public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

    /**
     * 以集合的迭代器返回顺序,构造一个具有集合中指定元素的ArrayList
     */
    public ArrayList(Collection c) {
        elementData = c.toArray();
        if ((size = elementData.length) != 0) {
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);
        } else {
            // replace with empty array.
            this.elementData = EMPTY_ELEMENTDATA;
        }
    }

3.ArrayList的核心函数

3.1 新增元素

3.1.1 add方法

    /**
     * 在数组末尾加上一个元素
     */
    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }

    /**
     * 在某个指定位置加入一个元素element 
     */
    public void add(int index, E element) {
        // 检查index是否越界 
        rangeCheckForAdd(index);
        // 进行扩容检查  
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        // 对数据进行复制操作,空出index位置,并插入element,后移index后面的元素
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);
        elementData[index] = element;
        size++;
    }
    /**
     * 添加一个集合的元素到末端,若要添加的集合为空返回false
     */
    public boolean addAll(Collection c) {
        // 将集合c转化为Object[]
        Object[] a = c.toArray();
        // 得到c集合的元素的数量
        int numNew = a.length;
        // 数组拓容
        ensureCapacityInternal(size + numNew);
        // 数组复制
        System.arraycopy(a, 0, elementData, size, numNew);
        //更新当前容器大小 ->容量变为size+numNew(添加的集合的元素的数量)
        size += numNew;
        return numNew != 0;
    }

    /**
     * 从指定位置开始添加一个集合
     * 若要添加的集合为空返回false
     */
    public boolean addAll(int index, Collection c) {
        // 判断索引是否越界。
        rangeCheckForAdd(index);
        // 将集合c转化为Object[]
        Object[] a = c.toArray();
        int numNew = a.length;
        // 数组扩容
        ensureCapacityInternal(size + numNew); 
        // 计算需要移动的长度
        int numMoved = size - index;
        if (numMoved > 0)
            // 数组复制,空出第index到index+numNum的位置,即将数组index后的元素向右移动numNum个位置
            System.arraycopy(elementData, index, elementData, index + numNew,
                             numMoved);
        // 将要插入的集合元素复制到数组空出的位置中
        System.arraycopy(a, 0, elementData, index, numNew);
        size += numNew;
        return numNew != 0;
    }

注:在add函数我们发现还有其他的函数ensureCapacityInternal,此函数可以理解为确保elementData数组有合适的大小。

3.1.2 扩容检查 ensureCapacityInternal

ensureCapacityInternal的具体函数如下

/**
 *  扩容检查 
 */
private void ensureCapacityInternal(int minCapacity) {
        // 判断元素数组是否为空数组(第一次add操作初始化,如果为空ArrayList,那么初始化容量为10  )
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            // 取较大值
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }
        // 判断是否需要扩容
        ensureExplicitCapacity(minCapacity);
    }

注:在ensureCapacityInternal函数中我们又发现了ensureExplicitCapacity函数,这个函数也是为了确保elemenData数组有合适的大小。

3.1.3 判断是否需要扩容 ensureExplicitCapacity

ensureExplicitCapacity的具体函数如下

    /**
     * 判断是否需要扩容
     */
    private void ensureExplicitCapacity(int minCapacity) {
        // 父类属性
        modCount++;
        // 扩容
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }

注:在ensureExplicitCapacity函数我们又发现了grow函数,grow函数才会对数组进行扩容,ensureCapacityInternal、ensureExplicitCapacity都只是过程,最后完成实际扩容操作还是得看grow函数。

3.1.4 实际扩容操作 grow具体函数如下

    /**
     * 将整个数组size扩容为1.5倍 
     */
    private void grow(int minCapacity) {
        // 旧容量
        int oldCapacity = elementData.length;
        // 新容量为旧容量的1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);// 新容量小于参数指定容量,修改新容量
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;// 新容量大于最大容量
        if (newCapacity - MAX_ARRAY_SIZE > 0) 
            newCapacity = hugeCapacity(minCapacity); // 指定新容量
        // 拷贝扩容
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

3.1.5 检查是否超过最大容量 hugeCapacity函数

grow函数在扩容时判断是否达到最大容量调用hugeCapacity函数

   /**
     * 检查是否溢出,若没有溢出,返回最大整数值
     * (java中的int为4字节, 所以最大为0x7fffffff)或默认最大值
     */
    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

3.1.6 越界检查方法

add(int index, E element)方法中进行越界检查调用rangeCheckForAdd方法用来检验index是否越界。

    /**
     * 检查index是否越界 否则抛出数组越界异常
     */
    private void rangeCheckForAdd(int index) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

3.1.7 异常抛出详情

rangeCheckForAdd(int index)方法检验index是否越界,否则调用outOfBoundsMsg(int index)方法抛出异常详情

    /**
     * 异常抛出详情
     */
    private String outOfBoundsMsg(int index) {
        return "Index: "+index+", Size: "+size;
    }

3.2 删除元素

3.2.1 单一删除

  • public E remove(int index) 删除指定位置的元素
    /**
     * 移除此列表中指定位置上的元素。向左移动所有后续元素(将其索引减 1)。
     */
    public E remove(int index) {
        // 检查索引是否合法
        rangeCheck(index);
        // 修改次数加1
        modCount++;
        // 取出要删除位置的元素,供返回使用
        E oldValue = elementData(index);
        // 计算数组需要复制的数量
        int numMoved = size - index - 1;
        // 将index后的数据都向前移一位
        if (numMoved > 0)
            // 数组复制,就是将index之后的元素往前移动一个位置
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        // 将数组最后一个元素置空,有利于进行GC
        elementData[--size] = null; 
        // 返回旧值
        return oldValue;
    }
  • public boolean remove(Object o) 删除在此列表中首次出现的指定元素(如果存在)
    /**
     * 移除此列表中首次出现的指定元素(如果存在)。如果列表不包含此元素,则列表不做改动
     */
    public boolean remove(Object o) {
        /**
          * 对要删除的元素进行null判断
          * 对数据元素进行遍历查找,直到找到第一个要删除的元素,删除后进行返回,
          * 如果要删除的元素正好是最后一个 ,时间复杂度可达O(n)
          */
        if (o == null) {  
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }
  • private void fastRemove(int index) 快速删除指定位置元素
    /*
     * 找到对应的元素后 删除。删除元素后的元素都向前移动一位  
     */
    private void fastRemove(int index) {
        modCount++;
        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
    }

3.2.2 批量删除

  • public void clear() 清空ArrayList
    /**
     * 清空ArrayList,将全部的元素设为null
     */
    public void clear() {
        modCount++;

        // clear to let GC do its work
        for (int i = 0; i < size; i++)
            elementData[i] = null;

        size = 0;
    }
  • protected void removeRange(int fromIndex, int toIndex) 删除指定范围的元素
    /**
     * 移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素
     */
    protected void removeRange(int fromIndex, int toIndex) {
        // 修改次数加1
        modCount++;  
        // 复制长度
        int numMoved = size - toIndex;   
        // 数组复制  原理就是移动  根之前的add ,remove一样理解
        System.arraycopy(elementData, toIndex, elementData, fromIndex,
                         numMoved);

        // 新容量
        int newSize = size - (toIndex-fromIndex);
        //移除位置(后面一截的 比如移除了2-3的位置的 ,则4-5的元素就会前移,所以后面的不需要了)的全部赋空 让Gc回收
        for (int i = newSize; i < size; i++) {
            elementData[i] = null;
        }
        size = newSize;
    }
  • 删除ArrayList中包含在指定容器c中的所有元素
    /**
      * 删除ArrayList中包含在指定容器c中的所有元素
      */
    public boolean removeAll(Collection c) {
        Objects.requireNonNull(c);
        return batchRemove(c, false);
    }
  • 移除ArrayList中不包含在指定容器c中的所有元素
    /**
      * 删除ArrayList中包含在指定容器c中的所有元素
      */
    public boolean retainAll(Collection c) {
        Objects.requireNonNull(c);
        return batchRemove(c, true);
    }

注:removeAll(Collection c)和retainAll(Collection c)都调用batchRemove(Collection c, boolean complement) 方法用来删除元素,如何删除则根据complement判断。

  • private boolean batchRemove(Collection c, boolean complement)
    根据complement判断删除c集合还是保留c集合
    /**
     * complement true时从数组保留指定集合中元素的值,为false时从数组删除指定集合中元素的值。
     * 数组中重复的元素都会被删除(而不是仅删除一次或几次),有任何删除操作都会返回true
     */    
    private boolean batchRemove(Collection c, boolean complement) {
        final Object[] elementData = this.elementData;
        int r = 0, w = 0;
        boolean modified = false;
        try {
            //遍历数组,并检查这个集合是否包含对应的值,移动要保留的值到数组前面,w最后值为要保留的元素的数量
            //简单点:若保留,就将相同元素移动到前段;若删除,就将不同元素移动到前段
            for (; r < size; r++)
                if (c.contains(elementData[r]) == complement)
                    elementData[w++] = elementData[r];
        } finally {// 确保异常抛出前的部分可以完成期望的操作,而未被遍历的部分会被接到后面
            // r!=size表示可能出错了:c.contains(elementData[r])抛出异常
            if (r != size) {
                System.arraycopy(elementData, r,
                                 elementData, w,
                                 size - r);
                w += size - r;
            }
            // 如果w==size:表示全部元素都保留了,所以也就没有删除操作发生,所以会返回false;反之,返回true,并更改数组
            // 而w!=size的时候,即使try块抛出异常,也能正确处理异常抛出前的操作,因为w始终为要保留的前段部分的长度,数组也不会因此乱序
            if (w != size) {
                // clear to let GC do its work
                for (int i = w; i < size; i++)
                    elementData[i] = null;
                // 改变的次数
                modCount += size - w;
                // 新的大小为保留的元素的个数
                size = w;
                modified = true;
            }
        }
        return modified;
    }

3.3 修改元素

3.3.1 set方法

    /**
     * 设置指定位置为一个新值,并返回之前的值,会检查这个位置是否超出数组长度
     */
    public E set(int index, E element) {
        // 检验索引是否合法
        rangeCheck(index);
        // 旧值
        E oldValue = elementData(index);
        // 赋新值
        elementData[index] = element;
        // 返回旧值
        return oldValue;
    }

3.4 查找元素

3.4.1 get方法

    /**
     * 返回指定位置的值,但是会检查这个位置数否超出数组长度
     */
    public E get(int index) {
        rangeCheck(index);

        return elementData(index);
    }

注:get函数会检查索引值是否合法(只检查是否大于size,而没有检查是否小于0),值得注意的是,在get函数中存在element函数,element函数用于返回具体的元素。

3.4.2 elementData(index)函数

    /**
      * 返回指定位置的值,因为是数组,所以速度特别快
      */
    @SuppressWarnings("unchecked")
    E elementData(int index) {
        return (E) elementData[index];
    }

3.4.3 contains(Object o)函数

    /**
      * 是否包含一个数 返回boolean 
      */
    public boolean contains(Object o) {
        // 调用indexOf(Object o)函数遍历数组
        return indexOf(o) >= 0;// 存在返回true,否则false
    }

3.4.4 indexOf(Object o)函数

    /**
     * 从首开始查找数组里面是否存在指定元素
     */
    public int indexOf(Object o) {
        if (o == null) {  // 若查找的元素为空
            // 从首部开始遍历数组,找到第一个为空的元素,返回下标
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {  // 查找的元素不为空
            // 从首部开始遍历数组,找到第一个和指定元素相等的元素,返回下标
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;  // 没有找到,返回空
    }

3.4.5 lastIndexOf(Object o)函数

    /**
     * 从尾部开始查找数组里面是否存在指定元素
     */
    public int lastIndexOf(Object o) {
        if (o == null) {  // 查找的元素为空
            // 从尾部开始遍历数组,找到第一个为空的元素,返回下标
            for (int i = size-1; i >= 0; i--)
                if (elementData[i]==null)
                    return i;
        } else {  // 查找的元素不为空
            // 从尾部开始遍历数组,找到第一个为空的元素,返回下标
            for (int i = size-1; i >= 0; i--)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;  // 没有找到,返回空
    }

3.5 其他方法

3.5.1 普通public方法

  • public void trimToSize()
    /**
     * 将底层数组的容量调整为当前实际元素的大小,来释放空间
     */
    public void trimToSize() {
        modCount++;
        //length是数组长度,size表示数组内元素个数;size
  • public int size()
    /**
     * 返回ArrayList的大小(元素个数)
     */
    public int size() {
        return size;
    }
  • public boolean isEmpty()
    /**
     * 判断ArrayList是否为空
     */
    public boolean isEmpty() {
        return size == 0;
    }
  • public Object clone()
    /**
     * 返回此 ArrayList实例的浅拷贝
     */
    public Object clone() {
        try {
            ArrayList v = (ArrayList) super.clone();
            v.elementData = Arrays.copyOf(elementData, size);
            v.modCount = 0;
            return v;
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw new InternalError(e);
        }
    }
  • public Object[] toArray()
    /**
     * 返回一个包含ArrayList中所有元素的数组
     */
    public Object[] toArray() {
        return Arrays.copyOf(elementData, size);
    }
  • public T[] toArray(T[] a)
    /**
     * 如果给定的参数数组长度足够,则将ArrayList中所有元素按序存放于参数数组中,并返回
     * 如果给定的参数数组长度小于ArrayList的长度,则返回一个新分配的、长度等于ArrayList长度的、包含ArrayList中所有元素的新数组
     */
    @SuppressWarnings("unchecked")
    public  T[] toArray(T[] a) {
        if (a.length < size)
            // Make a new array of a's runtime type, but my contents:
            return (T[]) Arrays.copyOf(elementData, size, a.getClass());
        System.arraycopy(elementData, 0, a, 0, size);
        if (a.length > size)  // 数组的长度比列表的元素数量多
            a[size] = null;   // 多余的用null填充
        return a;
    }

3.5.2 Serializable方法

  • private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException
    /**
     * java.io.Serializable的写入函数
     * 将ArrayList的“容量,所有的元素值”都写入到输出流中(即序列化)
     */
    private void writeObject(java.io.ObjectOutputStream s)
        throws java.io.IOException{
        // Write out element count, and any hidden stuff
        int expectedModCount = modCount;
        // 执行默认的反序列化/序列化过程。将当前类的非静态和非瞬态字段写入此流
        s.defaultWriteObject();

        // 写入大小
        s.writeInt(size);

        // 按顺序写入所有元素
        for (int i=0; i
  • private void readObject(java.io.ObjectInputStream s)
    throws java.io.IOException, ClassNotFoundException
    /**
     * java.io.Serializable的读取函数:根据写入方式读出
     * 先将ArrayList的“容量”读出,然后将“所有的元素值”读出(即反序列化)
     */
    private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException {
        elementData = EMPTY_ELEMENTDATA;

        // 执行默认的序列化/反序列化过程
        s.defaultReadObject();

        // 读入数组长度
        s.readInt(); 

        if (size > 0) {
            // be like clone(), allocate array based upon size not capacity
            ensureCapacityInternal(size);

            Object[] a = elementData;
            // 读入所有元素
            for (int i=0; i

3.5.3 其他方法

  • public boolean removeIf(Predicate filter)
    重写Collection接口的方法
    /**
     * 删除满足给定谓词的此集合的所有元素。 在迭代或谓词中抛出的错误或运行时异常被转发给调用者。
     * @param  filter - 一个谓词,为要删除的元素返回 true 
     */
    @Override
    public boolean removeIf(Predicate filter) {
        Objects.requireNonNull(filter);
        // figure out which elements are to be removed
        // any exception thrown from the filter predicate at this stage
        // will leave the collection unmodified
        int removeCount = 0;
        final BitSet removeSet = new BitSet(size);
        final int expectedModCount = modCount;
        final int size = this.size;
        for (int i=0; modCount == expectedModCount && i < size; i++) {
            @SuppressWarnings("unchecked")
            final E element = (E) elementData[i];
            if (filter.test(element)) {
                removeSet.set(i);
                removeCount++;
            }
        }
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }

        // shift surviving elements left over the spaces left by removed elements
        final boolean anyToRemove = removeCount > 0;
        if (anyToRemove) {
            final int newSize = size - removeCount;
            for (int i=0, j=0; (i < size) && (j < newSize); i++, j++) {
                i = removeSet.nextClearBit(i);
                elementData[j] = elementData[i];
            }
            for (int k=newSize; k < size; k++) {
                elementData[k] = null;  // Let gc do its work
            }
            this.size = newSize;
            if (modCount != expectedModCount) {
                throw new ConcurrentModificationException();
            }
            modCount++;
        }

        return anyToRemove;
    }
  • public void replaceAll(UnaryOperator operator)
    重写List接口的方法
    /**
     * 将该列表的每个元素替换为将该运算符应用于该元素的结果。
     *  运营商抛出的错误或运行时异常被转发给呼叫者。
     */
    @Override
    @SuppressWarnings("unchecked")
    public void replaceAll(UnaryOperator operator) {
        Objects.requireNonNull(operator);
        final int expectedModCount = modCount;
        final int size = this.size;
        for (int i=0; modCount == expectedModCount && i < size; i++) {
            elementData[i] = operator.apply((E) elementData[i]);
        }
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
        modCount++;
    }
  • public void forEach(Consumer action)
    重写Iterable接口的方法
    /**
     * 对Iterable的每个元素执行给定的操作,直到所有元素都被处理或动作引发异常。
     *  除非实现类另有规定,否则按照迭代的顺序执行操作(如果指定了迭代顺序)。 动作抛出的异常被转发给呼叫者。 
     */
    @Override
    public void forEach(Consumer action) {
        Objects.requireNonNull(action);
        final int expectedModCount = modCount;
        @SuppressWarnings("unchecked")
        final E[] elementData = (E[]) this.elementData;
        final int size = this.size;
        for (int i=0; modCount == expectedModCount && i < size; i++) {
            action.accept(elementData[i]);
        }
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
    }
  • public void sort(Comparator c)
    重写List接口的方法
    /**
     * 使用提供的 Comparator对此列表进行排序,以比较元素 
     */
    @Override
    @SuppressWarnings("unchecked")
    public void sort(Comparator c) {
        final int expectedModCount = modCount;
        Arrays.sort((E[]) elementData, 0, size, c);
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
        modCount++;
    }

3.6 内部类及调用内部类的方法

3.6.1 Itr private内部类

    /**
     * An optimized version of AbstractList.Itr
     */
    private class Itr implements Iterator {
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

        public boolean hasNext() {
            return cursor != size;
        }

        @SuppressWarnings("unchecked")
        public E next() {
            checkForComodification();
            int i = cursor;
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }

        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

        @Override
        @SuppressWarnings("unchecked")
        public void forEachRemaining(Consumer consumer) {
            Objects.requireNonNull(consumer);
            final int size = ArrayList.this.size;
            int i = cursor;
            if (i >= size) {
                return;
            }
            final Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length) {
                throw new ConcurrentModificationException();
            }
            while (i != size && modCount == expectedModCount) {
                consumer.accept((E) elementData[i++]);
            }
            // update once at end of iteration to reduce heap write traffic
            cursor = i;
            lastRet = i - 1;
            checkForComodification();
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }
  • 调用Itr内部类的方法
    /**
     * 以正确的顺序返回该列表中的元素的迭代器
     */
    public Iterator iterator() {
        return new Itr();
    }

3.6.2 ListItr private内部类

    /**
     * An optimized version of AbstractList.ListItr
     */
    private class ListItr extends Itr implements ListIterator {
        ListItr(int index) {
            super();
            cursor = index;
        }

        public boolean hasPrevious() {
            return cursor != 0;
        }

        public int nextIndex() {
            return cursor;
        }

        public int previousIndex() {
            return cursor - 1;
        }

        @SuppressWarnings("unchecked")
        public E previous() {
            checkForComodification();
            int i = cursor - 1;
            if (i < 0)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i;
            return (E) elementData[lastRet = i];
        }

        public void set(E e) {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                ArrayList.this.set(lastRet, e);
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

        public void add(E e) {
            checkForComodification();

            try {
                int i = cursor;
                ArrayList.this.add(i, e);
                cursor = i + 1;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }
    }
  • 调用ListItr 内部类的方法
    /**
     * 从列表中的指定位置开始,返回列表中的元素(按正确顺序)的列表迭代器。 
     */
    public ListIterator listIterator(int index) {
        if (index < 0 || index > size)
            throw new IndexOutOfBoundsException("Index: "+index);
        return new ListItr(index);
    }

    /**
     * 返回列表中的列表迭代器(按适当的顺序)
     */
    public ListIterator listIterator() {
        return new ListItr(0);
    }

3.6.3 SubList private内部类

    private class SubList extends AbstractList implements RandomAccess {
        private final AbstractList parent;
        private final int parentOffset;
        private final int offset;
        int size;

        SubList(AbstractList parent,
                int offset, int fromIndex, int toIndex) {
            this.parent = parent;
            this.parentOffset = fromIndex;
            this.offset = offset + fromIndex;
            this.size = toIndex - fromIndex;
            this.modCount = ArrayList.this.modCount;
        }

        public E set(int index, E e) {
            rangeCheck(index);
            checkForComodification();
            E oldValue = ArrayList.this.elementData(offset + index);
            ArrayList.this.elementData[offset + index] = e;
            return oldValue;
        }

        public E get(int index) {
            rangeCheck(index);
            checkForComodification();
            return ArrayList.this.elementData(offset + index);
        }

        public int size() {
            checkForComodification();
            return this.size;
        }

        public void add(int index, E e) {
            rangeCheckForAdd(index);
            checkForComodification();
            parent.add(parentOffset + index, e);
            this.modCount = parent.modCount;
            this.size++;
        }

        public E remove(int index) {
            rangeCheck(index);
            checkForComodification();
            E result = parent.remove(parentOffset + index);
            this.modCount = parent.modCount;
            this.size--;
            return result;
        }

        protected void removeRange(int fromIndex, int toIndex) {
            checkForComodification();
            parent.removeRange(parentOffset + fromIndex,
                               parentOffset + toIndex);
            this.modCount = parent.modCount;
            this.size -= toIndex - fromIndex;
        }

        public boolean addAll(Collection c) {
            return addAll(this.size, c);
        }

        public boolean addAll(int index, Collection c) {
            rangeCheckForAdd(index);
            int cSize = c.size();
            if (cSize==0)
                return false;

            checkForComodification();
            parent.addAll(parentOffset + index, c);
            this.modCount = parent.modCount;
            this.size += cSize;
            return true;
        }

        public Iterator iterator() {
            return listIterator();
        }

        public ListIterator listIterator(final int index) {
            checkForComodification();
            rangeCheckForAdd(index);
            final int offset = this.offset;

            return new ListIterator() {
                int cursor = index;
                int lastRet = -1;
                int expectedModCount = ArrayList.this.modCount;

                public boolean hasNext() {
                    return cursor != SubList.this.size;
                }

                @SuppressWarnings("unchecked")
                public E next() {
                    checkForComodification();
                    int i = cursor;
                    if (i >= SubList.this.size)
                        throw new NoSuchElementException();
                    Object[] elementData = ArrayList.this.elementData;
                    if (offset + i >= elementData.length)
                        throw new ConcurrentModificationException();
                    cursor = i + 1;
                    return (E) elementData[offset + (lastRet = i)];
                }

                public boolean hasPrevious() {
                    return cursor != 0;
                }

                @SuppressWarnings("unchecked")
                public E previous() {
                    checkForComodification();
                    int i = cursor - 1;
                    if (i < 0)
                        throw new NoSuchElementException();
                    Object[] elementData = ArrayList.this.elementData;
                    if (offset + i >= elementData.length)
                        throw new ConcurrentModificationException();
                    cursor = i;
                    return (E) elementData[offset + (lastRet = i)];
                }

                @SuppressWarnings("unchecked")
                public void forEachRemaining(Consumer consumer) {
                    Objects.requireNonNull(consumer);
                    final int size = SubList.this.size;
                    int i = cursor;
                    if (i >= size) {
                        return;
                    }
                    final Object[] elementData = ArrayList.this.elementData;
                    if (offset + i >= elementData.length) {
                        throw new ConcurrentModificationException();
                    }
                    while (i != size && modCount == expectedModCount) {
                        consumer.accept((E) elementData[offset + (i++)]);
                    }
                    // update once at end of iteration to reduce heap write traffic
                    lastRet = cursor = i;
                    checkForComodification();
                }

                public int nextIndex() {
                    return cursor;
                }

                public int previousIndex() {
                    return cursor - 1;
                }

                public void remove() {
                    if (lastRet < 0)
                        throw new IllegalStateException();
                    checkForComodification();

                    try {
                        SubList.this.remove(lastRet);
                        cursor = lastRet;
                        lastRet = -1;
                        expectedModCount = ArrayList.this.modCount;
                    } catch (IndexOutOfBoundsException ex) {
                        throw new ConcurrentModificationException();
                    }
                }

                public void set(E e) {
                    if (lastRet < 0)
                        throw new IllegalStateException();
                    checkForComodification();

                    try {
                        ArrayList.this.set(offset + lastRet, e);
                    } catch (IndexOutOfBoundsException ex) {
                        throw new ConcurrentModificationException();
                    }
                }

                public void add(E e) {
                    checkForComodification();

                    try {
                        int i = cursor;
                        SubList.this.add(i, e);
                        cursor = i + 1;
                        lastRet = -1;
                        expectedModCount = ArrayList.this.modCount;
                    } catch (IndexOutOfBoundsException ex) {
                        throw new ConcurrentModificationException();
                    }
                }

                final void checkForComodification() {
                    if (expectedModCount != ArrayList.this.modCount)
                        throw new ConcurrentModificationException();
                }
            };
        }

        public List subList(int fromIndex, int toIndex) {
            subListRangeCheck(fromIndex, toIndex, size);
            return new SubList(this, offset, fromIndex, toIndex);
        }

        private void rangeCheck(int index) {
            if (index < 0 || index >= this.size)
                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        }

        private void rangeCheckForAdd(int index) {
            if (index < 0 || index > this.size)
                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        }

        private String outOfBoundsMsg(int index) {
            return "Index: "+index+", Size: "+this.size;
        }

        private void checkForComodification() {
            if (ArrayList.this.modCount != this.modCount)
                throw new ConcurrentModificationException();
        }

        public Spliterator spliterator() {
            checkForComodification();
            return new ArrayListSpliterator(ArrayList.this, offset,
                                               offset + this.size, this.modCount);
        }
    }
  • 调用SubList内部类的方法
    /**
     * 返回此列表中指定的 fromIndex (包括)和 toIndex之间的独占视图
     */
    public List subList(int fromIndex, int toIndex) {
        subListRangeCheck(fromIndex, toIndex, size);
        return new SubList(this, 0, fromIndex, toIndex);
    }

注:调用SubList内部类之前进行越界检查

    /**
     * 越界检查
     */
    static void subListRangeCheck(int fromIndex, int toIndex, int size) {
        if (fromIndex < 0)
            throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
        if (toIndex > size)
            throw new IndexOutOfBoundsException("toIndex = " + toIndex);
        if (fromIndex > toIndex)
            throw new IllegalArgumentException("fromIndex(" + fromIndex +
                                               ") > toIndex(" + toIndex + ")");
    }

3.6.4 ArrayListSpliterator静态内部类

    /** Index-based split-by-two, lazily initialized Spliterator */
    static final class ArrayListSpliterator implements Spliterator {

        private final ArrayList list;
        private int index; // current index, modified on advance/split
        private int fence; // -1 until used; then one past last index
        private int expectedModCount; // initialized when fence set

        /** Create new spliterator covering the given  range */
        ArrayListSpliterator(ArrayList list, int origin, int fence,
                             int expectedModCount) {
            this.list = list; // OK if null unless traversed
            this.index = origin;
            this.fence = fence;
            this.expectedModCount = expectedModCount;
        }

        private int getFence() { // initialize fence to size on first use
            int hi; // (a specialized variant appears in method forEach)
            ArrayList lst;
            if ((hi = fence) < 0) {
                if ((lst = list) == null)
                    hi = fence = 0;
                else {
                    expectedModCount = lst.modCount;
                    hi = fence = lst.size;
                }
            }
            return hi;
        }

        public ArrayListSpliterator trySplit() {
            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
            return (lo >= mid) ? null : // divide range in half unless too small
                new ArrayListSpliterator(list, lo, index = mid,
                                            expectedModCount);
        }

        public boolean tryAdvance(Consumer action) {
            if (action == null)
                throw new NullPointerException();
            int hi = getFence(), i = index;
            if (i < hi) {
                index = i + 1;
                @SuppressWarnings("unchecked") E e = (E)list.elementData[i];
                action.accept(e);
                if (list.modCount != expectedModCount)
                    throw new ConcurrentModificationException();
                return true;
            }
            return false;
        }

        public void forEachRemaining(Consumer action) {
            int i, hi, mc; // hoist accesses and checks from loop
            ArrayList lst; Object[] a;
            if (action == null)
                throw new NullPointerException();
            if ((lst = list) != null && (a = lst.elementData) != null) {
                if ((hi = fence) < 0) {
                    mc = lst.modCount;
                    hi = lst.size;
                }
                else
                    mc = expectedModCount;
                if ((i = index) >= 0 && (index = hi) <= a.length) {
                    for (; i < hi; ++i) {
                        @SuppressWarnings("unchecked") E e = (E) a[i];
                        action.accept(e);
                    }
                    if (lst.modCount == mc)
                        return;
                }
            }
            throw new ConcurrentModificationException();
        }

        public long estimateSize() {
            return (long) (getFence() - index);
        }

        public int characteristics() {
            return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
        }
    }
  • 调用ArrayListSpliterator静态内部类的方法
    /**
     * 在此列表中的元素上创建late-binding和故障快速 Spliterator
     */
    @Override
    public Spliterator spliterator() {
        return new ArrayListSpliterator<>(this, 0, -1, 0);
    }

小结

通过ArrayList源码中可以看到其插入和删除操作都是调用System.arraycopy()这个效率低下的操作来赋值数组,所以导致ArrayList在插入和删除操作中效率不高。

你可能感兴趣的:(JDK1.8--ArrayList)