LinkedList 基本示例及源码解析

0次阅读

共计 18294 个字符,预计需要花费 46 分钟才能阅读完成。

一、JavaDoc 简介

LinkedList 双向链表,实现了 List 的 双向队列接口,实现了所有 list 可选择性操作,允许存储任何元素(包括 null 值)
所有的操作都可以表现为双向性的,遍历的时候会从首部到尾部进行遍历,直到找到最近的元素位置

注意这个实现不是线程安全的, 如果多个线程并发访问链表,并且至少其中的一个线程修改了链表的结构,那么这个链表必须进行外部加锁。(结构化的操作指的是任何添加或者删除至少一个元素的操作,仅仅对已有元素的值进行修改不是结构化的操作)。

List list = Collections.synchronizedList(new LinkedList(…)), 可以用这种链表做同步访问,但是最好在创建的时间就这样做,避免意外的非同步对链表的访问
迭代器返回的 iterators 和 listIterator 方法会造成 fail-fast 机制:如果链表在生成迭代器之后被结构化的修改了,除了使用 iterator 独有的 remove 方法外,都会抛出并发修改的异常因此,在面对并发修改的时候,这个迭代器能够快速失败,从而避免非确定性的问题

二、LinkedList 继承接口和实现类介绍
java.util.LinkedList 继承了 AbstractSequentialList 并实现了 List , Deque , Cloneable 接口,以及 Serializable 接口
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable {}
类之间的继承体系如下:

下面就对继承树中的部分节点进行大致介绍:
AbstractSequentialList 介绍:这个接口是 List 一系列子类接口的核心接口,以求最大限度的减少实现此接口的工作量,由顺序访问数据存储 (例如链接链表) 支持。对于随机访问的数据 (像是数组),AbstractList 应该优先被使用这个接口可以说是与 AbstractList 类相反的,它实现了随机访问方法,提供了 get(int index),set(int index,E element), add(int index,E element) and remove(int index) 方法对于程序员来说:
要实现一个列表,程序员只需要扩展这个类并且提供 listIterator 和 size 方法即可。对于不可修改的列表来说,程序员需要实现列表迭代器的 hasNext(), next(), hasPrevious(),previous 和 index 方法
AbstractList 介绍:
这个接口也是 List 继承类层次的核心接口,以求最大限度的减少实现此接口的工作量,由顺序访问数据存储 (例如链接链表) 支持。对于顺序访问的数据 (像是链表),AbstractSequentialList 应该优先被使用,如果需要实现不可修改的 list,程序员需要扩展这个类,list 需要实现 get(int) 方法和 List.size() 方法如果需要实现可修改的 list,程序员必须额外重写 set(int,Object) set(int,E)方法(否则会抛出 UnsupportedOperationException 的异常),如果 list 是可变大小的,程序员必须额外重写 add(int,Object) , add(int, E) and remove(int) 方法
AbstractCollection 介绍:
这个接口是 Collection 接口的一个核心实现,尽量减少实现此接口所需的工作量为了实现不可修改的 collection,程序员应该继承这个类并提供呢 iterator 和 size 方法为了实现可修改的 collection,程序团需要额外重写类的 add 方法,iterator 方法返回的 Iterator 迭代器也必须实现 remove 方法

三、LinkedList 基本方法介绍
上面看完了 LinkedList 的继承体系之后,来看看 LinkedList 的基本方法说明
png](/img/bVbqVeN)

字比较小,可能有些不清晰,下面我就来对上面图片做一个大致介绍:
添加
add():
—-> 1. add(E e) : 直接在 ’ 末尾 ’ 处添加元素
—-> 2. add(int index,E element) : 在 ’ 指定索引处添 ’ 加元素
—-> 3. addAll(Collections<? extends E> c) : 在 ’ 末尾 ’ 处添加一个 collection 集合
—-> 4. addAll(int index,Collections<? extends E> c):在 ’ 指定位置 ’ 添加一个 collection 集合
—-> 5. addFirst(E e): 在 ’ 头部 ’ 添加指定元素
—-> 6. addLast(E e): 在 ’ 尾部 ’ 添加指定元素

offer():
—-> 1. offer(E e):在链表 ’ 末尾 ’ 添加元素
—-> 2. offerFirst(E e): 在 ’ 链表头 ’ 添加指定元素
—-> 3. offerLast(E e): 在 ’ 链表尾 ’ 添加指定元素

push(E e): 在 ’ 头部 ’ 压入元素

移除

poll():
—-> 1. poll(): 访问并移除 ’ 首部 ’ 元素
—-> 2. pollFirst(): 访问并移除 ’ 首部 ’ 元素
—-> 3. pollLast(): 访问并移除 ’ 尾部 ’ 元素

pop(): 从列表代表的堆栈中弹出元素,从 ’ 头部 ’ 弹出

remove():
—-> 1. remove(): 移除并返回 ’ 首部 ’ 元素
—-> 2. remove(int index) : 移除 ’ 指定索引 ’ 处的元素
—-> 3. remove(Object o): 移除指定元素
—-> 4. removeFirst(): 移除并返回 ’ 第一个 ’ 元素
—-> 5. removeFirstOccurrence(Object o): 从头到尾遍历,移除 ’ 第一次 ’ 出现的元素
—-> 6. removeLast(): 移除并返回 ’ 最后一个 ’ 元素
—-> 7. removeLastOccurrence(Object o): 从头到尾遍历,移除 ’ 最后一次 ’ 出现的元素

clear(): 清空所有元素

访问

peek():
—-> 1. peek(): 只访问,不移除 ’ 首部 ’ 元素
—-> 2. peekFirst(): 只访问,不移除 ’ 首部 ’ 元素,如果链表不包含任何元素,则返回 null
—-> 3. peekLast(): 只访问,不移除 ’ 尾部 ’ 元素,如果链表不包含任何元素,返回 null

element(): 只访问,不移除 ’ 头部 ’ 元素

get():
—-> 1. get(int index): 返回 ’ 指定索引 ’ 处的元素
—-> 2. getFirst(): 返回 ’ 第一个 ’ 元素
—-> 3. getLast(): 返回 ’ 最后一个 ’ 元素

indexOf(Object o): 检索某个元素 ’ 第一次 ’ 出现所在的位置
LastIndexOf(Object o): 检索某个元素 ’ 最后一次 ’ 出现的位置

其他

clone() : 返回一个链表的拷贝,返回值为 Object 类型
contains(Object o): 判断链表是否包含某个元素
descendingIterator(): 返回一个迭代器,里面的元素是倒叙返回的
listIterator(int index) : 在指定索引处创建一个 ’ 双向遍历迭代器 ’
set(int index, E element): 替换某个位置处的元素
size() : 返回链表的长度
spliterator(): 创建一个后期绑定并快速失败的元素
toArray(): 将链表转变为数组返回

四、LinkedList 基本方法使用
学以致用,熟悉了上面基本方法之后,来简单做一个 demo 测试一下上面的方法:
/**
* 此方法描述
* LinedList 集合的基本使用
*/
public class LinkedListTest {

public static void main(String[] args) {

LinkedList<String> list = new LinkedList<>();
list.add(“111”);
list.add(“222”);
list.add(“333″);
list.add(1,”123”);

// 分别在头部和尾部添加元素
list.addFirst(“top”);
list.addLast(“bottom”);
System.out.println(list);

// 数组克隆
Object listClone = list.clone();
System.out.println(listClone);

// 创建一个首尾互换的迭代器
Iterator<String> it = list.descendingIterator();
while (it.hasNext()){
System.out.print(it.next() + ” “);
}
System.out.println();
list.clear();
System.out.println(“list.contains(‘111’) ? ” + list.contains(“111”));

Collection<String> collec = Arrays.asList(“123″,”213″,”321”);
list.addAll(collec);
System.out.println(list);
System.out.println(“list.element = ” + list.element());
System.out.println(“list.get(2) = ” + list.get(2));
System.out.println(“list.getFirst() = ” + list.getFirst());
System.out.println(“list.getLast() = ” + list.getLast());

// 检索指定元素出现的位置
System.out.println(“list.indexOf(213) = ” + list.indexOf(“213”));
list.add(“123”);
System.out.println(“list.lastIndexOf(123) = ” + list.lastIndexOf(“123”));
// 在首部和尾部添加元素
list.offerFirst(“first”);
list.offerLast(“999”);
System.out.println(“list = ” + list);
list.offer(“last”);
// 只访问,不移除指定元素
System.out.println(“list.peek() = ” + list.peek());
System.out.println(“list.peekFirst() = ” + list.peekFirst());
System.out.println(“list.peekLast() = ” + list.peekLast());

// 访问并移除元素
System.out.println(“list.poll() = ” + list.poll());
System.out.println(“list.pollFirst() = ” + list.pollFirst());
System.out.println(“list.pollLast() = ” + list.pollLast());
System.out.println(“list = ” + list);
// 从首部弹出元素
list.pop();
// 压入元素
list.push(“123”);
System.out.println(“list.size() = ” + list.size());
System.out.println(“list = ” + list);

// remove 操作
System.out.println(list.remove());
System.out.println(list.remove(1));
System.out.println(list.remove(“999”));
System.out.println(list.removeFirst());
System.out.println(“list = ” + list);

list.addAll(collec);
list.addFirst(“123”);
list.addLast(“123”);
System.out.println(“list = ” + list);
list.removeFirstOccurrence(“123”);
list.removeLastOccurrence(“123”);
list.removeLast();
System.out.println(“list = ” + list);
list.addFirst(“top”);
list.addLast(“bottom”);
list.set(2,”321″);
System.out.println(“list = ” + list);
System.out.println(“————————–“);

// 创建一个 list 的双向链表
ListIterator<String> listIterator = list.listIterator();
while(listIterator.hasNext()){
// 移到 list 的末端
System.out.println(listIterator.next());
}
System.out.println(“————————–“);
while (listIterator.hasPrevious()){
// 移到 list 的首端
System.out.println(listIterator.previous());
}
}
}
Console:
——-1——- [top, 111, 123, 222, 333, bottom]
——-2——-[top, 111, 123, 222, 333, bottom]
bottom 333 222 123 111 top
list.contains(‘111’) ? false
[123, 213, 321]
list.element = 123
list.get(2) = 321
list.getFirst() = 123
list.getLast() = 321
list.indexOf(213) = 1
list.lastIndexOf(123) = 3
——-4——- [first, 123, 213, 321, 123, 999]
list.peek() = first
list.peekFirst() = first
list.peekLast() = last
list.poll() = first
list.pollFirst() = 123
list.pollLast() = last
——-5——- [213, 321, 123, 999]
list.size() = 4
——-6——- [123, 321, 123, 999]
123
123
true
321
——-7——- []
——-8——- [123, 123, 213, 321, 123]
list = [123, 213]
——-9——- [top, 123, 321, bottom]
————————–
top
123
321
bottom
————————–
bottom
321
123
top
五、LinkedList 内部结构以及基本元素声明

LinkedList 内部结构是一个双向链表具体示意图如下

每一个链表都是一个 Node 节点,由三个元素组成
private static class Node<E> {
// Node 节点的元素
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;
}
}
first 节点也是头节点,last 节点也是尾节点
LinkedList 中有三个元素,分别是
transient int size = 0; // 链表的容量

transient Node<E> first; // 指向第一个节点

transient Node<E> last; // 指向最后一个节点
LinkedList 有两个构造函数,一个是空构造函数,不添加任何元素,一种是创建的时候就接收一个 Collection 集合。
/**
* 空构造函数
*/
public LinkedList() {}

/**
* 创建一个包含指定元素的构造函数
*/
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
六、LinkedList 具体源码分析
前言: 此源码是作者根据上面的代码示例一步一步跟进去的,如果有哪些疑问或者讲的不正确的地方,请与作者联系。
添加
添加的具体流程示意图:

包括方法有:

add(E e)

add(int index, E element)

addAll(Collection<? extends E> c)

addAll(int index, Collection<? extends E> c)

addFirst(E e)

addLast(E e)

offer(E e)

offerFirst(E e)

offerLast(E e)

下面对这些方法逐个分析其源码:
add(E e):
// 添加指定元素至 list 末尾
public boolean add(E e) {
linkLast(e);
return true;
}

// 真正添加节点的操作
void linkLast(E e) {
final Node<E> l = last;
// 生成一个 Node 节点
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
// 如果 l = null,代表的是第一个节点,所以这个节点即是头节点
// 又是尾节点
if (l == null)
first = newNode;
else
// 如果不是的话,那么就让该节点的 next 指向新的节点
l.next = newNode;
size++;
modCount++;
}

比如第一次添加的是 111,此时链表中还没有节点,所以此时的尾节点 last 为 null, 生成新的节点,所以 此时的尾节点也就是 111,所以这个 111 也是头节点,再进行扩容,修改次数对应增加
第二次添加的是 222,此时链表中已经有了一个节点,新添加的节点会添加到尾部,刚刚添加的 111 就当作头节点来使用,222 被添加到 111 的节点后面。

add(int index,E e) :
/**
* 在指定位置插入指定的元素
*/
public void add(int index, E element) {
// 下标检查
checkPositionIndex(index);

if (index == size)
// 如果需要插入的位置和链表的长度相同,就在链表的最后添加
linkLast(element);
else
// 否则就链接在此位置的前面
linkBefore(element, node(index));
}

// 越界检查
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

// 判断参数是否是有效位置(对于迭代或者添加操作来说)
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}

// linkLast 上面已经介绍过

// 查找索引所在的节点
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;
}
}

// 在非空节点插入元素
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
// succ 即是插入位置的节点
// 查找该位置处的前面一个节点
final Node<E> pred = succ.prev;
final Node<E> newNode = new Node<>(pred, e, succ);
succ.prev = newNode;
if (pred == null)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}

例如在位置为 1 处添加值为 123 的元素,首先对下标进行越界检查,判断这个位置是否等于链表的长度,如果与链表长度相同,就往最后插入,如果不同的话,就在索引的前面插入。
下标为 1 处并不等于索引的长度,所以在索引前面插入,首先对查找 1 这个位置的节点是哪个,并获取这个节点的前面一个节点,在判断这个位置的前一个节点是否为 null,如果是 null,那么这个此处位置的元素就被当作头节点,如果不是的话,头节点的 next 节点就指向 123

addFirst(E e) :
// 在头节点插入元素
public void addFirst(E e) {
linkFirst(e);
}

private void linkFirst(E e) {
// 先找到 first 节点
final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f);
first = newNode;
if (f == null)
// f 为 null,也就代表着没有头节点
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}
例如要添加 top 元素至链表的首部,需要先找到 first 节点,如果 first 节点为 null,也就说明没有头节点,如果不为 null,则头节点的 prev 节点是新插入的节点。
addLast(E e) :
/**
* 在末尾处添加节点
*/
public void addLast(E e) {
linkLast(e);
}

// 链接末尾处的节点
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}

方法逻辑与在头节点插入基本相同
addAll(Collections<? extends E> c) :
/**
* 在链表中批量添加数据
*/
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}

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;
// 直接在末尾添加,所以 index = size
if (index == size) {
succ = null;
pred = last;
} else {
succ = node(index);
pred = succ.prev;
}

// 遍历每个数组
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;
}

Collection<String> collec = Arrays.asList(“123″,”213″,”321”);
list.addAll(collec);

例如要插入一个 Collection 为 123,213,321 的集合,没有指定插入元素的位置,默认是向链表的尾部进行链接,首先会进行数组越界检查,然后会把集合转换为数组,在判断数组的大小是否为 0,为 0 返回,不为 0,继续下面操作
因为是直接向链尾插入,所以 index = size,然后遍历每个数组,首先生成对应的节点,在对节点进行链接,因为 succ 是 null,此时 last 节点 = pred,这个时候的 pred 节点就是遍历数组完成后的最后一个节点
然后再扩容数组,增加修改次数

addAll(Collections<? extends E> c) : 这个方法的源码同上
offer 也是对元素进行添加操作,源码和 add 方法相同
offerFirst(E e)和 addFirst(E e) 源码相同
offerLast(E e)和 addLast(E e) 源码相同)
push(E e) 和 addFirst(E e) 源码相同
取出元素
包括方法有:

peek()

peekFirst()

peekLast()

element()

get(int index)

getFirst()

getLast()

indexOf(Object o)

lastIndexOf(Object o)

peek()
/**
* 只是访问,但是不移除链表的头元素
*/
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}

peek() 源码比较简单,直接找到链表的第一个节点,判断是否为 null,如果为 null,返回 null,否则返回链首的元素
peekFirst():源码和 peek() 相同
peekLast():
/**
* 访问,但是不移除链表中的最后一个元素
* 或者返回 null 如果链表是空链表
*/
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}
源码也比较好理解
element() :
/**
* 只是访问,但是不移除链表的第一个元素
*/
public E element() {
return getFirst();
}

public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
与 peek()相同的地方都是访问链表的第一个元素,不同是 element 元素在链表为 null 的时候会报空指针异常
get(int index) :
/*
* 返回链表中指定位置的元素
*/
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}

// 返回指定索引下的元素的非空节点
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;
}
}

get(int index)源码也是比较好理解,首先对下标进行越界检查,没有越界的话直接找到索引位置对应的 node 节点,进行返回
getFirst():源码和 element()相同
getLast(): 直接找到最后一个元素进行返回,和 getFist 几乎相同
indexOf(Object o) :
/*
* 返回第一次出现指定元素的位置,或者 - 1 如果不包含指定元素。
*/
public int indexOf(Object o) {
int index = 0;
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;
}

两种情况:

如果需要检索的元素是 null,对元素链表进行遍历,返回 x 的元素为空的位置
如果需要检索的元素不是 null,对元素的链表遍历,直到找到相同的元素,返回元素下标

lastIndexOf(Object o) :
/*
* 返回最后一次出现指定元素的位置,或者 - 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;
}
从 IndexOf(Object o)源码反向理解
删除
删除节点的示意图如下:

包括的方法有:

poll()

pollFirst()

pollLast()

pop()

remove()

remove(int index)

remove(Object o)

removeFirst()

removeFirstOccurrence(Object o)

removeLast()

removeLastOccurrence(Object o)

clear()

poll() :
/*
* 访问并移除链表中指定元素
*/
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(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;
f.next = null; // help GC
first = next;
if (next == null)
last = null;
else
next.prev = null;
size–;
modCount++;
return element;
}
poll()方法也比较简单直接,首先通过 Node 方法找到第一个链表头,然后把链表的元素和链表头指向的 next 元素置空,再把 next 节点的元素变为头节点的元素
pollFirst() : 与 poll() 源码相同
pollLast(): 与 poll() 源码很相似,不再解释
pop()

/*
* 弹出链表的指定元素,换句话说,移除并返回链表中第一个元素
*/
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}

// unlinkFirst 源码上面???? 有

removeFirst 源码就多了如果首部元素为 null,就直接抛出异常的操作
remove(int index):
/*
* 移除链表指定位置的元素
*/
public E remove(int index) {
checkElementIndex(index);
// 找到 index 的节点,断开指定节点
return unlink(node(index));
}

// 断开指定节点
E unlink(Node<E> x) {
// 找到链接节点的元素,next 节点和 prev 节点
final E element = x.item;
final Node<E> next = x.next;
final Node<E> prev = x.prev;

if (prev == null) {
first = next;
} else {
prev.next = next;
x.prev = null;
}

if (next == null) {
last = prev;
} else {
next.prev = prev;
x.next = null;
}

x.item = null;
size–;
modCount++;
return element;
}

remove(Object o)
/*
* 移除列表中第一次出现的指定元素,如果存在的话。如果列表不包含指定元素,则不会改变,
* 更进一步来说,移除索引最小的元素,前提是(o == null ? get(i) == null : o.equals(get(i)))
*/
public boolean remove(Object o) {
// 如果 o 为 null
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
// 匹配 null 对象,删除控对象,返回 true
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
// 如果不为 null
for (Node<E> x = first; x != null; x = x.next) {
// 匹配对应节点,返回 true
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}
removeFirst() 和 remove() 源码相同
removeFirstOccurrence(Object o)和 remove(Object o) 源码相同
removeLast() 和 pollLast() 相同
removeLastOccurrence(Object o) 和 removeFirstOccurrence(Object o) 相似
clear()

/*
* 清空所有元素
*/
public void clear() {
// 遍历元素,把元素的值置为 null
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++;
}
clear()方法,先找到链表头,循环遍历每一项,把每一项的 prev,item,next 属性置空,最后再清除 first 和 last 节点,注意源码有一点,x = next,这行代码是向后遍历的意思,根据 next 的元素再继续向后查找
其他方法
链表最常用的方法就是添加、查找、删除,下面来介绍一下其他的方法
clone()
/*
* 链表复制
*/
public Object clone() {
// 此处的 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;
}

private LinkedList<E> superClone() {
try {
return (LinkedList<E>) super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
// 本地方法
protected native Object clone() throws CloneNotSupportedException;
clone() 方法调用 superClone()能够获取拷贝过后的值,但是为什么要把 first 和 last 置为 null,debug 的时候就发现 clone 对象所有的值都为 null 了,而且为什么又要循环遍历链表再添加一遍?
contains(Object o) : 和 index 源码几乎相同
set(int index, E element)

/*
* 在指定位置替换指定元素
*/
public E set(int index, E element) {
// 越界检查
checkElementIndex(index);
// 找到索引元素所在的位置
Node<E> x = node(index);
// 元素替换操作,返回替换之前的元素
E oldVal = x.item;
x.item = element;
return oldVal;
}
descendingIterator()

public Iterator<E> descendingIterator() {
return new DescendingIterator();
}

private class DescendingIterator implements Iterator<E> {
private final ListItr itr = new ListItr(size());
public boolean hasNext() {
return itr.hasPrevious();
}
public E next() {
return itr.previous();
}
public void remove() {
itr.remove();
}
}
descendingIterator 就相当于创建了一个倒置的 Iterator,倒叙遍历
listIterator(int index) :

/*
* 在指定位置上返回一个列表的迭代器,这个 list-Iterator 是有快速失败机制的
* 可以参见我的另一篇文章 ArrayList 源码解析
*/
public ListIterator<E> listIterator(int index) {
checkPositionIndex(index);
return new ListItr(index);
}

// ListItr 是 LinkedList 的一个内部类
private class ListItr implements ListIterator<E> {
// 上一个被返回的节点
private Node<E> lastReturned;
// 下一个节点
private Node<E> next;
// 下一个下标
private int nextIndex;
// 期望的修改次数 = 修改次数,用于判断并发情况
private int expectedModCount = modCount;

// 在指定位置创建一个迭代器
ListItr(int index) {
next = (index == size) ? null : node(index);
nextIndex = index;
}

// 判断是否有下一个元素
// 判断的标准是下一个索引的值 < size , 说明当前位置最大 = 链表的容量
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() {
// 通过元素索引是否等于 0,来判断是否达到开头。
return nextIndex > 0;
}

// 遍历之前的元素
public E previous() {
checkForComodification();
if (!hasPrevious())
throw new NoSuchElementException();
// next 指向链表的上一个元素
lastReturned = next = (next == null) ? last : next.prev;
nextIndex–;
return lastReturned.item;
}

// 下一个索引
public int nextIndex() {
return nextIndex;
}

// 上一个索引
public int previousIndex() {
return nextIndex – 1;
}

// 移除元素,有 fail-fast 机制
public void remove() {
checkForComodification();
if (lastReturned == null)
throw new IllegalStateException();

Node<E> lastNext = lastReturned.next;
unlink(lastReturned);
if (next == lastReturned)
next = lastNext;
else
nextIndex–;
lastReturned = null;
expectedModCount++;
}

// 设置当前节点为 e,有 fail-fast 机制
public void set(E e) {
if (lastReturned == null)
throw new IllegalStateException();
checkForComodification();
lastReturned.item = e;
}

// 将 e 添加到当前节点的前面,也有 fail-fast 机制
public void add(E e) {
checkForComodification();
lastReturned = null;
if (next == null)
linkLast(e);
else
linkBefore(e, next);
nextIndex++;
expectedModCount++;
}

// jdk1.8 引入,用于快速遍历链表元素
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();
}

// 判断“modCount 和 expectedModCount 是否相等”,依次来实现 fail-fast 机制
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
toArray()

/*
* 返回 LinkedList 的 Object[]数组
*/
public Object[] toArray() {
Object[] result = new Object[size];
int i = 0;
// 将链表中所有节点的数据都添加到 Object[]数组中
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
return result;
}
toArray(T[] a)
/*
* 返回 LinkedList 的模板数组。所谓模板数组,即可以将 T 设为任意的数据类型
*/
public <T> T[] toArray(T[] a) {
// 若数组 a 的大小 < LinkedList 的元素个数(意味着数组 a 不能容纳 LinkedList 中全部元素)
// 则新建一个 T[]数组,T[]的大小为 LinkedList 大小,并将该 T[]赋值给 a。
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
// 将链表中所有节点的数据都添加到数组 a 中
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;
}
后记 : 笔者才疏学浅,如果有哪处错误产生误导,请及时与笔者联系更正,一起共建积极向上的 it 氛围
文章参考
Java 集合系列 05 之 LinkedList 详细介绍 (源码解析) 和使用示例
[java 双向链表示意图](
欢迎关注 Java 建设者,获取最新的技术文章,海量资源分享

正文完
 0