JDK版本: 1.8
IDEA版本:2020.01
文章目录
继承图
跟ArrayList相比,有了点新东西:与ArrayList相比,没有实现RandomAccess
接口,说明LinkedList随机访问效率不咋地;实现了Deque
(double-ended queue,双端队列),双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行。
与ArrayList相比,最大的不同便是LinkedList用双向链表实现,特点就是随机访问效率低,插入/删除元素效率高。
以下的“元素”表示便是要存的数据,而“节点”则表示包裹着元素(数据)的一个对象。
构造器
无参:
/**
* 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);//这个是个public方法,后面讲到的时候再细说
}
public boolean add(E e)
public boolean add(E e) {
linkLast(e);//直接添加到链表最后,这里封装了一个私有函数去做 添加元素到链表尾部 的工作
return true;
}
void linkLast(E e) {
final Node<E> l = last;//先保留对链表尾节点的引用
final Node<E> newNode = new Node<>(l, e, null);//新建节点,参数分别是
last = newNode;//链表尾元素指向新节点
if (l == null)//原先尾节点是null,说明链表还没有节点,添加的元素e是第一个节点
first = newNode;//所以头节点指针指向新节点
else
l.next = newNode;//原来链表就有节点了,改变原先尾节点的next指针,指向新节点
size++;//
modCount++;
}
public void addFirst(E e)
、public void addLast(E e)
就不说了。
addAll
有两个,分别是public boolean addAll(Collection<? extends E> c)
和 public boolean addAll(int index, Collection<? extends E> c)
。
- public boolean addAll(Collection<? extends E> c)
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);//调用了下面的public boolean addAll(int index, Collection<? extends E> c)
}
- public boolean addAll(int index, Collection<? extends E> c)
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);//边界校验
Object[] a = c.toArray();//变为数组,所以要求c不能为空否则会有NPE
int numNew = a.length;//数组长度
if (numNew == 0)//长度为0,不做任何操作直接返回false表明没有节点被添加到链表
return false;
Node<E> pred, succ;
//index的意思是从链表的index位置(包括)往后添加c的所有元素
if (index == size) {//index与size相等则说明:
succ = null;//1、没有节点(以及该节点后的所有节点组成的子链表)要被往后移
pred = last;//2、将c的所有元素插入链表的最后节点之后,插入c中的第一个元素的
} else {
succ = node(index);//该节点及该节点后的所有节点组成的子链表会在添加完c的所有元素之后再接上链表
pred = succ.prev;//前段子链表的尾节点
}
//其实上面的if-else就是用来将原先链表拆分为两个子链表,
//pred我称它为前段子链表的尾节点,succ我称它为后段子链表的头节点(请仔细体会一下~)
//这里就开始遍历c的所有元素包装为节点依次添加到 前段子链表的尾节点 之后,即pred节点之后,添加完的节点又成为新的前段子链表的尾节点
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;//添加完的节点又成为新的前段子链表的尾节点
}
//下面就是把 后段子链表与上面的 前段子链表 连在一起,此时pred依旧是 前段子链表的尾节点
if (succ == null) {//如果后段子链表的头节点为null,则说明 后段子链表 其实没有节点
last = pred;//直接让尾节点指针指向 前段子链表的尾节点(pred)
} else {//段子链表为正常的有节点的链表,则补充succ的prev指针指向和pred的next指针指向
pred.next = succ;//前段子链表的尾节点的next指针指向 后段子链表的头节点succ
succ.prev = pred;//后段子链表的头节点succ的prev指针指向 前段子链表的尾节点pred
}
size += numNew;
modCount++;
return true;
}
public E get(int index)
public E get(int index) {
checkElementIndex(index);//边界检查
return node(index).item;//定位节点并返回数据;记住这个方法,在下面remove里有出现,这里先不赘述。
}
remove
LinkedList的remove可太丰富了…
remove()
内部调用的就是removeFirst():
public E remove() {
return removeFirst();
}
removeFirst()
public E removeFirst() {
final Node<E> f = first;
if (f == null)//第一个节点都为空了说明这链表没有任何节点了
throw new NoSuchElementException();//这里抛异常
return unlinkFirst(f);//第一个节点不为null的话就把第一个节点解绑,从链条中解脱出去,让该节点等待GC的关顾
}
/**
* Unlinks non-null first node f.
*/
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;//置为null
f.next = null;//置为null,让GC清除
first = next;//第二个节点瞬间“转正”,正式成为第一个节点
if (next == null)//如果第二个节点不存在
last = null;//处理下尾指针,让它指向null
else
next.prev = null;//如果第二个节点存在,在“转正”正式成为第一个节点之后它前面就没节点了,所以它指向前个节点的指针就置为null
size--;
modCount++;
return element;//返回保存元素数据
}
public boolean remove(Object o)
从链表的头到尾用==
和equals
去找到第一个节点并unlink掉它(),如果找到返回true,否则返回false。
其中用==
判断
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;
}
public E remove(int index)
public E remove(int index) {
checkElementIndex(index);//边界校验
return unlink(node(index));//找到对应位置的节点然后解绑~
}
找给定位置的节点的代码有点意思,这里贴出来:
Node<E> node(int index) {
// assert isElementIndex(index);
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;
}
}
可以看到由于链表是双向的并且有size保存节点的个数,要找确定的位置的节点,就可以判断位置是否小于(size/2),是则从前往后,否则从后往前,要是我写这段代码可能就没考虑到这个优化点。
其他的removeXXX的不说了,要注意一下的是有些removeXXX找不到会抛NoSuchElementException
异常。
public void clear()
会把链表的所有节点内的所有属性(prev、item、next)置为null,以便GC。
public void clear() {
// Clearing all of the links between nodes is "unnecessary", but:
// - helps a generational GC if the discarded nodes inhabit
// more than one generation
// - is sure to free memory even if there is a reachable Iterator
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++;
}
其他
LinkedList还有一些其他的方法,比如:返回链表的第一个元素但不把它移出链表的public E peek()
、返回链表的第一个元素并把它移出链表的public E poll()
等等,大多都是实现了Deque
/Qeque
而来的,后面讲到队列的时候再说吧。
jend.