1.类图
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
有以上类图和代码可以看出ArrayList继承了AbstractList,实现了List,RandAccess,Cloneable,Serializable接口。
ArrayList是实现List接口的,底层采用数组实现,可以根据集合大小进行自动扩容。
ArrayList 实现了Cloneable接口,即覆盖了函数clone(),能被克隆。
ArrayList 实现java.io.Serializable接口,这意味着ArrayList支持序列化,能通过序列化去传输。
2.原理
2.1 类成员变量
private static final long serialVersionUID = 8683452581122892189L;
//默认初始化大小
private static final int DEFAULT_CAPACITY = 10;
//调用有参构造器且参数为0时的初始化数组
private static final Object[] EMPTY_ELEMENTDATA = new Object[0];
//调用无参构造器时初始化的数组
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = new Object[0];
//真正保存数据的数组
transient Object[] elementData;
//列表的实际元素数量
private int size;
//数组的最大容量
private static final int MAX_ARRAY_SIZE = 2147483639;
2.2 构造方法
//参数为数组容量的构造方法,并对参数进行非负检查
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else {
if (initialCapacity != 0) {
throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
}
this.elementData = EMPTY_ELEMENTDATA;
}
}
//无参构造方法
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
//参数为集合的构造方法,
//将集合转化为数组,对数组长度进行检查,若不为0将元素将元素赋给elementData,若为0则构建空数组
public ArrayList(Collection<? extends E> c) {
this.elementData = c.toArray();
if ((this.size = this.elementData.length) != 0) {
if (this.elementData.getClass() != Object[].class) {
this.elementData = Arrays.copyOf(this.elementData, this.size, Object[].class);
}
} else {
this.elementData = EMPTY_ELEMENTDATA;
}
}
2.3 核心方法
2.3.1 获取
public E get(int index) {
Objects.checkIndex(index, this.size);
return this.elementData(index);
}
对index进行有效性检验,如果参数合法,则返回对应数组下标的元素
2.3.2 插入
private void add(E e, Object[] elementData, int s) {
if (s == elementData.length) {
elementData = this.grow();
}
elementData[s] = e;
this.size = s + 1;
}
//将元素添加至列表末尾,若数组容量不够则进行扩容后添加至数组末尾
public boolean add(E e) {
++this.modCount;
this.add(e, this.elementData, this.size);
return true;
}
//将元素添加至指定位置,先对index进行有效性检验,再对数组的容量进行检查。
//将数组index后面的元素后移,将元素插入至index位置
public void add(int index, E element) {
this.rangeCheckForAdd(index);
++this.modCount;
int s;
Object[] elementData;
if ((s = this.size) == (elementData = this.elementData).length) {
elementData = this.grow();
}
System.arraycopy(elementData, index, elementData, index + 1, s - index);
elementData[index] = element;
this.size = s + 1;
}
ArrayList底层是数组实现,在指定位置插入元素后,需要将后面的元素向后移动。
2.3.3 删除
删除某个位置的元素
private void fastRemove(Object[] es, int i) {
++this.modCount;
int newSize;
if ((newSize = this.size - 1) > i) {
System.arraycopy(es, i + 1, es, i, newSize - i);
}
es[this.size = newSize] = null;
}
首先对参数进行有效性检查,接着将index位置后的数组元素往前挪一位,最后减小列表大小
删除某个具体元素
public boolean remove(Object o) {
Object[] es = this.elementData;
int size = this.size;
int i = 0;
if (o == null) {
while(true) {
if (i >= size) {
return false;
}
if (es[i] == null) {
break;
}
++i;
}
} else {
while(true) {
if (i >= size) {
return false;
}
if (o.equals(es[i])) {
break;
}
++i;
}
}
this.fastRemove(es, i);
return true;
}
先判断要删除的元素是否为null,不为null的话遍历所有元素,找到需要删除的元素,调用fastRemove方法删除。
fastRemove方法
private void fastRemove(Object[] es, int i) {
++this.modCount;
int newSize;
if ((newSize = this.size - 1) > i) {
System.arraycopy(es, i + 1, es, i, newSize - i);
}
es[this.size = newSize] = null;
}
2.3.4 扩容
当插入时容量不足,便会触发扩容
private int newCapacity(int minCapacity) {
int oldCapacity = this.elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity <= 0) {
if (this.elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(10, minCapacity);
} else if (minCapacity < 0) {
throw new OutOfMemoryError();
} else {
return minCapacity;
}
} else {
return newCapacity - 2147483639 <= 0 ? newCapacity : hugeCapacity(minCapacity);
}
}
int newCapacity = oldCapacity + (oldCapacity >> 1);
可以发现,ArrayList在容量不够的情况下容量会扩大到原来的1.5倍。
3.适用场景
因为Array是基于索引(index)的数据结构,它使用索引在数组中搜索和读取数据是很快的,可以直接返回数组中index位置的元素,因此在随机访问集合元素上有较好的性能。Array获取数据的时间复杂度是O(1),但是要插入、删除数据却是开销很大的,因为这需要移动数组中插入位置之后的的所有元素。
如果应用程序对数据有较多的随机访问,ArrayList对象要优于LinkedList对象;
4.总结
4.1 读取时间复杂度O(1),修改时间复杂度O(n)
4.2 非线程安全
4.3 扩容默认扩大到原来的1.5倍