对ArrayList进行简易的源码剖析
首先查看常用的参数
public class ArrayList extends AbstractList
implements List, RandomAccess, Cloneable, java.io.Serializable
{
//默认容量大小
private static final int DEFAULT_CAPACITY = 10;
//默认空数组
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
//内部存储数据的数组
transient Object[] elementData;
//数量
private int size;
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
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);
}
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;
}
}
我们查看3个构造方法,
ArrayList()-> this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
ArrayList(int initialCapacity)-> this.elementData = new Object[initialCapacity];
ArrayList(Collection extends E> c)->elementData = c.toArray();
都是对elementData进行初始化,显然易见,ArrayList内部就是使用的数组进行存储数据。
接下来查看add:
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
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++;
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
首先是检查当前数组的大小是否足够,是否需要扩容,当不需要扩容时,直接添加。
重点查看add(int index, E element) 中的
System.arraycopy(elementData, index, elementData, index + 1,size - index);
elementData[index] = element;
将index后的数据全部往后移动一位,然后在当前的index位置赋值新增的数据。
由此可以知道ArrayList在中间插入的效率是相对比较低的,需要移动数组内的数组,腾出位置给添加的数据。
接着查询方法
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
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;
}
判断下是否为空,然后直接通过数组获取
接下来查询
public E remove(int index) {
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;
}
public boolean remove(Object o) {
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;
}
与add类似,删除元素后,使用
System.arraycopy(elementData, index+1, elementData, index,numMoved)进行数组拷贝,index后的数据全部往前移动一位
改的方法:
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
根据我们查看的常用参数和构造、增删改查的函数,可知道:
1.ArrayList内部是由一个顺序数组组成,底层使用System.arraycopy进行数组的移动
2.add时,则要先注意数组的大小,是否需要扩容。
3.在进行任意位置添加与删除时,需要对数组进行移动,速度较低,但是查询与修改时根据index,可快速进行