// 参数是capacity
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() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
// 参数是集合的
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray(); // Object[] toArray(); 返回一个新分配的对象数组,包含此集合的所有元素
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
// (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
// Arrays.asList(x).toArray().getClass()返回的class类型不一定是Object[].class
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class); // 关于数组的复制,见下面
} else {
// 指向空数组
this.elementData = EMPTY_ELEMENTDATA;
}
}
数组复制函数如下:
System类:
// src是源数组、srcPos源数组开始的位置、dest是目的数组、destPos是目的数组开始的位置、length是元素复制的长度
// 如果src和dest是同一个引用,复制的时候,会把这个数组先复制到一个临时数组,然后在从临时数组复制到这个数组
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
// 列表中第一次出现对象o的位置,找到就立刻返回,找不到返回-1
// 使用的是元素的equals方法
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData))
return i;
}
return -1;
}
// 列表中最后一次出现对象o的位置,找到就立刻返回,找不到返回-1
// 使用的是元素的equals方法
public int lastIndexOf(Object o) {
if (o == null) {
for (int i = size-1; i >= 0; i--)
if (elementData==null)
return i;
} else {
for (int i = size-1; i >= 0; i--)
if (o.equals(elementData))
return i;
}
return -1;
}
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
set
// 检查是否越界,设置元素,返回老元素
public E set(int index, E element) {
rangeCheck(index);
// 检查位置是否越界,在原数组上把index后面(不包括index)的元素向前移动一位
// size减1,返回删除的元素
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);
elementData[--size] = null; // clear to let GC do its work
return oldValue;
}
// 查找是否存在o,如果存在就调用fastRemove(int)方法,返回true
// 否则返回false
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)差不多,就是去掉了越界检查和返回值
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
}
// 将数组清空
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData = null;
// clear to let GC do its work
int newSize = size - (toIndex-fromIndex);
for (int i = newSize; i < size; i++) {
elementData = null;
}
size = newSize;
}
// 把c包含的元素都删除掉(前提是contains方法不抛错)
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, false);
}
// 仅留下c包含的元素(前提是contains方法不抛错)
public boolean retainAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, true);
}
private boolean batchRemove(Collection<?> c, boolean complement) {
final Object[] elementData = this.elementData;
int r = 0, w = 0;
boolean modified = false;
try {
for (; r < size; r++)
if (c.contains(elementData[r]) == complement)
elementData[w++] = elementData[r];
} finally {
// 返回一个新数组
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
// 如果a.length小于size则返回elementData的copy
// 否则,将elementData的元素直接复制到a中
public <T> T[] toArray(T[] a) {
if (a.length < size)
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
iterator
// 返回Iterator
public Iterator<E> iterator() {
return new Itr();
}
// 返回指定位置的ListIterator
public ListIterator<E> listIterator(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index);
return new ListItr(index);
}
// 返回第0位的ListIterator
public ListIterator<E> listIterator() {
return new ListItr(0);
}
Iterator和listIterator都是ArrayList的私有内部类,继承结构如下:
private class Itr implements Iterator<E>
private class ListItr extends Itr implements ListIterator<E>
public interface ListIterator<E> extends Iterator<E>
// 调用Arrays的sort函数进行排序
public void sort(Comparator<? super E> c) {
final int expectedModCount = modCount;
Arrays.sort((E[]) elementData, 0, size, c);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
1
2
3
4
5
6
7
8
9
clone
// 克隆方法,克隆对象的elementData是一个新数组,属于浅拷贝
public Object clone() {
try {
ArrayList<?> v = (ArrayList<?>) super.clone();
v.elementData = Arrays.copyOf(elementData, size); // 赋值新数组
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError(e);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
serialize
// 序列化的写入操作会调用此方法
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
// Write out element count, and any hidden stuff
int expectedModCount = modCount;
s.defaultWriteObject();
// Write out size as capacity for behavioural compatibility with clone()
s.writeInt(size);
// Write out all elements in the proper order.
for (int i=0; i<size; i++) {
s.writeObject(elementData);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}