1、ArrayList 简介
1.1、ArrayList简介
1)ArrayList是可以动态增长和缩减的索引序列,它是基于数组实现的List类。
2)该类封装了一个动态再分配的Object[]数组,每一个类对象都有一个capacity属性,表示它们所封装的Object[]数组的长度,当向ArrayList中添加元素时,该属性值会自动增加。
如果想ArrayList中添加大量元素,可使用ensureCapacity方法一次性增加capacity,可以减少增加重分配的次数提高性能。
3)ArrayList的用法和Vector向类似,但是Vector是一个较老的集合,具有很多缺点,不建议使用。
另外,ArrayList和Vector的区别是:ArrayList是线程不安全的,当多条线程访问同一个ArrayList集合时,程序需要手动保证该集合的同步性,而Vector则是线程安全的。
4)ArrayList和Collection的关系:
1.2、ArrayList的数据结构
分析一个类的时候,数据结构往往是它的灵魂所在,理解底层的数据结构其实就理解了该类的实现思路,具体的实现细节再具体分析。
ArrayList的数据结构是:
容量:CAPACITY ; 实际大小:size;
说明:底层的数据结构就是数组,数组元素类型为Object类型,即可以存放所有类型数据。我们对ArrayList类的实例的所有的操作底层都是基于数组的。
2、ArrayList源码分析
2.1、继承结构和层次关系
分析:
1)为什么要先继承AbstractList,而让AbstractList先实现List<E>?而不是让ArrayList直接实现List<E>?
这里是有一个思想,接口中全都是抽象的方法,而抽象类中可以有抽象方法,还可以有具体的实现方法,正是利用了这一点,让AbstractList是实现接口中一些通用的方法,而具体的类,
如ArrayList就继承这个AbstractList类,拿到一些通用的方法,然后自己在实现一些自己特有的方法,这样一来,让代码更简洁,就继承结构最底层的类中通用的方法都抽取出来,
先一起实现了,减少重复代码。所以一般看到一个类上面还有一个抽象类,应该就是这个作用。
2)ArrayList实现了哪些接口?
List<E>接口:我们会出现这样一个疑问,在查看了ArrayList的父类AbstractList也实现了List<E>接口,那为什么子类ArrayList还是去实现一遍呢?
在stackOverFlow中找到了答案 http://stackoverflow.com/questions/2165204/why-does-linkedhashsete-extend-hashsete-and-implement-sete开发这个collection 的作者Josh说这其实是一个mistake,因为他写这代码的时候觉得这个会有用处,但是其实并没什么用,但因为没什么影响,就一直留到了现在。
RandomAccess接口:这个是一个标记性接口,通过查看api文档,它的作用就是用来快速随机存取,有关效率的问题,在实现了该接口的话,那么使用普通的for循环来遍历,性能更高,例如ArrayList。
public interface RandomAccess {
}
而没有实现该接口的话,使用Iterator来迭代性能更高,例如linkedList。所以这个标记性只是为了让我们知道我们用什么样的方式去获取数据性能更好。
Cloneable接口:实现了该接口,就可以使用Object.Clone()方法了。
Serializable接口:实现该序列化接口,表明该类可以被序列化,什么是序列化?简单的说,就是能够从类变成字节流传输,然后还能从字节流变成原来的类。
2.2、类中的属性
/**
* 默认初始容量
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* 空对象数组
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
/**
* 默认空对象数组, 与EMPTY_ELEMENTDATA 区分开来是为了了解何时添加第一个元素
* 当调用无参构造方法,返回的是该数组。刚创建一个ArrayList 时,其内数据量为0。
* 它与EMPTY_ELEMENTDATA的区别就是:该数组是默认返回的,而EMPTY_ELEMENTDATA是在用户指定容量为0时返回。
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
*元素数组
*/
transient Object[] elementData; // non-private to simplify nested class access
/**
*实际元素大小,默认为0
*/
private int size;
/**
*最大数组容量
为什么要减去8呢?
因为某些VM会在数组中保留一些头字,尝试分配这个最大存储容量,可能会导致array容量大于VM的limit,最终导致OutOfMemoryError。
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
//备注:MAX.VALUE为0x7fffffff,转换成十进制就是2147483647,也就是数组的最大长度是2147483639;
2.3、构造方法
ArrayList有三个构造方法:
public ArrayList()
public ArrayList(int initialCapacity)
public ArrayList(Collection<? extends E> c)
2.3.1、无参构造方法
/**
* Constructs an empty list with an initial capacity of ten.
构造一个空的初始容量为10的List
*/
public ArrayList() {
//elementData 是Object[]类型,DEFAULTCAPACITY_EMPTY_ELEMENTDATA是个空的Object[]
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
2.3.2、有参构造函数一
/**
* Constructs an empty list with the specified initial capacity.
构造一个空的有初始容量值的list
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
//大于0将自定义的容量大小当成初始化elementData的大小
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
//等于0使用EMPTY_ELEMENTDATA的空数组构建
this.elementData = EMPTY_ELEMENTDATA;
} else {
//判断如果自定义大小的容量小于0,则报下面这个非法数据异常
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
2.3.3、有参构造方法三(不常用)
/**
//这个构造方法不常用,举个例子就能明白什么意思
Class Strudent exends Person
ArrayList<Person>, Person这里就是泛型
我还有一个Collection<Student>,由于这个Student继承了Person,那么根据这个构造方法,我就可以把这个Collection<Student>转换为ArrayList<Sudent>这就是这个构造方法的作用
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray(); //转换为数组
if ((size = elementData.length) != 0) { //size数组中的数据个数
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)//每个集合的toarray()的实现方法不一样,所以需要判断一下,如果不是Object[].class类型,那么久需要使用ArrayList中的方法去改造一下。
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
总结:arrayList的构造方法就做一件事情,就是初始化一下储存数据的容器,其实本质上就是一个数组,在其中就叫elementData。
2.4、核心方法
2.4.1、add() 方法(有四个)
public boolean add(E e)
public void add(int index, E element)
public boolean addAll(Collection<? extends E> c)
public boolean addAll(int index, Collection<? extends E> c)
2.4.1.1、public boolean add(E e) //默认直接在末尾添加元素
//添加一个特定的元素到list的末尾。
public boolean add(E e) {
//确定内部容量是否够了,size是数组中数据的个数,因为要添加一个元素,所以size+1,先判断size+1的这个个数数组能否放得下,就在这个方法中去判断是否数组.length是否够用了。
ensureCapacityInternal(size + 1); // Increments modCount!!
//在数据中正确的位置上放上元素e,并且size++
elementData[size++] = e;
return true;
}
接着我们来分析 private void ensureCapacityInternal(int minCapacity) 的逻辑
//数组容量检查,不够时则进行扩容,用private修饰只供类内部使用
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
//计算容量
private static int calculateCapacity(Object[] elementData, int minCapacity) {
// 若elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA,则取minCapacity为DEFAULT_CAPACITY和参数minCapacity之间的最大值。
//DEFAULT_CAPACITY在此之前已经定义为默认的初始化容量是10。
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
//否则返回minCapacity
return minCapacity;
}
//数组容量检查,不够时则进行扩容
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
//最小容量>数组缓冲区当前长度
if (minCapacity - elementData.length > 0)
grow(minCapacity);//扩容
}
这个add 方法是在list的末尾添加一个元素。
size是数组中数据的个数,因为要添加一个元素,所以size+1,先判断size+1的这个个数数组能否放得下。calculateCapacity方法用来计算容量。判断初始化的elementData是不是空的数组,如果是空数组长度size就是0,否则数组长度就不是0。
- 如果elementData元素是空的,就是第一次添加元素,minCapacity=size+1,其实就是等于1,空的数组没有长度就存放不了,所以就将minCapacity变成10,也就是默认容量的大小。也就是elementData数组的此时需要的最小容量为变10了,此时ensureExplicitCapacity()方法中的minCapacity就是10。到此只是说elementData数组需要的容量至少是10,还没有改变elementData的大小。
- 如果elementData数组中的元素不是空的,那么它此时需要的最小容量就是原先的数组长度加1,minCapacity代表着elementData中元素增加之后的实际数据个数。
- 要始终记住minCapacity = size+1,在ensureExplicitCapacity()方法中,首先操作数自增1,再把需要的最小空间容量与数组当前实际长度进行比较:
- 如果elementData中的元素是空的,它现在需要的容量是10,但是elementData.length为0,所以要扩容。
- 如果elementData数组中的元素不是空的,若它添加一个元素后需要的容量比原数组长度大,就需要扩容,否则就不需要扩容。
我们接着往下看,ArrayList能自动扩展大小的关键方法就在下面的grow()方法里:
/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
扩容,保证ArrayList至少能存储minCapacity个元素
第一次扩容,逻辑为newCapacity = oldCapacity + (oldCapacity >> 1);即在原有的容量基础上增加一半。
第一次扩容后,如果容量还是小于minCapacity,就将容量扩充为minCapacity
* @param minCapacity the desired minimum capacity
*/
private void grow(int minCapacity) {
// overflow-conscious code
// 获取当前数组的容量
int oldCapacity = elementData.length;
// 扩容。新的容量=当前容量+当前容量/2.即将当前容量增加一半(当前容量增加1.5倍
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
//如果扩容后的容量还是小于想要的最小容量
//将扩容后的容量再次扩容为想要的最小容量
//当elementData是空数组的时候,length=0,那么oldCapacity=0,newCapacity=0,在这里就是真正的初始化elementData的大小了,就是为minCapacity=10.
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
//如果扩容后的容量大于临界值,则进行大容量分配
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
//新的容量大小已经确定好了,就copy数组,改变容量大小。
//copyof(原数组,新的数组长度)
elementData = Arrays.copyOf(elementData, newCapacity);
}
//进行大容量分配
private static int hugeCapacity(int minCapacity) {
//如果minCapacity<0,抛出异常
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
//如果想要的容量大于MAX_ARRAY_SIZE,则分配Integer.MAX_VALUE,否则分配MAX_ARRAY_SIZE
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
综合以上在list末尾追加数据的方法,如果原数组是空的,添加1个元素时数组容量变为10,如果原数组不为空,追加元素时容量变为原来的1.5倍。如果扩容后的容量大于分配给ArrayList的容量,判断需要的容量是否比分派的容量大,是就把Integer.MAX_VALUE:2147483647赋值给minCapacity,否就用MAX_ARRAY_SIZE:2147483639。
我们调用add方法时,函数调用过程如下:
程序调用add,如果需要扩容就可能会调用到grow,grow可能会调用hugeCapacity。
例1(扩容的情况):
List<Integer> list = new ArrayList<Integer>();
list.add(1);
我们可以看到,在add方法之前开始elementData = {};调用add方法时会继续调用,直至grow,最后elementData的大小变为10,之后再返回到add函数,把8放在elementData[0]中。
例2(不扩容的情况):
List<Integer> list = new ArrayList<Integer>(6);
list.add(2);
在调用add方法之前,elementData的大小已经为6,之后再进行传递,不会进行扩容处理。
2.4.1.2、public void add(int index, E element)
/**
* 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} 如果索引超出的size抛出的异常
*/
public void add(int index, E element) {
//越界检查
rangeCheckForAdd(index);
//确认list容量,如果不够,容量加1。注意:只加1,保证资源不被浪费
ensureCapacityInternal(size + 1); // Increments modCount!!
// 对数组进行复制处理,目的就是空出index的位置插入element,并将index后的元素位移一个位置
//在插入元素之前,要先将index之后的元素都往后移一位
//arraycopy(原数组,源数组中的起始位置,目标数组,目标数据中的起始位置,要复制的数组元素的数量)
System.arraycopy(elementData, index, elementData, index + 1, size - index);
//将指定的index位置赋值为element
elementData[index] = element;
//实际容量+1
size++;
}
/**
* A version of rangeCheck used by add and addAll.
*/
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)//插入的位置不能大于size 和小于0,如果是就报越界异常
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
在指定的位置添加元素,也就是插入元素。
举例:在索引2处添加元素e,大概过程如下:
2.4.2、get()方法
/**
* Returns the element at the specified position in this list.
* 返回list指定位置的元素
* @param index index of the element to return 要返回的元素索引
* @return the element at the specified position in this list 位于list中指定位置的元素
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E get(int index) {
rangeCheck(index); //越界检查
return elementData(index); //返回索引为index的元素
}
/**
* Checks if the given index is in range. If not, throws an appropriate
* runtime exception. This method does *not* check if the index is
* negative: It is always used immediately prior to an array access,
* which throws an ArrayIndexOutOfBoundsException if index is negative.
检查指定索引是否在范围内。如果不在,抛出一个运行时异常。
这个方法不检查索引是否为负数,它总是在数组访问之前立即优先使用,
如果给出的索引index>=size,抛出一个越界异常
*/
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
/**
* Constructs an IndexOutOfBoundsException detail message.
* Of the many possible refactorings of the error handling code,
* this "outlining" performs best with both server and client VMs.
*/
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
// Positional Access Operations 位置访问操作
// 返回索引为index的元素
@SuppressWarnings("unchecked")
E elementData(int index) {
return (E) elementData[index];
}
从代码中可以看到,因为ArrayList底层是数组,所以它的get方法非常简单,先是判断一下有没有越界(索引小于0或者大于等于数组实际长度,错误信息返回索引和数组的实际长度),之后就直接通过数组下标来获取元素。
2.4.3、set()方法
/**
* Replaces the element at the specified position in this list with
* the specified element.
* 用指定的元素替换列表中指定位置的元素。
* @param index index of the element to replace 要替换的元素的索引
* @param element element to be stored at the specified position 要存储在指定位置的元素
* @return the element previously at the specified position 先前位于指定位置的元素(返回被替换的元素)
* @throws IndexOutOfBoundsException {@inheritDoc} 如果参数指定索引index>=size,抛出一个越界异常
*/
public E set(int index, E element) {
//检查索引是否越界。如果参数指定索引index>=size,抛出一个越界异常
rangeCheck(index);
//记录被替换的元素(旧值)
E oldValue = elementData(index);
//替换元素(新值)
elementData[index] = element;
//返回被替换的元素
return oldValue;
}
确保set的位置小于当前数组的长度(size)并且大于0,获取指定位置(index)元素,然后放到oldValue存放,将需要设置的元素放到指定的位置(index)上,然后将原来位置上的元素oldValue返回给用户。
2.4.4、remove()方法
2.4.4.1、根据索引remove,通过删除指定位置上的元素
/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left (subtracts one from their
* indices).
* 删除list中位置为指定索引index的元素,索引之后的元素向左移一位
* @param index the index of the element to be removed 被删除元素的索引
* @return the element that was removed from the list 被删除的元素
* @throws IndexOutOfBoundsException {@inheritDoc} 如果参数指定索引index>=size,抛出一个越界异常
*/
public E remove(int index) {
//检查索引是否越界。如果参数指定索引index>=size,抛出一个越界异常
rangeCheck(index);
//结构性修改次数+1
modCount++;
//记录索引处的元素
E oldValue = elementData(index);
//删除指定元素后,需要左移的元素个数
int numMoved = size - index - 1;
//如果有需要左移的元素,就移动(移动后,该删除的元素就已经被覆盖了)
if (numMoved > 0)
//arraycopy(原数组,源数组中的起始位置,目标数组,目标数据中的起始位置,要复制的数组元素的数量)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
//size减一,然后将索引为size-1处的元素置为null。为了让GC起作用,必须显式的为最后一个位置赋null值
elementData[--size] = null; // clear to let GC do its work
//返回被删除的元素
return oldValue;
}
从中我们可以看到根据索引删除元素的步骤:
- 进行越界检查
- 记录修改次数(modCount 可以用来检测快速失败的一种标志。)
- 通过索引找到要删除的元素
- 计算要移动的位数
- 移动元素(其实是覆盖掉要删除的元素)
- 将–size上的位置赋值为null,让gc(垃圾回收机制)更快的回收它。
- 返回被删除的元素
2.4.4.2、根据对象remove
/**
* Removes the first occurrence of the specified element from this list,
* if it is present. If the list does not contain the element, it is
* unchanged. More formally, removes the element with the lowest index
* <tt>i</tt> such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
* (if such an element exists). Returns <tt>true</tt> if this list
* contained the specified element (or equivalently, if this list
* changed as a result of the call).
* 从列表中删除指定元素的第一个出现项,如果它存在的话。如果列表不包含该元素,它将保持不变。更正式地说,删除索引最低的元素...
* @param o element to be removed from this list, if present
* @return <tt>true</tt> if this list contained the specified element
*/
public boolean remove(Object o) {
if (o == null) {
//循环遍历,找到后调用fastRemove(index) 快速删除,通过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 remove method that skips bounds checking and does not
* return the value removed.
私有的remove方法,该方法跳过边界检查,并且不返回已删除的值
*/
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
//arraycopy(原数组,源数组中的起始位置,目标数组,目标数据中的起始位置,要复制的数组元素的数量)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}
循环遍历所有对象,得到对象所在索引位置,然后调用fastRemove方法,执行remove操作。
定位到需要remove的元素索引,先将index后面的元素往前面移动一位(调用System.arraycopy实现),然后将最后一个元素置空。
这里最主要是知道arrayList可以存储null值。
2.4.4.3、ArrayList 内部方法 removeRange(int fromIndex, int toIndex)
/**
* Removes from this list all of the elements whose index is between
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
* Shifts any succeeding elements to the left (reduces their index).
* This call shortens the list by {@code (toIndex - fromIndex)} elements.
* (If {@code toIndex==fromIndex}, this operation has no effect.)
* 从该列表中删除索引位于两者之间的所有元素,包含fromIndex,但是不包含toIndex,
* 将任何后续元素向左移动(减少它们的索引)
* 这个调用通过{@code (toIndex - fromIndex)}元素缩短列表。
*(如果{@code toIndex==fromIndex},此操作无效。)
* @throws IndexOutOfBoundsException if {@code fromIndex} or
* {@code toIndex} is out of range
* ({@code fromIndex < 0 ||
* fromIndex >= size() ||
* toIndex > size() ||
* toIndex < fromIndex})
*/
protected void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex; //被删除的索引后面的个数
//arraycopy(原数组,源数组中的起始位置,目标数组,目标数据中的起始位置,要复制的数组元素的数量)
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// clear to let GC do its work
int newSize = size - (toIndex-fromIndex); //新数组的长度
for (int i = newSize; i < size; i++) {
elementData[i] = null; //置为null,等待GC
}
size = newSize;
}
此方法删除fromIndex到toIndex之间的全部元素,把toIndex以后的元素移动(size-toIndex)位,把左移后空的元素置为null好让垃圾回收机制回收内存,最后把新数组的大小赋值给size。
2.4.4.4、removeAll(Collection<?> c) 与 retainAll(Collection<?> c)
待完善。。。
2.4.5、indexOf() 与 lastIndexOf()
/**
* Returns the index of the first occurrence of the specified element
* in this list, or -1 if this list does not contain the element.
* More formally, returns the lowest index <tt>i</tt> such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
* or -1 if there is no such index.
返回此列表中指定元素的第一个出现项的索引,如果该列表不包含该元素,则返回-1。
*/
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;
}
/**
* Returns the index of the last occurrence of the specified element
* in this list, or -1 if this list does not contain the element.
* More formally, returns the highest index <tt>i</tt> such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
* or -1 if there is no such index.
返回此列表中指定元素的最后一次出现的索引,如果该列表不包含该元素,则返回-1。
*/
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;
}
2.4.6、clear()方法
/**
* Removes all of the elements from this list. The list will
* be empty after this call returns.
从列表中删除所有元素。该调用返回后,列表将为空。
*/
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
3、总结
- arrayList可以存放null。
- arrayList本质上就是一个elementData数组。
- arrayList区别于数组的地方在于能够自动扩展大小,其中关键的方法就是gorw()方法。
- arrayList中removeAll(collection c)和clear()的区别就是removeAll可以删除批量指定的元素,而clear是全是删除集合中的元素。
- arrayList由于本质是数组,所以它在数据的查询方面会很快,而在插入删除这些方面,性能下降很多,有移动很多数据才能达到应有的效果。
- arrayList实现了RandomAccess,所以在遍历它的时候推荐使用for循环。