0%

ArrayList源码

一、创建ArrayList对象

ArrayList有三个构造函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* 空数组
*/
private static final Object[] EMPTY_ELEMENTDATA = {};

/**
* 空数组.
* We distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
* first element is added.
*
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

/**
* 构造一个指定容量的空数组
*
* @param initialCapacity 指定容量的大小
* @throws IllegalArgumentException 如果指定大小为负数的话
*/
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
// 如果指定的容量大于0则创建一个大小为initialCapacity的Object数组
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
// 如果指定的容量是0则默认给一个空数组
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}

/**
* 构造一个初始容量为10的空的数组
* 初始容量真正被初始化成10的时候是在add扩容的时候
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}

/**
* 构造一个包含特殊集合的数组
*
* @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;
}
}

二、Add方法

当调用Add方法的时候,会把元素添加到list最后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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!!
// 底层是C看不懂
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}

最核心的内容就是ensureCapacityInternal,这是自动扩容机制的核心

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
private void ensureCapacityInternal(int minCapacity) {
// 如果是调用无参构造创建list对象,则去在list的size和默认容量10中取大的
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}

ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
// 记录list的修改的次数
modCount++;

// overflow-conscious code
// 如果要扩展的容量大于当前的数组的大小,就开始进行扩容操作
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length; // 老的容量
int newCapacity = oldCapacity + (oldCapacity >> 1); // 新的容量是老的容量1.5倍
// 如果扩容的大小比传入的容量小
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
// 如果扩容的大小大于最大的容量(可能会OOM)
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) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}

三、set和get

Array的put和get函数就比较简单了,先做index检查,然后执行赋值或访问操作:

1
2
3
4
5
6
7
8
9
10
11
12
public E get(int index) {
rangeCheck(index);

return elementData(index);
}
public E set(int index, E element) {
rangeCheck(index);

E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}

四、remove

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
// 把最后的置null
elementData[--size] = null; // clear to let GC do its work

return oldValue;
}

参考资料

Java-ArrayList工作原理及实现