您的位置:首页 > 编程语言 > Java开发

【JDK源码解析】- ArrayList源码解析,绝对详细

2021-04-15 15:24 148 查看

ArrayList源码解析

简介

ArrayList
Java
集合框架中非常常用的一种数据结构。继承自
AbstractList
,实现了
List
接口。底层基于数组来实现动态容量大小的控制,允许
null
值的存在。同时还实现了
RandomAccess
Cloneable
Serializable
接口,支持快速访问、复制、序列化操作。

了解数组

数组简单来说就是将所有的数据排成一排存放在系统分配的一个内存块上,通过使用特定元素的索引作为数组的下标,可以在常数时间内访问数组元素的这么一个结构;

数组优缺点

优点

  • 简单方便已使用
  • 访问元素快

缺点

  • 大小固定:数组的大小是静态的,在使用前必须确定好数组的大小
  • 分配一个连续空间块:数组初始分配空间时,有时候无法分配能存储整个数组的内存空间(当数组规模太大时);
  • **基于位置的插入操作实现复杂:**如果要在数组中的给定位置插入元素,那么可能就会需要移动存储在数组中的其他元素,这样才能腾出指定的位置来放插入的新元素;而如果在数组的开始位置插入元素,那么这样的移动操作开销就会很大。

ArrayList解析

我们提到数组的特点是大小固定,

ArrayList
的底层是基于数组来实现容量的大小动态变化的,那我们一起来结合源码看看,是如何实现这一功能的。

我们找到

java.util.ArrayList
包查看代码。并通过注释的方式,一起来揭开面纱。

1、成员变量

// 默认的容量大小
private static final int DEFAULT_CAPACITY = 10;
// 空数组对象Object
private static final Object[] EMPTY_ELEMENTDATA = {};
// 有一个空数据对象Object
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
// 默认修饰且不参与序列化的 数组对象,也是实际存储数据的地方
transient Object[] elementData; // non-private to simplify nested class access
// 实际大小容量
private int size;

我们发现有两个一样的空数组对象,为什么要用两个呢?源代码中也进行来解释

We distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when first element is added.

也就是说,这是一个共享的空数组实例,通过与默认的空数组区分开,好处是,添加元素时知道该

elementData
从空的构造函数还是有参构造函数被初始化的。以便确认如何扩容。

AbstractList
父类中还有一个变量

protected transient int modCount = 0;

用来记录对List的操作次数。作用在使用

Iterator
时,防止在迭代过程中集合被修改。

2、构造函数

无参数构造

/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}

默认的无参数构造中直接给

elementData
赋值来一个空的数据。但我们看到注释上说初始容量为10的数组,这好像不太对啊。其实这只是一个延后的操作,当第一次添加数据进去时,容量会扩容到10,好处是避免无用的
ArrayList
的出现。具体的实现我们接着往后看。

指定初始容量构造

public ArrayList(int initialCapacity) {
// 指定的容量大于0,直接new一个指定容量大小的数组
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
// 指定容量等于0。那就赋值空数组。
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
// 无效容量大小
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}

这个还是比较清晰的,根据指定容量初始化一个数组。加入了容量大小的判断操作。

指定Collection集合构造

public ArrayList(Collection<? extends E> c) {
// 首先转成数组
Object[] a = c.toArray();
// 有效大小的数组哈
if ((size = a.length) != 0) {
// 这里做了优化,如果也是一个ArrayList集合直接赋值即可
if (c.getClass() == ArrayList.class) {
elementData = a;
} else {
// 其他的类型就做拷贝啦
elementData = Arrays.copyOf(a, size, Object[].class);
}
} else {
// replace with empty array.
elementData = EMPTY_ELEMENTDATA;
}
}

简单点说其实就是把集合转成数组,然后赋值给

elementData
。可能你看到的版本不一样,主要是在

c.getClass() == ArrayList.class
做了优化。如果也是
ArrayList
的集合,那就不用做数组拷贝了,这个还是比较耗性能的。

主要操作函数解析

下面将主要的增删改操作进行分析

添加元素操作

单元素添加

public boolean add(E e) {
// 我们需要添加 一个元素,则需要判断+1后的容量是否需要扩容了,同时记录modCount
ensureCapacityInternal(size + 1);  // Increments modCount!!
// 接在index后添加元素,并且更新当前集合大小size
// 可以理解成 index =size+1;elementData[index];size++
elementData[size++] = e;
return true;
}

// 提取方法哈,比较计算,确定容量大小
//private static int calculateCapacity(Object[] elementData, int minCapacity) {
//    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
//        return Math.max(DEFAULT_CAPACITY, minCapacity);
//    }
//    return minCapacity;
//}

// 做了简化,与calculateCapacity 一致
private void ensureCapacityInternal(int minCapacity) {
//ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
// 提取calculateCapacity方便可以做一个简化,可能你的版本就是这样
// 判断是否是空数组,如果是空数组,那么minCapacity = 10
// 这样就解答了我们前面提到的问题,在添加第一个元素时才将容量设置成10
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity)
}

// 记录操作次数,然后判断是否需要扩容
private void ensureExplicitCapacity(int minCapacity) {
modCount++;

// overflow-conscious code
// 当添加一个元素后的容量大于当前元素个数,则需要扩容了
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
// 扩容方法。minCapacity 添加一个元素后的容量
private void grow(int minCapacity) {
// overflow-conscious code
// 先记录当前容量,也就是元素的个数
int oldCapacity = elementData.length;
// 不管,先扩容1.5倍
int newCapacity = oldCapacity + (oldCapacity >> 1);
// 如果扩容后的大小小于添加元素后的容量,则没必要了,直接用添加元素后的容量了
// 这里可以看到哈,如果是空构造,添加参数时,newCapacity是等于0的,然后再赋值了10。
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:
// 数组拷贝 底层还是System.arraycopy
elementData = Arrays.copyOf(elementData, newCapacity);
}

指定index添加元素

public void add(int index, E element) {
// 检查index
rangeCheckForAdd(index);
// 是否需要扩容操作,记录modCount
ensureCapacityInternal(size + 1);  // Increments modCount!!
// 进行数组拷贝,给这个添加的元素腾出一个位置index
System.arraycopy(elementData, index, elementData, index + 1,size - index);
// 在这个位置index放置元素
elementData[index] = element;
size++;
}

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

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

我们看到哈,在添加元素的方法中,原则是计算容量,判断是否需要扩容,并记录修改次数。这里有一个非常重要的方法就是数组拷贝。扩容需要拷贝,在指定index中放入元素也需要拷贝哈。下面我们通过画图的方式来解释一下

System.arraycopy
这个函数

System.arraycopy详解

这是

System
类中的一个静态方法,用来实现数组之间的复制,具体的实现我们就不去了解,我们主要来看看他的用法,以及在
ArrayList
是怎么使用的

public static native void arraycopy(Object src,  int  srcPos,
Object dest, int destPos,
int length);

参数说明

  • src:the sourse arr
    源数组
  • srcPos:starting position in the source array.
    源数组的起始位置
  • dest:the destination array.
    目标数组
  • destPos:starting position in the destination data.
    目标数组的起始位置
  • length:the number of array elements to be copied.
    复制的长度

举几个例子

// 给定数组
int[] src = {1,2,3,4,5};
// 给定目标数组
int[] dest = new int[src.length]

// 要求1 将src 数组全部复制到dest中
System.arraycopy(src,0,dest,0,src.length);
// 要求2 将src的前三位数复制到dest中的后三位
System.arraycopy(src,0,dest,2,src.length-2);

数组拷贝图解

grow扩容拷贝

假定当前我们的集合元素已经有10个了,此时还需要添加一个元素。会经历这样的操作。

1、判断需要扩容,新的容量为15的数组。

2、将源数组拷贝到新的数组中,重新复制给

elementData
;

3、在

index=10
的位置添加元素

add index 移动拷贝

在集合中已经有了5个元素了。现在需要在

index=1
的位置插入一个新的元素,可以理解成插队。

1、判断是否需要扩容。这里发现不需要

2、

System.arraycopy(elementData, index, elementData, index + 1,size - index);

以图中为例,我们需要将

index 1、2、3、4
整体往后挪动,就像有人插队一样,插入的位置后面整体后移了一位。
index=0
的位置是不用动的。这里的写法应该是

System.arraycopy(elementData, 1, elementData, 1 + 1,5 - 1);

3、将

index=1
的位置重新赋值,原来i
ndex=1
的位置已经被移到现在的
index=2
的位置了。

详细的流程可以通过代码的方式观察即可理解这个过程。

移除元素操作

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

// 指定index 删除其索引位置的元素并返回
public E remove(int index) {
// 老规矩,检查index的边界
rangeCheck(index);
// 记录操作次数
modCount++;
// 找到这个待删除的元素,主要用户返回
E oldValue = elementData(index);
// 需要移动的数量
int numMoved = size - index - 1;
// 如果需要移动 ,如果只从后面删除的话,例如 size=5 index = 4 ,那么numMoved=0
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) {
// ArrayList元素允许为空的
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
// 比较元素,然后找到其所在的index 交由fastRemove通过index移除。与remove(index)
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}

// 快速删除 和remove(index) 基本一致 ,在数组index的操作是高效,通过index去操作
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[i] = null;
size = 0;
}

这里我们会发现一个问题啊,我们在静态的数组中进行

index
所在数据的删除时,一般是直接对
arr[index] = 0;
直接对索引位置的元素进行
null
赋值。但在
ArrayList
中就不一定是这样了,他一直都是对最后一位元素进行操作
elementData[size—] = null;
我们来画个图看一下

例如我们要对上图中

index=1
的位置元素进行
remove
操作,怎么做呢?

1、找到

index 2、3、4、5
需要移动的元素。

2、将他们整体往前移动一位。这个时候需要删除的元素已经被覆盖了

3、再将最后一个删除。(真正移除的那个元素其实和前面一位一样哦)

整体下来发现和

add(E e,int index)
整个流程好像正好相反,哈哈!

修改元素操作

public E set(int index, E element) {
// index 检查
rangeCheck(index);
// 找到旧元素
E oldValue = elementData(index);
// 替换所在位置的元素
elementData[index] = element;
return oldValue;
}

这个还是比较简单的。可以理解成是一个替换的操作就可以了。

查询操作

// 指定index 返回其所在的元素
public E get(int index) {
// 边界检查
rangeCheck(index);
// 返回,这个简单,索引快速定位
return elementData(index);
}

// 从前往后查询,第一次出现的位置index
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;
}
// 从后往前查询,第一次出现的位置index
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;
}

查询操作就简单了很多哈。基本上都是基于索引来访问的。

到这里我们已经总结了很多常用的方法,在

ArrayList
中还有非常多的方法,例如迭代器
Iterator
suList
操作等等。这里就不过多进行解析了,不过后面会通过专门的篇幅来介绍迭代器
Iterator
和为什么不能在
for
遍历集合时对集合进行
remove
操作,有时还会抛出异常
ConcurrentModificationException

if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}

这里有一个我们非常熟悉的变量

modCount
。详细的后面在来解析把。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: