java集合系列之ArrayList源码分析

时间:2023-03-09 05:16:35
java集合系列之ArrayList源码分析

java集合系列之ArrayList源码分析(基于jdk1.8)

  ArrayList简介

  ArrayList时List接口的一个非常重要的实现子类,它的底层是通过动态数组实现的,因此它具备查询速度快,增删速度慢的特点。另外数组拥有索引,因此可通过索引直接访问集合中的元素,ArrayList集合中允许存放重复的元素。

  下面将对ArrayList集合中的重要方法的底层实现做一下简单的介绍,如有错误,请指正。

  • ArrayList的成员变量:
  //序列化id
private static final long serialVersionUID = 8683452581122892189L;
//默认初始化容量
private static final int DEFAULT_CAPACITY = 10;/**
* 一个空数组,如果使用带参构造方法创建ArrayList对象,并且传入的参数为0,直接将elementData = EMPTY_ELEMENTDATA。
*/
private static final Object[] EMPTY_ELEMENTDATA = {}; /**
* 另一个空数组,如果使用无参构造方法创建ArrayList对象,直接将elementData = EMPTY_ELEMENTDATA。
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; //实际数据存放的数组
transient Object[] elementData; // non-private to simplify nested class access //数组中包含元素的数量
private int size;

  需要注意的是ArrayList从父类那里继承了一个非常重要的成员变量modCount,用来记录数组被修改的次数(增、删、清空数组该变量都会自增),这个变量在并发修改异常时会再次讲解

protected transient int modCount = 0;
  • ArrayList的构造方法:
//带参构造
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {//传入的参数大于0,创建数组
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {//长度为0,赋值为空数组常量,此时add,初始化容量为1
this.elementData = EMPTY_ELEMENTDATA;
} else {//小于0,抛出非法参数异常,并将该参数输出
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
/**
* 空参构造,此时add元素时,初始化容量变为10
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
/**
* 将一个Collection对象转换成数组,然后将这个数组的引用赋给elementData,前提这个Collection对象中存放的是E或者E的子类
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)这个地方是jdk的一个bug不用考虑
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// 如果elementData为空,则指向空数组EMPTY_ELEMENTDATA
this.elementData = EMPTY_ELEMENTDATA;
}
}
  • ArrayList的添加元素
   /**
* 添加元素(添加到数组最后):
* 1.确保数组已使用长度+1之后能存放下一个数据(ensureCapacityInternal())
* 2.在size位置处添加元素,并将size自增(元素个数+1)
* 3.返回true
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
} /**
* 确保数组的长度能够放得下下一个元素
* 1.如果elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA,即ArrayList对象是通过空参构造创建的,则将最小容量设为10,否则设为还是size+1
* @param minCapacity
*/
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
} /**
* 修改次数自增1
* 判断数组是否需要扩容,条件为所需的最小容量 > 数组的长度*/
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; /**
* 1. 将原数组的长度扩充为原来的1.5倍,如果此时数组的长度超过了MAX_ARRAY_SIZE,则调用hugeCapacity进行判断
* 2. 数组copy
*
*/
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
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);
} /**
* 如果minCapacity < 0,抛出OutOfMemoryError
* 否则返回Integer的最大值
*/
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
/**
* 指定索引处添加元素:
* 1. 检查索引必须 0 <= index <=size
* 2. 确保数组已使用长度+1之后能存放下一个数据
* 3. 将该索引及其之后的元素全部后移一位。
* 4. 将新元素添加到该索引处
* 5. 元素数量自增
*/
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++;
}   private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
/**
* 将集合中的元素全部添加到ArrayList对象中:
* 1.将集合转变成数组
* 2.确保当前数组的长度能够放的下size+所添加数组的长度
* 3.数组copy
* 4.size增加
* 5.返回添加是否成功
*/
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;
}
  • ArrayList删除元素
 /**
* 删除指定索引处的元素,并将该元素返回
* 1. 检查所删除元素的索引是否越界,即保证删除元素的存在,如果不存在抛出IndexOutOfBoundsException
* 2. 修改次数自增
* 3. 如果删除的不是最后一个元素,要将该索引之后的所有元素左移一位
* 4. 将数组最后一个元素设为null,便于垃圾回收,并将数组中的元素数量减1
* 5. 返回所删除的元素(在数组移动之前已经保存在临时变量中)
*/
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;
}   private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
} /**
* 删除包含指定数据的元素:
* 1.如果o==null,则使用==比较,否则用equals比较,如果找到返回true
* 2.如果没有找到返回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;
} /**
* 1. 修改次数自增
* 2. 如果删除的不是最后一个元素,要将该索引之后的所有元素左移一位
* 3. 将数组最后一个元素设为null,便于垃圾回收,并将数组元素数量减1
*/
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
} /**
* 清空集合中的元素:
* 1. 修改次数自增
* 2. 将所有的元素设为null,便于垃圾回收
* 3. size设为0
*/
public void clear() {
modCount++; // clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null; size = 0;
}
  • ArrayList元素迭代的ConcurrentModificationException
/**
* 返回一个迭代器对象new Itr(),该对象为一个内部类
*/
public Iterator<E> iterator() {
return new Itr();
} /**
* An optimized version of AbstractList.Itr
*/
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;//将集合的修改次数赋值给expectedModCount(期望修改次数) public boolean hasNext() {
return cursor != size;
} /**
* 在迭代遍历集合时,调用该集合的next()方法,首先会检查该集合首先调用checkForComodification()检查是否在迭代过程中发生了修改,
* 如果被修改,则会抛出并发修改异常
*/
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
} /**
* 用迭代器的remove()方法之所以不会抛出并发修改异常的原因在于,它在删除元素之后,又重新将modCount赋值给expectedModCount,
* 所以在下次调用next()方法进行checkForComodification时,expectedModCount = modCount仍然成立
*/
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification(); try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;//又重新将modCount赋值给expectedModCount
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
/**
* 如果modCount != expectedModCount,则抛出并发修改异常
* 即实际修改的数量不等于并发修改的数量
*/
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
  • 其他方法
    /**
* 查看集合是否包含对象o,注意参数为Object类型,不是泛型
* 判断元素在数组中第一次出现的索引,并与0比较
*/
public boolean contains(Object o) {
return indexOf(o) >= 0;
} /**
* 判断该元素在数组中第一次出现的索引
* 1. 如果o为null,则遍历数组用==比较
* 2. 如果不为null,则遍历数组用equals比较
* 3.如果都没找到,返回-1
*/
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;
} /**
* 该元素在数组中最后出现的位置
* 实现方式与indexOf(Object o)方法相同,仅仅需要倒序遍历数组即可
*/
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;
}