1、概述
ArrayList
可以说是我们最常用的集合类了,底层通过数组实现,同时允许有空值存在。
首先我们看源码中ArrayList
类的继承以及实现:
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
我们可以看出,ArrayList
继承了AbstractList
,实现了List<E>
、RandomAccess
、Cloneable
、Serializable
类。所以ArrayList
支持快速访问、复制、序列化的。
2、成员变量
//默认容量
private static final int DEFAULT_CAPACITY = 10;
//空数组
private static final Object[] EMPTY_ELEMENTDATA = {};
//空数组
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
//存放集合数据
transient Object[] elementData; // non-private to simplify nested class access
//实际元素大小
private int size;
//AbstractList里的属性,记录对list的操作次数
protected transient int modCount = 0;
注意:
size
代表的是数组中实际的元素个数,而elementData.length
为集合的容量,表示数组最多可以存储多少元素。- 两个空数组,简单来讲就是第一次添加元素时知道该
elementData
从空的构造函数还是有参构造函数被初始化的。以便确认如何扩容。
3、构造函数
3.1、无参构造
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
默认构造了一个容量为10的空的List
集合,但初始化的时候只是赋了一个空的数组,实际上是第一次添加元素将容量扩容到10的。
3.2、根据容量构造
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);
}
}
构造一个初时容量为initialCapacity
的List集合。在无参构造中我们使用的是DEFAULTCAPACITY_EMPTY_ELEMENTDATA
,而在根据容量构造时,如果容量为0我们使用的是EMPTY_ELEMENTDATA
,这些差别我们一会在add
方法中就可以看到区别。
3.3、根据Collection构造
public ArrayList(Collection<? extends E> c) {
Object[] a = c.toArray();
if ((size = a.length) != 0) {
if (c.getClass() == ArrayList.class) {
elementData = a;
} else {
elementData = Arrays.copyOf(a, size, Object[].class);
}
} else {
// replace with empty array.
elementData = EMPTY_ELEMENTDATA;
}
}
将Collection转换为数组赋给elementData
,如果c
的size
不为0,则判断c
的类型,如果与该List
不一致则作一次转换再赋值。如果c
的size为
0,则赋空数组:EMPTY_ELEMENTDATA
。
4、主要方法
4.1、add方法
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
private void ensureExplicitCapacity(int minCapacity) {
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;
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) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
在每次进行add
操作之前都要先进行容量确定。然后size
增加一。
calculateCapacity
函数中elementData
如果为DEFAULTCAPACITY_EMPTY_ELEMENTDATA
,则返回DEFAULT_CAPACITY
,与minCapacity
的最大值,对于第一次add
当然是DEFAULT_CAPACITY
更大一些,所以无参构造默认构造容量为10的List
。而如果是有参构造的,也就是通过EMPTY_ELEMENTDATA
构造的List
,第一次add
后容量为1,这就是两者的区别。ensureExplicitCapacity
函数modCount
自增,然后判断minCapacity
与elementData.length
的大小,如果minCapacity
大,就进行扩容操作。grow
函数也很好理解,首先将原来容量扩大1.5倍,如果1.5倍满足不了新的容量需求,则直接将所需容量当作新容量。如果所需容量或者1.5容量过大,则直接Integer.MAX_VALUE
作为新的容量。- 可能有人会问为什么扩容的时候先判断1.5倍是否合乎要求而不是直接扩容到所需容量?那是因为如果每次扩展都是只扩展到当时所需容量,那么一旦执行一次扩容后,下一次再次执行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++;
}
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;
}
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);
System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
add
方法有种,一种是默认在数组最后方添加新的元素;另一种是在索引index
处添加或者修改元素,这种方法在最开始会判断index
位置是否存在(包括数组后方第一个元素),不存在会抛出IndexOutOfBoundsException
数组越界异常。
addAll(Collection<? extends E> c)
是将Collection
中的所有元素放在该List
最后。
addAll(int index, Collection<? extends E> c)
是将Collection
中的所有元素放在该List
索引index
位置,原List
的index
后元素后移。
4.2、set方法
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
该方法是修改数组上index
位置上的元素,改为element
。该方法与add(int index, E element)
方法类似。区别就是add
可以在数组最后添加新的元素,而set
只能修改已存在的元素。这也就是rangeCheck
与rangeCheckForAdd
的区别。
4.3、get方法
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
获取索引index
位置的元素。
4.4、remove方法
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;
}
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;
}
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
}
先看这两常用的romove
方法,第一个是通过索引index
来移除index
处的元素,然后将后面元素前移,size
减一,当然index
需要判断是否存在。第二个是通过对象删除,如果为空则找到第一个为空的元素位置然后调用fastRemove
移除该元素,将index
后的元素前移,size
减一;如何不为空则找到第一个与给定对象equals
的元素位置,然后就跟为空一样了。没有找到则返回false
。fastRemove
方法其实跟remove(int index)
一样只是返回值不同。
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, false);
}
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 {
// Preserve behavioral compatibility with AbstractCollection,
// even if c.contains() throws.
if (r != size) {
System.arraycopy(elementData, r,
elementData, w,
size - r);
w += size - r;
}
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;
}
removeAll()
是去除所有存在于Collection c
中的元素。而retainAll()
是去除所有不存在于Collection c
中的元素。这两个方法刚好相反,第一个相当于求交集之后再求补集,另一个是求交集。
@Override
public boolean removeIf(Predicate<? super E> filter) {
//首先要验证规则不能为空
Objects.requireNonNull(filter);
// figure out which elements are to be removed
// any exception thrown from the filter predicate at this stage
// will leave the collection unmodified
//计数器,记录revove掉的次数
int removeCount = 0;
//声明要remove掉的集合,声明为set,保证不重复remove
final BitSet removeSet = new BitSet(size);
final int expectedModCount = modCount;
final int size = this.size;
//循环遍历list
for (int i=0; modCount == expectedModCount && i < size; i++) {
@SuppressWarnings("unchecked")
final E element = (E) elementData[i];
//boolean test(T t);校验参数是否匹配,匹配则返回true,如果匹配,则在removeSet中存入索引,removeCount值加一
if (filter.test(element)) {
removeSet.set(i);
removeCount++;
}
}
//校验此时的集合是否在多线程IO下受到改变,若受到改变则抛出异常
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
//源码中这句,表明了,将不需要移除的空间左移
// shift surviving elements left over the spaces left by removed elements
//定义anyToRemove,如果removeCount > 0则为true
final boolean anyToRemove = removeCount > 0;
//如果anyToRemove为true则继续进行,否则意味着没有匹配的可移除元素,直接return anyToRemove = false;
if (anyToRemove) {
//若有元素符合移除条件,则声明新size,重新定义list大小(ArrayList初始默认容量为10)
final int newSize = size - removeCount;
//循环遍历元素赋值,这里用到了一个新的方法nextClearBit(fromIndex)
//将需要移除的元素在重新存储的时候跳过
for (int i=0, j=0; (i < size) && (j < newSize); i++, j++) {
i = removeSet.nextClearBit(i);
elementData[j] = elementData[i];
}
//如果新的集合与原集合比较,将指定要移除的设置为null,GC回收内存
for (int k=newSize; k < size; k++) {
elementData[k] = null; // Let gc do its work
}
//更新全局变量的集合大小为移除后的新集合大小
this.size = newSize;
//再次判断是否受到多线程IO影响
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
return anyToRemove;
}
//官方解释:返回设置为在指定的起始索引上或之后出现的第一个位的索引。
public int nextClearBit(int fromIndex) {
// Neither spec nor implementation handle bitsets of maximal length.
// See 4816253.
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex);
checkInvariants();
int u = wordIndex(fromIndex);
if (u >= wordsInUse)
return fromIndex;
long word = ~words[u] & (WORD_MASK << fromIndex);
while (true) {
if (word != 0)
return (u * BITS_PER_WORD) + Long.numberOfTrailingZeros(word);
if (++u == wordsInUse)
return wordsInUse * BITS_PER_WORD;
word = ~words[u];
}
}
private static int wordIndex(int bitIndex) {
return bitIndex >> ADDRESS_BITS_PER_WORD;
}
这里面代码有一丢丢复杂哈,具体上网上找了一篇有中文注释的源码大家阅读一下(手动狗头)。具体解释就是去除集合中满足某个条件的元素。例:去除集合中所有三岁的狗。
class Dog {
private String name;
private int age;
...
}
List<Dog> list = new ArrayList<>();
list.removeIf(dog -> dog.age == 3);
由于ArrayList
集合允许重复所以一般remove()
用的比较少,因为remove()
只能去除第一次出现的元素。一般我们都用removeAll()
或者removeIf()
,因为这两个方法可以去除集合中所有满足条件的元素。
4.5、indexOf方法
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 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;
}
这两个方法是找出对象在List
中的位置,第一个为对象在List
中从前往后第一次出现的位置,没有返回-1;第二个为对象在List
中从后往前第一次出现的位置,没有返回-1。
4.6、toArray方法
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
该方法是将List
转换为数组,第一种无参的是直接转换为Object
类型的;第二种则是转换为给定类型T
的数组,其中T
必须是原List
中元素对象E
发父类或者E
本身,否则抛出ArrayStoreException
异常。如果给定的T[]
长度小于size
则返回原List
长度的数组,大于的话将多出来的部分用null
填充。
4.7、iterator方法
这部分具体参考https://www.cnblogs.com/mrpour/p/10764088.html
4.8、其他方法
public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = (size == 0)
? EMPTY_ELEMENTDATA
: Arrays.copyOf(elementData, size);
}
}
将List
的容量缩小到实际长度的大小。
public int size() {
return size;
}
返回List
的实际长度。
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
判断List
是否包含某个对象,即是否存在一个对象element
满足element.equals(o)
为true。
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
清除List
中所有的元素,使其为空值。
public List<E> subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, 0, fromIndex, toIndex);
}
static void subListRangeCheck(int fromIndex, int toIndex, int size) {
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
if (toIndex > size)
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex + ")");
}
从fromIndex
截取到toIndex
,返回截取出来的List
。
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++;
}
对于List
中的元素进行排序,如果在排序过程中modCount
发生变化,即List
发生改变,则抛出ConcurrentModificationException
异常。