1.类的定义

public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable

其实现的各个接口及集成类。
RandomAccess此接口可以实现随机访问,实现此接口的类使用for循环遍历比迭代器遍历性能更高

2.基本成员变量

1.序列化所需

private static final long serialVersionUID = 8683452581122892189L;

2.默认构造大小

private static final int DEFAULT_CAPACITY = 10;

3.一个空的数组

private static final Object[] EMPTY_ELEMENTDATA = {};

4.存储数据所需的数组(ArrayList是基于数组的)

private transient Object[] elementData;

5.当前集合的长度

private int size;

6.ArrayList的最大长度

private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

3.构造方法

1.有参构造 参数为初始化的集合大小,给elementData创建数组

public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}

2.无参构造 直接将elementData置为空数组

public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}

3.有参构造 将一个集合构造成ArrayList

public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}

4.trimToSize方法

public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = Arrays.copyOf(elementData, size);
}
}

此方法是去掉ArrayList中的空余空间

5.ensureCapacity

public void ensureCapacity(int minCapacity) {
int minExpand = (elementData != EMPTY_ELEMENTDATA)
// any size if real element table
? 0
// larger than default for empty table. It's already supposed to be
// at default size.
: DEFAULT_CAPACITY;

if (minCapacity > minExpand) {
ensureExplicitCapacity(minCapacity);
}
}

可以通过此方法预设ArrayList的大小,其目的是为了减少ArrayList的频繁扩容

6.ensureCapacityInternal

private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}

ensureExplicitCapacity(minCapacity);
}

此为内部调用的扩容方法,作用和上面相同。

7.ensureExplicitCapacity

private void ensureExplicitCapacity(int minCapacity) {
modCount++;

// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}

此为真正的扩容上面5.6其实调用的都是此方法,然后此方法调用​​grow(size)​​进行扩容

8.grow

private void grow(int minCapacity) {
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}

可以看出其扩容为​​oldCapacity + (oldCapacity >> 1)​​及1.5倍扩容

9.hugeCapacity

private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}

返回最大容量

10.size

public int size() {
return size;
}

返回当前长度

11.isEmpty

public boolean isEmpty() {
return size == 0;
}

判断当前集合是否为空

12.contains

public boolean contains(Object o) {
return indexOf(o) >= 0;
}

判断是否包含某个元素,通过查找这个元素的索引来进行判断。

13.indexOf

public int indexOf(Object o) {
//先判断o是否为空,再根据情况进行遍历整个ArrayList
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;
}

从前往后找某个元素,然后返回下标。

14.lastIndexOf

public int lastIndexOf(Object o) {
if (o == null) {
for (int i = size-1; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = size-1; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}

从后往前找某个元素,然后返回下标。

15.clone

public Object clone() {
try {
@SuppressWarnings("unchecked")
ArrayList<E> v = (ArrayList<E>) 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();
}
}

克隆一个数组即可。

16.toArray

public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}

返回一个包含ArrayList中所有元素的数组

17.toArray

public <T> T[] toArray(T[] a) {
if (a.length < size)
//如果输入的数组大大小小于ArrayList的大小,则新创建一个并且返回
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
//直接复制
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}

将ArrayList的值放入到某个数组中

18.elementData

E elementData(int index) {
return (E) elementData[index];
}

直接返回某个下标的数据

19.get

public E get(int index) {
//先检查是否合法
rangeCheck(index);
return elementData(index);
}

可以看出此方法调用了18方法

20.set

public E set(int index, E element) {
//先检查合法性
rangeCheck(index);
//获得原来的数据
E oldValue = elementData(index);
//将新的放入
elementData[index] = element;
//返回旧数据
return oldValue;
}

21.add(obj)

public boolean add(E e) {
//先看是否需要扩容
ensureCapacityInternal(size + 1); // Increments modCount!!
//存入数据
elementData[size++] = e;
return true;
}

22.add(index,obj)

public void add(int index, E element) {
//检查下标
rangeCheckForAdd(index);
//检验是否需要扩容
ensureCapacityInternal(size + 1); // Increments modCount!!
//将数组中[index,size)的数据拷贝到[index+1,size+1),及向后移动
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}

23.remove(index)

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);
//将最后一个元素置为空便于gc回收
elementData[--size] = null; // clear to let GC do its work
//返回以前的值
return oldValue;
}

删除某个下标下的某个元素

24.remove(obj)

public boolean remove(Object o) {
//根据不同情况进行循环遍历,然后表用fastRemove,通过下标删除
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;
}

25. fastRemove

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
}

此方法和23方法基本相同,只是取消了返回值。

26.clear

public void clear() {
modCount++;

// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;

size = 0;
}

清除整个ArrayList

27.addAll(Collection)

public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
//根据情况进行扩容
ensureCapacityInternal(size + numNew); // Increments modCount
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}

将一个Collection集合直接放入到ArrayList中

28.addAll(index,Collection)

public boolean addAll(int index, Collection<? extends E> c) {
//检查下标合法性
rangeCheckForAdd(index);
//获得数组
Object[] a = c.toArray();
//获得要插入的数组长度
int numNew = a.length;
//根据情况看是否需要扩容
ensureCapacityInternal(size + numNew); // Increments modCount
//需要移动的元素数
int numMoved = size - index;
//移动
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);
//将a数组中的元素放入
System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}

将某个集合中的元素插入到index下标之后

29.removeRange

protected void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);

// clear to let GC do its work
int newSize = size - (toIndex-fromIndex);
for (int i = newSize; i < size; i++) {
elementData[i] = null;
}
size = newSize;
}

删除这个[from,to)区间内的所有元素

30. rangeCheck

private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

检查下标是否合法

31.rangeCheckForAdd

private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

检查在添加时候的下标合法性

32.outOfBoundsMsg

private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}

返回的越界信息

33.removeAll

public boolean removeAll(Collection<?> c) {
return batchRemove(c, false);
}

直接调用其他方法可以看到第二个参数为false

34.retainAll

public boolean retainAll(Collection<?> c) {
return batchRemove(c, true);
}

也是直接调用其他方法可以看到此时调用的时候其第二个参数为true

35.batchRemove

private boolean batchRemove(Collection<?> c, boolean complement) {
final Object[] elementData = this.elementData;
int r = 0, w = 0;
boolean modified = false;
try {
//遍历ArrayList查看之中有没有c集合中的元素,然后根据不同的complement,进行处理,可以看出当为true的时候说明保留ArrayList和c重复的元素,当位false的时候保留不重复的
for (; r < size; r++)
if (c.contains(elementData[r]) == complement)
elementData[w++] = elementData[r];
} finally {
// Preserve behavioral compatibility with AbstractCollection,
// even if c.contains() throws.
//当r不等于size的时候说明上面的for并没有执行完毕说明有错误所以需要其之后的续接上
if (r != size) {
System.arraycopy(elementData, r,
elementData, w,
size - r);
w += size - r;
}
//如果w!=size说明当前的ArrayList有了空余空间,所以需要释放空余空间,并且重置size
if (w != size) {
// clear to let GC do its work
for (int i = w; i < size; i++)
elementData[i] = null;
modCount += size - w;
size = w;
modified = true;
}
}
return modified;
}

根据complement的不同,去重复的或求交集。

其余方法为很少调用的方法以及迭代器方法就不在此论述了