成员变量
/**
* 初始容量
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* 空数组容器
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
/**
* 用于默认大小的空数组实例 和EMPTY_ELEMENTDATA是用不同的扩展策略
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
* 存储ArrayList元素的数组缓冲区
*/
transient Object[] elementData; // non-private to simplify nested class access
/**
* The size of the ArrayList (the number of elements it contains).
* 大小
*/
private int size;
构造方法
//initialCapacity 容量
public ArrayList(int initialCapacity) {
//根据容量创建数组
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
//如果为0,就把 EMPTY_ELEMENTDATA空数组赋值给elementData
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
//抛异常
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
//空参构造方法 elementData为默认大小的空数组
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public ArrayList(Collection<? extends E> c) {
//把Collection(集合)转成数组
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
//有可能出错,没有转成Object数组
if (elementData.getClass() != Object[].class)
//copy一份
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
//空数组
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
add方法
public boolean add(E e) {
//确定内部容量
ensureCapacityInternal(size + 1); // Increments modCount!!
//添加数据,更新size
elementData[size++] = e;
return true;
}
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
ensureCapacityInternal(size + 1); // Increments modCount!!
//从index开始(包括),数据向后移动一位(这里的效率低)
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
//确定内部容量
private void ensureCapacityInternal(int minCapacity) {
// 第一次就会走这个
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
//添加一条--九条,minCapacity最小容量都是10。
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
//确定明确的容量
private void ensureExplicitCapacity(int minCapacity) {
// 修改次数
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
//扩充容器
private void grow(int minCapacity) {
//当前数组的大小
int oldCapacity = elementData.length;
//当前数组的大小+当前数组大小的一半也就是扩充1.5倍
int newCapacity = oldCapacity + (oldCapacity >> 1);
//如果小于minCapacity
if (newCapacity - minCapacity < 0)
//重新设置容器大小
newCapacity = minCapacity;
//大于Integer.MAX_VALUE - 8;
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);
}
//处理容量巨大时的情况
private static int hugeCapacity(int minCapacity) {
//超过int的最大值,抛出异常
if (minCapacity < 0)
throw new OutOfMemoryError();
//根据情况设置为int的最大值或者MAX_ARRAY_SIZE
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
这里大概描述一下流程
如果默认new ArrayList();—>此刻elementData==DEFAULTCAPACITY_EMPTY_ELEMENTDATA–>我们添加第一条数据,minCapacity=size+1=1;—> minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity) =10;—>10-elementData.length > 0成立–>grow(10)–>newCapacity经过一些列计算最终赋值为10–>通过copyOf方法,把当前null的elementData变成一个容量为10的新的数组赋值给elementData—>最后 elementData[size++] = e(执行顺序,先elementData[0]=e;再size=1),把数据添加进去。
get()方法
//获取指定位置上的数据
public E get(int index) {
//非法参数校验
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
//返回数据
return elementData(index);
}
直接根据index锁定内存。效率很高
set()方法
public E set(int index, E element) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
E oldValue = (E) elementData[index];
elementData[index] = element;
return oldValue;
}
也比较简单就能看明白,先通过index查找出旧的数据,再把新数据进行替换返回出去。
remove()方法
public E remove(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
modCount++;
E oldValue = (E) elementData[index];
//计算要移动数据的个数
int numMoved = size - index - 1;
if (numMoved > 0)
//从index+1开始,所有数据向前移动一位,这样,index位置上的数据就被覆盖了,相当于移除了
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
//容器的最后一位置null;应为每次都要移动空一位,同时更新size
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;
}
//跟remove(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
}
clear()方法
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}