面试题
- ArrayList的大小是如何自动增加的
- 什么情况下你会使用ArrayList?什么时候你会选择LinkedList?
- 如何复制某个ArrayList到另一个ArrayList中去
- ArrayList插入删除一定慢么?
- ArrayList的遍历和LinkedList遍历性能比较如何?
- ArrayList是线程安全的么?
- ArrayList如何remove
不想看源码解析的同学,可以直接去最下方查看答案
源码解析
List是最简单的线性数据结构,Java作为面向对象的语言,在最上层提供了List接口,然后通过AbstractList实现了List接口。
ArrayList和LinkedList是Java中最常用的List实现类。
ArrayList底层是由数组实现的,相当于动态数组。提供了很好的 顺序/随机 遍历能力,但是在一般情况下,插入和删除操作,需要移动一些元素,要花费一些时间。
LinkedList底层使用双向链表的实现方式,对于插入和删除操作,能保证O(n)的时间复杂度。但是不支持随机遍历的能力,在平均情况下,查找一个元素的时间复杂度也是O(n)。LinkedList还实现了Deque接口,可以用来实现双端队列。
下面我们开始分析源码
ArrayList
底层数据结构
/**
* 默认初始化的数组大小
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* 所有ArrayList实例共享的空list实例
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
/**
* 使用DEFAULTCAPACITY_EMPTY_ELEMENTDATA与EMPTY_ELEMENTDATA区分,标识
* elementData数组是通过默认构造方法创建的空数组,并且还没有向其中添加
* 元素
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
* 底层用来真实存储数据的数组,在调用ArrayList默认构造方法时,该数组会被
* 赋值为DEFAULTCAPACITY_EMPTY_ELEMENTDATA。直到第一次向数组中添加元素时,
* 会被扩容为DEFAULT_CAPACITY
*/
transient Object[] elementData;
/**
* 数组中元素的数量
*/
private int size;
/**
* 可以分配的最大数组大小。某些VM在数组中保留一些header words。
* 尝试分配更大的数组可能会导致OutOfMemoryError
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
构造方法
/**
* 通过给定的初始容量创建一个空list
*/
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
// 如果指定的容量大于0,就新建一个数组
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
// 如果指定的容量等于0,那么就是一个空数组
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: " +
initialCapacity);
}
}
/**
* 不指定初始容量,创建一个容量为10的空数组
*/
public ArrayList() {
// 使用DEFAULTCAPACITY_EMPTY_ELEMENTDATA标识elementData数组
// 是通过默认构造方法创建的,在第一向ArrayList添加元素时,会进行
// 扩容,而扩充的容量为DEFAULT_CAPACITY(10)
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
/**
* 创建一个ArrayList,包含给定集合c中的所有元素,顺序即为c迭代器遍历的顺序。
*
* @throws NullPointerException 如果c为null,抛出NPE
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// 如果给定集合c的size不为0
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
// c.toArray()并不一定返回Object[]类型,如果返回的不是
// Object[]类型,就调用Arrays的复制方法变为Object类型
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// 如果给定集合c的size为0,那么就构造一个空数组
this.elementData = EMPTY_ELEMENTDATA;
}
}
动态扩容
/**
* 修剪elementData多余的槽,使ArrayList的capacity修剪为当前的size
*/
public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = (size == 0)
? EMPTY_ELEMENTDATA
: Arrays.copyOf(elementData, size);
}
}
/**
* 增加ArrayList的capacity,确保elementData的容量最少能支持
* minCapacity
*
* @param minCapacity 需要的最小容量
*/
public void ensureCapacity(int minCapacity) {
// 1. 如果ArrayList不是通过默认构造方式构造的,或者
// ArrayList中已经添加过元素了,则minExpand为0,
// 2. 如果ArrayList是通过默认构造方式构造的,且从未
// 添加过任何元素,那么minExpand就为默认的初始化
// 容量DEFAULT_CAPACITY(10)
int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
? 0 : DEFAULT_CAPACITY;
if (minCapacity > minExpand) {
// 如果需要扩容
ensureExplicitCapacity(minCapacity);
}
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
/**
* @param minCapacity 需要的最小容量
*/
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
// newCapacity = 1.5 * oldCapacity
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// 对elementData进行扩容,然后将elementData原有的内容,
// 复制到扩容后的数组中
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;
}
插入/替换
/**
* 添加元素e到list尾部
*/
public boolean add(E e) {
// 检查是否需要扩容,并将modCount + 1
ensureCapacityInternal(size + 1);
elementData[size++] = e;
return true;
}
/**
* 在指定index插入元素element,并将原先在index位置及右方元素向右移动一位
*/
public void add(int index, E element) {
// 检查index是否有效
rangeCheckForAdd(index);
// 检查是否需要扩容,并将modCount + 1
ensureCapacityInternal(size + 1);
// 把index及index右面的元素全部向右移动一位
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
/**
* 将给定的集合c中的所有元素按照c迭代器的顺序添加到list的末尾。
* <p>
* 在遍历集合c的过程中,如果对c进行了修改,那么会产生未定义的现象。
*/
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
// 扩容,并修改modCount
ensureCapacityInternal(size + numNew);
// 将数组a复制到数组elementData尾部
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
/**
* 将给定的集合c中的所有元素按照c迭代器的顺序添加到list的index位置。
* 并把index及index之后的元素的位置向后移动n个位置,n为集合c的大小。
*/
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
Object[] a = c.toArray();
int numNew = a.length;
// 扩容并修改modCount
ensureCapacityInternal(size + numNew);
int numMoved = size - index;
if (numMoved > 0)
// 如果index小于size,即为在之前的元素中间插入,所以要把index及之后的
// 元素向右移动numNew位
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);
// 将数组a的元素,复制到elementData数组中
System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
/**
* 用element替换下标为index的元素,并返回之前的元素
*/
public E set(int index, E element) {
// 检查index是否超过限制
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
删除
/**
* 删除指定下标的元素,并将该下标右边的元素全部向左移动一位
*/
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
// 将index+1及之后的元素全部向左移动一位
System.arraycopy(elementData, index + 1, elementData, index,
numMoved);
elementData[--size] = null;
return oldValue;
}
/**
* 如果list中包含一个或多个元素o,那么删除第一个o(下标最小的),并将第一个o对应
* 下标右边的元素全部向左移动一位。
* <p>
* 如果list中不包含元素o,那么不做任何改变
*/
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;
}
/**
* 快速删除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
}
/**
* 清空elementData中的所有元素的引用
*/
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
// 帮助GC
elementData[i] = null;
size = 0;
}
/**
* 移除下标在 [fromIndex, toIndex) 之间的元素,并将toIndex和之后
* 的元素全部向左移动至fromIndex的位置
*/
protected void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex;
// 将toIndex和之后的元素向左移动至fromIndex的位置
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// 计算新的数组的大小
int newSize = size - (toIndex - fromIndex);
// 将多余的元素引用置为null,帮助GC
for (int i = newSize; i < size; i++) {
elementData[i] = null;
}
size = newSize;
}
/**
* 从list中删除指定集合c中包含的所有元素。
*/
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, false);
}
/**
* 保留在list中且在指定集合c中包含的所有元素
*/
public boolean retainAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, true);
}
/**
* @param complement false-remove,true-retain
*/
private boolean batchRemove(Collection<?> c, boolean complement) {
final Object[] elementData = this.elementData;
int r = 0, w = 0;
boolean modified = false;
try {
// 双指针遍历list
for (; r < size; r++)
// c.contains(elementData[r])判断给定集合c中是否存
// 在r指针对应的元素。
// 如果complement为false,代表remove集合c中的元素。
// 如果complement为true,代表retain集合c中的元素。
if (c.contains(elementData[r]) == complement)
elementData[w++] = elementData[r];
} finally {
// 如果c.contains抛出异常,仍能保证与AbstractCollection相同的兼容性
if (r != size) {
// 如果r指针没有遍历完数组,就把r指针未遍历的元素,复制到r指针
// 之后,因为r指针-w指针之间的元素应该被移除
System.arraycopy(elementData, r, elementData, w, size - r);
w += size - r;
}
if (w != size) {
// 清空不用的元素引用,帮助GC
for (int i = w; i < size; i++)
elementData[i] = null;
modCount += size - w;
size = w;
modified = true;
}
}
return modified;
}
查找
/**
* 返回list中第一次出现给定元素o的下标,如果不存在元素o
* 则返回-1
*/
public int indexOf(Object o) {
// 将o为null和非null的情况分开做处理
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;
}
/**
* 返回list中最后一次出现给定元素o的下标,如果不存在元素o
* 则返回-1
*/
public int lastIndexOf(Object o) {
// 与indexOf实现方式相同,只是把遍历的方向
// 改为从后向前遍历
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;
}
/**
* 返回下标为index的元素
*/
@SuppressWarnings("unchecked")
E elementData(int index) {
return (E) elementData[index];
}
/**
* 返回下标为index的元素
*/
public E get(int index) {
// 检查index是否超过限制
rangeCheck(index);
return elementData(index);
}
@Override
public void forEach(Consumer<? super E> action) {
Objects.requireNonNull(action);
final int expectedModCount = modCount;
@SuppressWarnings("unchecked") final E[] elementData = (E[]) this.elementData;
final int size = this.size;
// forEach的内部实现其实就是遍历内部elementData数组,
// 然后对每个元素进行action.accept操作。
// 遍历过程中要比较modCount是否发生变化,如果发生了变化,
// 会抛出ConcurrentModificationException,快速失败
for (int i = 0; modCount == expectedModCount && i < size; i++) {
action.accept(elementData[i]);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
迭代器
private class Itr implements Iterator<E> {
int cursor; // 下一个要访问的元素的下标
int lastRet = -1; // 上一次访问的元素的下标,如果没有则为-1
int expectedModCount = modCount;
// 是否包含下一个元素
public boolean hasNext() {
return cursor != size;
}
@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后移
cursor = i + 1;
// 返回下标为i的元素,并将lastRet置为i
return (E) elementData[lastRet = i];
}
public void remove() {
// 如果上一个访问的元素不存在,则抛出异常
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
// 删除上一个访问的元素
ArrayList.this.remove(lastRet);
// 重置下标
cursor = lastRet;
// 因为上一个访问的元素已经删除了,所以不存在了
// 要把lastRet置为-1
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
@Override
@SuppressWarnings("unchecked")
public void forEachRemaining(Consumer<? super E> consumer) {
Objects.requireNonNull(consumer);
final int size = ArrayList.this.size;
int i = cursor;
if (i >= size) {
return;
}
final Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length) {
throw new ConcurrentModificationException();
}
while (i != size && modCount == expectedModCount) {
consumer.accept((E) elementData[i++]);
}
// update once at end of iteration to reduce heap write traffic
cursor = i;
lastRet = i - 1;
checkForComodification();
}
// 防止并发冲突
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
/**
* 在Iterator的基础上,支持了双向遍历
*/
private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
super();
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
/**
* 与Iterator的next方法一样,只不过改成了向前遍历
*/
@SuppressWarnings("unchecked")
public E previous() {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i;
return (E) elementData[lastRet = i];
}
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.set(lastRet, e);
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(E e) {
checkForComodification();
try {
int i = cursor;
ArrayList.this.add(i, e);
cursor = i + 1;
// 向list添加元素后,上一次访问的元素就发生了变化,
// 所以要将lastRet置为-1
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
其他
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
private static final long serialVersionUID = 8683452581122892189L;
/**
* 返回ArrayList的浅拷贝
*/
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);
}
}
/**
* 以数组的形式返回list中的所有元素
*/
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
/**
* 以数组的形式返回list中的所有元素, 数组的类型为T
*/
@SuppressWarnings("unchecked")
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;
}
/**
* 检查index是否超出限制,需要检查index的原因是当list动态扩容时,会分配出
* 未使用的空间,访问时并不会报错。而size记录了当前真实使用的空间,所以
* 需要将index与size比较。
* <p>
* 不检查index为负的情况,因为当index为负时,访问数组会抛出ArrayIndexOutOfBoundsException
*/
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
/**
* add and addAll 时检查是否index是否有效
*/
private void rangeCheckForAdd(int index) {
// 与rangeCheck版本不同,这里index是可以等于size的,因为size的值
// 就是下一个要插入的下标值,所以index==size就相当于在list尾插入
//
// 不知道为什么这里还判断了index < 0 的情况
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: " + index + ", Size: " + size;
}
}
LinkedList
底层数据结构
transient int size = 0;
/**
* 指向链表第一个元素
*/
transient Node<E> first;
/**
* 指向链表最后一个元素
*/
transient Node<E> last;
/**
* 链表结点的数据结构
*/
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
构造方法
/**
* 构造一个空list
*/
public LinkedList() {
}
/**
* 创建一个LinkedList,包含给定集合c中的所有元素,顺序即为c迭代器遍历的顺序。
*
* @throws NullPointerException 如果c为null,抛出NPE
*/
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
插入/替换
/**
* 添加元素e到链表头部
*/
private void linkFirst(E e) {
final Node<E> f = first;
// 创建一个新的node,node的前驱为null,后继为first,值为e
final Node<E> newNode = new Node<>(null, e, f);
// 把首结点指向新创建的node
first = newNode;
if (f == null)
// first为null,说明第一次添加元素,
// last结点也为 newNode
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}
/**
* 添加元素e到链表尾部
*/
void linkLast(E e) {
final Node<E> l = last;
// 创建一个新的node,node的前驱为last,后继为null,值为e
final Node<E> newNode = new Node<>(l, e, null);
// 把尾结点指向新创建的node
last = newNode;
if (l == null)
// last为null,说明第一次添加元素,
// first结点也为 newNode
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
/**
* 在succ前添加元素e
*/
void linkBefore(E e, Node<E> succ) {
// pred为succ的前驱
final Node<E> pred = succ.prev;
// 创建一个新node,前驱为pred,后继为succ,值为e
final Node<E> newNode = new Node<>(pred, e, succ);
// 修改succ的前驱为newNode
succ.prev = newNode;
if (pred == null)
// 如果succ之前的前驱为null,说明succ之前是
// 头结点,天剑newNode后,新的头结点即为newNode
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}
/**
* 添加元素到头结点
*/
public void addFirst(E e) {
linkFirst(e);
}
/**
* 添加元素到尾结点
*/
public void addLast(E e) {
linkLast(e);
}
/**
* 在list尾部添加新元素,与addLast方法效果相同
*/
public boolean add(E e) {
linkLast(e);
return true;
}
/**
* 将给定的集合c中的所有元素按照c迭代器的顺序添加到list的末尾。
* <p>
* 在遍历集合c的过程中,如果对c进行了修改,那么会产生未定义的现象。
*/
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
/**
* 将给定的集合c中的所有元素按照c迭代器的顺序添加到list的index位置。
* 并把index及index之后的元素的位置向后移动
*/
public boolean addAll(int index, Collection<? extends E> c) {
// 检查index是否有效
checkPositionIndex(index);
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;
Node<E> pred, succ;
if (index == size) {
// 如果index==size,就是向list结尾添加后继为null
// 前驱就是尾结点
succ = null;
pred = last;
} else {
// 如果index < size,那么就是在index元素之前添加
// 元素,所以后继就位index对应元素,前驱就是index
// 结点的前驱
succ = node(index);
pred = succ.prev;
}
// 依次将集合c中的元素插入到list中
for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o;
Node<E> newNode = new Node<>(pred, e, null);
if (pred == null)
first = newNode;
else
pred.next = newNode;
pred = newNode;
}
// 处理后继结点
if (succ == null) {
last = pred;
} else {
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
/**
* 在指定index插入元素element,并将原先在index位置及右方元素向右移动一位
*/
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
/**
* 向list尾部添加元素
*/
public boolean offer(E e) {
return add(e);
}
// Deque operations
/**
* 向list头部添加元素
*/
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
/**
* 向list尾部添加元素
*/
public boolean offerLast(E e) {
addLast(e);
return true;
}
/**
* 等于addFirst
*/
public void push(E e) {
addFirst(e);
}
/**
* 替换index位置的元素的值,并返回原值
*/
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
删除
/**
* 删除头结点,并返回头结点对应的元素值
*/
private E unlinkFirst(Node<E> f) {
// assert f == first && f != null;
final E element = f.item;
final Node<E> next = f.next;
f.item = null;
f.next = null; // help GC
// 用first.next代替first成为头结点
first = next;
if (next == null)
last = null;
else
next.prev = null;
size--;
modCount++;
return element;
}
/**
* 删除尾结点,并返回尾结点对应的元素值
*/
private E unlinkLast(Node<E> l) {
// assert l == last && l != null;
final E element = l.item;
final Node<E> prev = l.prev;
l.item = null;
l.prev = null; // help GC
last = prev;
if (prev == null)
first = null;
else
prev.next = null;
size--;
modCount++;
return element;
}
/**
* 删除结点x,并返回x对应元素
*/
E unlink(Node<E> x) {
// assert x != null;
final E element = x.item;
// x的后继结点
final Node<E> next = x.next;
// x的前驱结点
final Node<E> prev = x.prev;
if (prev == null) {
// 如果x的前驱为null,说明x之前为头结点
// 要删除x结点,就把头结点置为x.next
first = next;
} else {
prev.next = next;
x.prev = null;
}
if (next == null) {
// 如果x的后继为null,说明x之前为尾结点
// 要删除x结点,就把尾结点置为x.prev
last = prev;
} else {
next.prev = prev;
x.next = null;
}
x.item = null;
size--;
modCount++;
return element;
}
/**
* 删除list头结点
*/
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
/**
* 删除list尾结点
*/
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
/**
* 如果list中包含一个或多个元素o,那么删除第一个o
* <p>
* 如果list中不包含元素o,那么不做任何改变
*/
public boolean remove(Object o) {
// 从头结点遍历list,找到第一个与o相等的节点,删除该节点
// 并返回。如果不存在,则不做任何改变
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}
public void clear() {
// 把结点之间的next, prev置为null并不是必要的,
// 但是可以帮助GC
for (Node<E> x = first; x != null; ) {
Node<E> next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
first = last = null;
size = 0;
modCount++;
}
/**
* 删除指定下标的元素,并将该下标右边的元素全部向左移动一位
*/
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
/**
* 删除并返回list第一个元素,如果list为空,则抛出NoSuchElementException
*/
public E remove() {
return removeFirst();
}
/**
* 等于remove
*/
public boolean removeFirstOccurrence(Object o) {
return remove(o);
}
/**
* 与remove相似,但是删除list中最后一个o,而不是第一个
*/
public boolean removeLastOccurrence(Object o) {
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}
/**
* 等于removeFirst
*/
public E pop() {
return removeFirst();
}
查找
/**
* 返回list第一个元素(头结点)
*/
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
/**
* 返回list最后一个元素(尾结点)
*/
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
/**
* 返回index位置对元素,因为底层使用链表实现
* 所以即使是使用index查询,还是需要遍历链表
* 无法实现随机遍历
*/
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
/**
* 返回下标index对应的元素
*/
Node<E> node(int index) {
// assert isElementIndex(index);
// 如果index在list的前半部分,则从前向后遍历,
// 否则在后半部分,则从后向前遍历
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
/**
* 返回list中第一次出现给定元素o的下标,如果不存在元素o
* 则返回-1
*/
public int indexOf(Object o) {
int index = 0;
// o为null和非null的情况下分别处理,防止NPE出现
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null)
return index;
index++;
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item))
return index;
index++;
}
}
return -1;
}
/**
* 与indexOf功能相近。但是返回list中最后一次出现给定元素o
* 的下标,所以从后向前遍历。如果不存在元素e则返回-1
*/
public int lastIndexOf(Object o) {
int index = size;
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (x.item == null)
return index;
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (o.equals(x.item))
return index;
}
}
return -1;
}
/**
* 返回list第一个元素,但不删除,如果list为空则返回null
*/
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
/**
* 返回list第一个元素,但不删除,如果list为空则抛出NoSuchElementException
*/
public E element() {
return getFirst();
}
/**
* 删除并返回list第一个元素,如果list为空,则返回null
*/
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
/**
* 等于peek方法
*/
public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
/**
* 返回list最后一个元素,但不删除,如果list为空则返回null
*/
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}
/**
* 等于poll方法
*/
public E pollFirst() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
/**
* 删除并返回list最后一个元素,如果list为空,则返回null
*/
public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
}
迭代器
/**
* ListIterator提供了双向遍历的能力
*/
public ListIterator<E> listIterator(int index) {
checkPositionIndex(index);
return new ListItr(index);
}
private class ListItr implements ListIterator<E> {
// 上一个遍历过的结点
private Node<E> lastReturned;
// 下一个需要遍历的结点
private Node<E> next;
// 下一个需要遍历的结点的坐标
private int nextIndex;
private int expectedModCount = modCount;
ListItr(int index) {
// assert isPositionIndex(index);
next = (index == size) ? null : node(index);
nextIndex = index;
}
public boolean hasNext() {
return nextIndex < size;
}
public E next() {
checkForComodification();
if (!hasNext())
throw new NoSuchElementException();
lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.item;
}
public boolean hasPrevious() {
return nextIndex > 0;
}
public E previous() {
checkForComodification();
if (!hasPrevious())
throw new NoSuchElementException();
lastReturned = next = (next == null) ? last : next.prev;
nextIndex--;
return lastReturned.item;
}
public int nextIndex() {
return nextIndex;
}
public int previousIndex() {
return nextIndex - 1;
}
public void remove() {
checkForComodification();
if (lastReturned == null)
throw new IllegalStateException();
Node<E> lastNext = lastReturned.next;
// 删除上一个遍历过的结点
unlink(lastReturned);
if (next == lastReturned)
// next==lastReturned代表的向前遍历,next保存的是上一次
// 遍历过的结点。如果不删除lastReturned结点,下次访问的
// 应该是lastReturned.prev结点。现在删除lastReturned后,
// 应该通过lastReturned.next.prev结点进行访问
next = lastNext;
else
nextIndex--;
lastReturned = null;
expectedModCount++;
}
public void set(E e) {
if (lastReturned == null)
throw new IllegalStateException();
checkForComodification();
lastReturned.item = e;
}
public void add(E e) {
checkForComodification();
lastReturned = null;
if (next == null)
linkLast(e);
else
linkBefore(e, next);
nextIndex++;
expectedModCount++;
}
public void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while (modCount == expectedModCount && nextIndex < size) {
action.accept(next.item);
lastReturned = next;
next = next.next;
nextIndex++;
}
checkForComodification();
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
其他
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
/**
* 如果list中包含给定的元素o,返回true
*/
public boolean contains(Object o) {
return indexOf(o) != -1;
}
/**
* list中元素的数量
*/
public int size() {
return size;
}
private boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
private String outOfBoundsMsg(int index) {
return "Index: " + index + ", Size: " + size;
}
private void checkElementIndex(int index) {
if (!isElementIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
@SuppressWarnings("unchecked")
private LinkedList<E> superClone() {
try {
return (LinkedList<E>) super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
/**
* 浅拷贝
*/
public Object clone() {
LinkedList<E> clone = superClone();
// Put clone into "virgin" state
clone.first = clone.last = null;
clone.size = 0;
clone.modCount = 0;
// Initialize clone with our elements
for (Node<E> x = first; x != null; x = x.next)
clone.add(x.item);
return clone;
}
public Object[] toArray() {
Object[] result = new Object[size];
int i = 0;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
return result;
}
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
if (a.length < size)
// 通过反射创建类型为T的数组
a = (T[]) java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
int i = 0;
Object[] result = a;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
if (a.length > size)
a[size] = null;
return a;
}
private static final long serialVersionUID = 876323262645176354L;
}
面试题解答
-
ArrayList的大小是如何自动增加的
当调用ArrayList的add, addAll等方法时,会调用ensureCapacityInternal方法检查底层数组容量是否满足所需容量,如果容量不够大,就调用grow方法将容量扩展至原来的1.5倍(一般情况下)
-
什么情况下你会使用ArrayList?什么时候你会选择LinkedList?
- ArrayList:底层由数组实现,需要扩容
- 优点:
- 因为底层由数组实现,所以可以直接通过下标访问,适合随机访问的情况
- 数组中只保存元素值,不需要额外指针,节省空间
- 缺点:
- 数组是有界限的,当数组容量不够时,需要进行动态扩容。频繁插入的情况下,需要耗费时间复制数组。
- 在数组中间进行插入/删除时,需要移动大量数据。
- 优点:
- LinkedList:底层由指针实现,Node结点需要额外指针,保存链表信息
- 优点:
- 底层由链表实现,相当于无界的结构。插入时无需扩容
- 插入/删除 结点时,只需要修改结点前后的指针就行,不需要复制操作
- 缺点:
- 无法通过下标直接访问,必须通过遍历访问
- 不仅需要保存元素值本身,还需要维护next和prev指针,浪费空间
- 优点:
- ArrayList:底层由数组实现,需要扩容
-
如何复制某个ArrayList到另一个ArrayList中去
public class Node implements Cloneable { public Node() {} public Object clone() { Node node = null; try { node = (Node) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return node; } } public static void main(String[] args) { ArrayList<Node> srcList = new ArrayList<>(); srcList.add(new Node()); srcList.add(new Node()); for (Node node : srcList) { System.out.println("srcList: " + node); } /* --------------------- 浅复制 --------------------- */ System.out.println("浅复制"); // 循环遍历 List<Node> destList1 = new ArrayList<>(); srcList.forEach(src -> destList1.add(src)); System.out.println("循环遍历"); for (Node node : destList1) { System.out.println("descList: " + node); } // 构造方法 List<Node> destList2 = new ArrayList<>(srcList); System.out.println("构造方法"); for (Node node : destList2) { System.out.println("descList: " + node); } // addAll方法 List<Node> destList3 = new ArrayList<>(); destList3.addAll(srcList); System.out.println("addAll方法"); for (Node node : destList3) { System.out.println("descList: " + node); } // clone方法 List<Node> destList4 = (List<Node>) srcList.clone(); System.out.println("clone方法"); for (Node node : destList4) { System.out.println("descList: " + node); } // System.arraycopy Node[] destArray = new Node[srcList.size()]; System.arraycopy(srcList.toArray(), 0, destArray, 0, srcList.size()); System.out.println("System.arraycopy方法"); for (Node node : destArray) { System.out.println("descList: " + node); } /* --------------------- 深复制 --------------------- */ System.out.println("深复制"); // 改造后的clone方法 List<Node> destList5 = (List<Node>) srcList.clone(); System.out.println("改造后的clone方法"); for (Node node : destList5) { System.out.println("descList: " + node.clone()); } }返回结果
srcList: Node@71bc1ae4 srcList: Node@6ed3ef1 ---------- 浅复制 ---------- 循环遍历 descList: Node@71bc1ae4 descList: Node@6ed3ef1 构造方法 descList: Node@71bc1ae4 descList: Node@6ed3ef1 addAll方法 descList: Node@71bc1ae4 descList: Node@6ed3ef1 clone方法 descList: Node@71bc1ae4 descList: Node@6ed3ef1 System.arraycopy方法 descList: Node@71bc1ae4 descList: Node@6ed3ef1 ---------- 深复制 ---------- 改造后的clone方法 descList: Node@17d99928 descList: Node@3834d63f -
ArrayList插入/删除一定慢吗
取决于插入与删除的位置
插入:在插入过程中会将index及之后的元素向后移动,如果插入的位置是数组靠后的位置。那么要移动的元素并不多,通过index直接访问的,操作并不会很慢。
删除:与插入相同,插入过程中会将index及之后的元素向前移动,如果位置靠后,移动的元素也不多。
-
ArrayList的遍历和LinkedList遍历性能比较如何?
先说结论如下,具体测试用例可参照后面代码
- 通过下标遍历的情况下,ArrayList的性能要远优于LinkedList
- 通过for遍历的情况下,两者相同
- 通过Iterator遍历,两者相同
原因:
- 通过下标index遍历时,ArrayList可以直接访问到下标对应元素,时间复杂度为O(1)。而对于每个下标,LinkedList每次要遍历index-1个元素,时间复杂度为O(n)。
- 通过for遍历时,ArrayList相当于通过下标遍历。LinkedList相当于通过Iterator遍历。每个元素的时间复杂度为O(1),整体时间复杂度都是O(n)。
- 通过Iterator遍历时,两者的每个元素的时间复杂度为O(1),整体时间复杂度都是O(n)。
public static void main(String[] args) throws InterruptedException { int size = 100000; List<Integer> array = new ArrayList<>(); List<Integer> linked = new LinkedList<>(); for (int i = 0; i < size; i++) { array.add(i); linked.add(i); } System.out.println("------------------------ index loop -----------------------"); long start = System.currentTimeMillis(); for (int i = 0; i < size; i++) { array.get(i); } System.out.println("arrayList index loop cost " + (System.currentTimeMillis() - start) + " ms"); start = System.currentTimeMillis(); for (int i = 0; i < size; i++) { linked.get(i); } System.out.println("linkedList index loop cost " + (System.currentTimeMillis() - start) + " ms"); System.out.println("------------------------ forEach loop -----------------------"); start = System.currentTimeMillis(); for (Integer data : array) { ; } System.out.println("arrayList for loop cost " + (System.currentTimeMillis() - start) + " ms"); start = System.currentTimeMillis(); for (Integer data : linked) { ; } System.out.println("linkedList forEach loop cost " + (System.currentTimeMillis() - start) + " ms"); System.out.println("------------------------ Iterator loop -----------------------"); start = System.currentTimeMillis(); Iterator<Integer> iterator = array.iterator(); while (iterator.hasNext()) { iterator.next(); } System.out.println("arrayList Iterator loop cost " + (System.currentTimeMillis() - start) + " ms"); start = System.currentTimeMillis(); iterator = linked.iterator(); while (iterator.hasNext()) { iterator.next(); } System.out.println("linkedList Iterator loop cost " + (System.currentTimeMillis() - start) + " ms"); }------------------------ index loop ----------------------- arrayList index loop cost 4 ms linkedList index loop cost 4764 ms ------------------------ for loop ----------------------- arrayList forEach loop cost 4 ms linkedList forEach loop cost 4 ms ------------------------ Iterator loop ----------------------- arrayList Iterator loop cost 4 ms linkedList Iterator loop cost 4 ms -
ArrayList是线程安全的么?
不是。对ArrayList的操作并没有做任何同步或者加锁的行为。可以看到对ArrayList的结构性操作中,都会对modCount值进行修改。这样在操作时,通过比较modCount可以实现fail-fast机制,在并发冲突时,抛出ConcurrentModificationException
-
ArrayList如何remove
public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(1); list.add(2); list.add(2); list.add(3); list.add(3); List<Integer> list2 = new ArrayList<>(list); for (int i = 0; i < list2.size(); i++) { if ((list2.get(i) % 2) == 0) { list2.remove(i); } } System.out.println(list2); // [1, 1, 2, 3, 3] list2 = new ArrayList<>(list); Iterator<Integer> iterator = list2.iterator(); while (iterator.hasNext()) { if ((iterator.next() % 2) == 0) { iterator.remove(); } } System.out.println(list2); // [1, 1, 3, 3] list2 = new ArrayList<>(list); for (Integer data : list2) { // ConcurrentModificationException if ((data % 2) == 0) { list2.remove(data); } } System.out.println(list2); }-
通过遍历下标的方式删除(×):
这种方式是错误的,有可能会遗漏元素。比如数组中的元素为[1, 1, 2, 2, 3, 3],当下标index为2时,对应的元素为第一个[2],满足条件删除后,数组中的元素变为[1, 1, 2, 3, 3],因为[2]被删除后,之后的[2, 3, 3]向左移动。在遍历中,index++ 变为3,对应的元素为[3],所以数组中第二个[2]被遗漏了,没有遍历到。
-
通过迭代器进行迭代(√):
这种方式是删除的正确方式
-
在forEach中进行删除(×):
这种方式是错误的,会抛出ConcurrentModificationException。原因在于ArrayList中的forEach方法:
-
final int expectedModCount = modCount; 在整个遍历之前会先记录modCount。 -
在调用ArrayList的remove方法时,会通过modCount++对modCount值进行修改, 这时modCount与遍历前记录的modCount已经不一致了。 -
for (int i = 0; modCount == expectedModCount && i < size; i++) { action.accept(elementData[i]); } 遍历每个元素时,会检查modCount是否与之前一致。而在remove方法中, 已经进行了修改,所以在删除元素后的下次遍历时会退出循环。 -
if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } 如果modCount与之前不一致了,就抛出ConcurrentModificationException
-
-