概述
ArrayList是一个以动态数组为基础实现的非线程安全的集合,ArrayList的元素可以为空、可以重复,同时又是有序的(读取和存放的顺序一致 )。
ArrayList继承AbstractList,实现了List、RandomAccess(可以快速访问)、Cloneable(可以被克隆)、java.io.Serializable(支持序列化),下面是源码片段:
初始化
ArrayList的初始化方式有三种:
- 1、无参构造,默认长度为10,是我们使用的最多的一种初始化方式:
/** * Constructs an empty list with an initial capacity of ten. */ public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; }
这个时候,我们从源码中可以看到,里面只有一行代码:this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA,那么定义的DEFAULTCAPACITY_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 = {};
通过注释可以得知,源码中定义了一个空的数组作为默认的大小,并且在第一个元素添加进来的时候再确定把数组扩充多少,这段逻辑会在接下来添加元素部分作出解释。
- 2、指定初始化长度:
/** * Constructs an empty list with the specified initial capacity. * @param initialCapacity the initial capacity of the list * @throws IllegalArgumentException if the specified initial capacity * is negative */ 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); } }
- 3、用一个Collection对象来构造
/** * Constructs a list containing the elements of the specified * collection, in the order they are returned by the collection's * iterator. * * @param c the collection whose elements are to be placed into this list * @throws NullPointerException if the specified collection is null */ public ArrayList(Collection<? extends E> 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; } }
添加元素
List提供了add方法来添加元素,我们进源码看一下这部分代码:
/** * Appends the specified element to the end of this list. * * @param e element to be appended to this list * @return <tt>true</tt> (as specified by {@link Collection#add}) */ public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }
在elementData[size++] = e之前,有一个ensureCapacityInternal(size + 1),这个是扩容方法,在下一块会涉及到,所以此处暂不去管它,添加元素就是在elementData的某个位置处添加了一个元素的引用。
扩容
在上面的代码中我们提到了一个方法ensureCapacityInternal(),传入的参数是size+1(size是ArrayList所拥有的元素的个数),现在我们再来具体看这个方法:
private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); }
如果存储的数组是一个空数组,那么最小容量就在DEFAULT_CAPACITY(默认容量,值为10)和size+1之间去一个最大值,现在我们回过头来想,如果这是第一次添加元素,那么minCapacity为此时就会被赋值为10,也就是,无参构造时,ArrayList的长度为10了。
接下来执行ensureExplicitCapacity(minCapacity),我们继续跟进源码:
private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); }
modCount是为了统计集合被修改的次数的,如果是安全失败的迭代器发现这块有问题会抛出异常,在此不是重点,所以不详细介绍。
如果上面步骤得出的minCapacity(需要的最小容量) 比数组的容量大的话,就需要执行扩容,grow方法如下:
/** * Increases the capacity to ensure that it can hold at least the * number of elements specified by the minimum capacity argument. * * @param minCapacity the desired minimum capacity */ 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); }
定义oldCapacity为数组原来容量,newCapacity 为扩容后数组新的容量,
新容量为oldCapacity + (oldCapacity >> 1),通过移位运算,为1.5倍的oldCapacity ,也就是扩容到原来的1.5倍,1.5倍是开发人员在时间和空间上权衡取的一个值。如果计算后,新的容量还是小于所需要的最小容量的话,那么就把最小容量设置为新的容量。
下面的比较是当容量特别大时(极少出现的情况)所做的处理。
最后把数组中元素从原来的数组中的元素拷贝到新的数组中,方法如下:
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { @SuppressWarnings("unchecked") T[] copy = ((Object)newType == (Object)Object[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }
数组中元素顺序不变,新数组中没有元素的位置为空。
删除元素
- 1、通过下角标删除元素:
/** * Removes the element at the specified position in this list. * Shifts any subsequent elements to the left (subtracts one from their * indices). * * @param index the index of the element to be removed * @return the element that was removed from the list * @throws IndexOutOfBoundsException {@inheritDoc} */ 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; }
首先执行删除检查,如果要删除的元素位置大于数组长度,就会报下角标越界异常。
删除的过程,就是从指定元素后面的所有元素,通过System.arraycopy()整体向前移动,最后把尾部的元素置为null,等待gc的回收。
所以说ArrayList的删除性能是不稳定的,因为它需要把元素位置后面的所有元素向前拷贝,要拷贝的元素越多,性能就越差。
2、通过对象删除,这个过程原理和通过下角标删除时一样的,在此不做赘述。
通过下角标插入
单纯的去尾部添加元素,性能其实并不差,但是向指定位置添加元素那么性能就不稳定了:
/** *Inserts the specified element at the specified position in this * list. Shifts the element currently at that position (if any) and * any subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted * @param element element to be inserted * @throws IndexOutOfBoundsException {@inheritDoc} */ 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++; }
首先执行,添加检查,如果添加的位置比数组长度大,那么就会报下角标越界异常。
最重要的点是:在指定位置添加,需要把该位置后面所有的元素整体都向后挪一个位置,也是通过System.arraycopy来实现,执行完毕,再把该位置的元素设置为要添加的元素,这时,性能就不稳定了吧,要挪动的元素越多,性能越差。
元素查找
元素查找很简单:
/** * Returns the element at the specified position in this list. * * @param index index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E get(int index) { rangeCheck(index); return elementData(index); }
不涉及复杂的操作,直接返回该位置的元素,性能很好。
总结:
本文从源码出发,详细的分析了ArrayList的初始化、添加、扩容、删除的过程,通过此过程我们可以很清晰地了解到:
1、ArrayList的元素查找是非常快的,因为它是通过数组实现,可以通过下角标直接找到元素,并且实现了RandomAccess接口,加快了访问速度;
2、单纯添加一个元素,过程很简单,先判断是否涉及扩容,然后直接想尾部添加一个元素;
3、删除一个元素,涉及到该位置后面所有元素的整体拷贝,性能不稳定,也可以理解为很差;