前言
我们在面试的时候,经常遇到面试官问的Java基础问题,今天记录一个经常被问到的问题————ArrayList和LinkedList的区别。
二者区别
我们先说结论,然后再从源码角度去看具体实现。
前者底层是动态数组实现;后者底层是链表实现。
随机访问数据:前者快,后者慢。
插入和删除(非末尾)数据:前者慢,后者快。
前者需要扩容;后者不需要扩容。
源码分析
分析的源码是基于jdk1.8的
ArrayList
从经常使用的方法(比如构造方法、add、get、remove)入手开始分析。
构造方法如下:
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
* will be expanded to DEFAULT_CAPACITY when the first element is added.
*
* 用elementData数组来存储ArrayList中的数据,ArrayList的长度就是数组长度,当第一个元素被add进来之后,数组的长度就被扩展为DEFAULT_CAPACITY=10
*/
transient Object[] elementData; // non-private to simplify nested class access
/**
* Shared empty array instance used for empty instances.
* 如果创建的是空list、则使用EMPTY_ELEMENTDATA数组、所有的空list指向此数组、避免代码中过多的空数组造成资源浪费
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
/**
* Shared empty array instance used for default sized empty instances. We
* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
* first element is added.
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
//空list的时候。使用EMPTY_ELEMENTDATA作为存储数据的数组、避免资源浪费
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
三个构造方法:
第一个构造方法有一个参数,用来设置初始长度的,如果明确知道list的长度,则使用此方法来构造,若传进来的参数为0,则直接使用EMPTY_ELEMENTDATA数组,这样做的好处,是避免程序中,有过多的空list的情况下,造成资源浪费,jdk1.8以前的版本是,如果传进来的参数为0,则直接new一个空数组,这样会造成资源的浪费;
第二个方法构造方法,也是我们经常用的构造方法,没有参数的构造方法会使用DEFAULTCAPACITY_EMPTY_ELEMENTDATA数组作为数据容器;
第三个构造方法是有一个Collection类型参数的构造方法,若参数中的数据不为空,会把参数中的数据,存进elementData数组中;
下面看看如何add数据的
/**
* 在list末尾增加一个数据,
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
//扩容
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
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);
}
/**
* 真正的扩容的方法
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
private void grow(int minCapacity) {
// overflow-conscious code
//当前的容量
int oldCapacity = elementData.length;
//新容量是当前容量的1.5倍
int newCapacity = oldCapacity + (oldCapacity >> 1);
//如果新容量比所需要的容量小,就使用所需要的容量扩容
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
//如果新容量大于MAX_ARRAY_SIZE=Integer.MAX_VALUE - 8;新容量会根据所需容量minCapacity与MAX_ARRAY_SIZE大小,分别使用Integer.MAX_VALUE或者Integer.MAX_VALUE - 8
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
//将elementData扩容并将elementData中的内容复制到扩容后的数组elementData
//这里会调用到native方法
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方法中,将要添加的元素添加到elementData的末尾即可。
然后简单看一下add(int index, E element)方法:
/**
* Inserts the specified element at the specified position in this
* list. Shifts the element currently at that position (if any) and
* any subsequent elements to the right (adds one to their indices).
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
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++;
}
跟add(E element)方法基本一样,唯一不同的是多了一句System.arraycopy(elementData, index, elementData, index + 1, size - index);这句代码就是将源数组中从位置index之后的数据整体向后移动一位,以便空出index位,为插入element做准备、然后将element插入到index位置。
get(int index)方法很简单不贴代码了。
看下删除数据的吧remove(int index)和remove(Object o)两个方法
/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left (subtracts one from their
* indices).
*
* @param index the index of the element to be removed
* @return the element that was removed from the list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
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;
}
这个很简单,看注释就好。
/**
* Removes the first occurrence of the specified element from this list,
* if it is present. If the list does not contain the element, it is
* unchanged. More formally, removes the element with the lowest index
* <tt>i</tt> such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
* (if such an element exists). Returns <tt>true</tt> if this list
* contained the specified element (or equivalently, if this list
* changed as a result of the call).
*
* @param o element to be removed from this list, if present
* @return <tt>true</tt> if this list contained the specified element
*/
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;
}
很简单,从0开始遍历数组,找到第一个==被删除元素的位置,调用fastRemove(index)方法
/*
* Private remove method that skips bounds checking and does not
* return the value removed.
*/
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
}
what?这不就是remove(int index)方法吗?哈哈。逻辑一样。到此删除的逻辑讲清楚了
ArrayList的源码就分析到这里,其实就是对数组操作的封装,其他方法,感兴趣可以自行查看源码。
LinkedList
我们也从构造方法,add、get和remove入手,先看构造方法:
/**
* Constructs an empty list.
*/
public LinkedList() {
}
/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
常用的是第一个,构造一个空的list;对于第二个会通过参数来构造一个list,其中调用到了addAll(Collection<? extends E> c)。先不看他,因为我们还不知道LinkedList是用什么结构存储数据的呢,看下LinkedList都有哪些全局变量
transient int size = 0;
/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
* 指向第一个节点
*/
transient Node<E> first;
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
* 指向最后一个节点
*/
transient Node<E> last;
三个,第一个不用说,后两个是一个Node类型。这是什么类型呢。这是LinkedList的一个内部类:
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;
}
}
这不是一个链表结构的元素的结构嘛!那LinkedList就是用链表存储数据的。
看下addAll(Collection<? extends E> c)方法最终调用的方法
/**
* Inserts all of the elements in the specified collection into this
* list, starting at the specified position. Shifts the element
* currently at that position (if any) and any subsequent elements to
* the right (increases their indices). The new elements will appear
* in the list in the order that they are returned by the
* specified collection's iterator.
*
* @param index index at which to insert the first element
* from the specified collection
* @param c collection containing elements to be added to this list
* @return {@code true} if this list changed as a result of the call
* @throws IndexOutOfBoundsException {@inheritDoc}
* @throws NullPointerException if the specified collection is null
*/
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;
Node<E> pred, succ;//pred代表要插入的Collection的节点的前驱
//在末尾添加的时候,所以pred指向最后一个节点
if (index == size) {
succ = null;
pred = last;
} else {//不是在末尾添加的逻辑
//先获取要插入位置上节点
succ = node(index);
//然后前驱指向被插入链表上插入位置节点的前驱
pred = succ.prev;
}
//遍历要插入的Collection集合
for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o;
//对每一个元素生成一个Node 、pred为前驱
Node<E> newNode = new Node<>(pred, e, null);
if (pred == null)//如果没有前驱,代表是第一个节点firstNode
first = newNode;
else//否则把前一个前驱的后继指向新生成的节点
pred.next = newNode;
//然后将新生成的节点更改为下一个节点的前驱
pred = newNode;
}
if (succ == null) {//将Collection添加到末尾的情况下,last指向最后一个节点
last = pred;
} else {
//将Collection中的最后一个节点的next指向原list中index位置上的节点succ
pred.next = succ;
//将原list中index位置上的节点succ的前驱指向Collection中的最后一个节点
succ.prev = pred;
}
//更新size
size += numNew;
modCount++;
return true;
}
这里的逻辑有点绕,看不懂可以在纸上画一画,其实就是链表插入操作,分为在末尾插入和中间插入两种情况,并保存全局变量first和last。
构造方法看完了,接下来看下单个元素的添加add(E e)方法吧
/**
* Appends the specified element to the end of this list.
* 在list的末尾添加一个新的元素
* <p>This method is equivalent to {@link #addLast}.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
linkLast(e);
return true;
}
/**
* Links e as last element.
*/
void linkLast(E e) {
//获取当前list的最后一个元素
final Node<E> l = last;
//生成一个新的元素,并把新元素的前驱指向last,后继指为null
final Node<E> newNode = new Node<>(l, e, null);
//更新last元素为新生成的这个node
last = newNode;
//last==null表明当前是空表,所以first需要指向新生成的newNode
if (l == null)
first = newNode;
//非空表 原list的last节点的next需要指向新生成的节点newNode
else
l.next = newNode;
//更新size
size++;
modCount++;
}
在看下add(int index , E element)方法,在中间位置插入输入
/**
* Inserts the specified element at the specified position in this list.
* Shifts the element currently at that position (if any) and any
* subsequent elements to the right (adds one to their indices).
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
checkPositionIndex(index);
//判断是否是在末尾插入,若是调用linkLast方法,否则调用linkBefore方法
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
/**
* Inserts element e before non-null Node succ.
* succ 是要插入位置的节点
* e 是要插入的节点
*/
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
//定义一个节点pred保存要插入位置的节点的前驱节点
final Node<E> pred = succ.prev;
//新生成一个节点前驱为上一步保存的pred,后继为succ节点
final Node<E> newNode = new Node<>(pred, e, succ);
//将succ的前驱指向新生成的节点
succ.prev = newNode;
//判断插入位置是否是第一个位置,若是则满足pred==null 将first指向newNode
if (pred == null)
first = newNode;
else//否则更新pred节点的后继为新生成的节点
pred.next = newNode;
//更新size
size++;
modCount++;
add方法分析完毕,下面看一下get方法
/**
* Returns the element at the specified position in this list.
*
* @param index index of the element to return
* @return the element at the specified position in this list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
/**
* Returns the (non-null) Node at the specified element index.
*/
Node<E> node(int index) {
// assert isElementIndex(index);
//判断index是否小于size的1/2
if (index < (size >> 1)) {
Node<E> x = first;
//从list头开始遍历查找数据找到第index位置上的元素
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {//否则从list尾开始遍历,找到index位置上的元素
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
get方法需要从list的头或者尾开始遍历查找,所以比起数组存储的ArrayList来说,会比较耗时。
下面来看看remove(int index)方法
/**
* Removes the element at the specified position in this list. Shifts any
* subsequent elements to the left (subtracts one from their indices).
* Returns the element that was removed from the list.
*
* @param index the index of the element to be removed
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E remove(int index) {
checkElementIndex(index);
//获取要删除位置的元素并调用unlink方法
return unlink(node(index));
}
/**
* Unlinks non-null node x.
*/
E unlink(Node<E> x) {
// assert x != null;
//获取element及其前驱和后继
final E element = x.item;
final Node<E> next = x.next;
final Node<E> prev = x.prev;
//前驱为null表明是第一个元素,更新first为此node的后继
if (prev == null) {
first = next;
} else {
//否则不是第一个元素,将前驱的后继指向当前node的后继
prev.next = next;
//断开当前node的前驱
x.prev = null;
}
//后继为null表明是最后一个元素,更新last指此向node的前驱
if (next == null) {
last = prev;
} else {
//否则不是最后一个元素,将后继的前驱指向荡秋千node的前驱
next.prev = prev;
//断开当前node的后继
x.next = null;
}
//产出node的item并更新size
x.item = null;
size--;
modCount++;
return element;
}
查看源码remove(Object o)
/**
* Removes the first occurrence of the specified element from this list,
* if it is present. If this list does not contain the element, it is
* unchanged. More formally, removes the element with the lowest index
* {@code i} such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
* (if such an element exists). Returns {@code true} if this list
* contained the specified element (or equivalently, if this list
* changed as a result of the call).
*
* @param o element to be removed from this list, if present
* @return {@code true} if this list contained the specified element
*/
public boolean remove(Object 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;
}
根据要删除的数据是否为null、分别遍历查找第一个出现的o,并调用unlink方法。remove方法也讲完了。
以上就是经常用到的方法的源码分析
总结
最后来个总结吧。看前面列出的区别点,之所以出现“随机访问数据:前者快,后者慢。”的情况,就是因为后者的随机访问,需要从头遍历链表,因为他们存储的位置是不连续的。而对于“插入和删除(非末尾)数据:前者慢,后者快。”这个问题,由于数组存储位置是连续的,从中间删了的数据,空出来的位置,需要由后面的元素补上空位,需要移动元素,而后者不需要。所以会有此差异。
|
|