LinkedList ~ 源码学习:
初学的时候 只知道其中的方法怎么用,并不太了解其中的实现,所以呢,写篇记录一下学习过程。
目录
先来看看
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
LinkedList 实现了 List接口,Depue 接口,双向链表。
属性和构造函数
transient :将不需要序列化的属性加上 transient 关键字,意味着在序列化对象的时候,该属性不会被序列化。
transient int size = 0;
transient Node<E> first; // 指向第一个节点
transient Node<E> last; // 指向尾节点
// 无参构造
public LinkedList() {
}
// 集合为参 构造方法
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
内部类Node(私有)
看到属性的时候 ,会有内部类Node 的出现,点进去会看到:
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;
}
}
部分方法
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)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
add(int index,E element)
public void add(int index, E element) {
// 校验 此时传入的index 是否越界
checkPositionIndex(index);
// 如果传入的index 和此时元素个数相同,则直接在尾部添加
// 否则 :在链表中插入元素
if (index == size)
linkLast(element);
else
// 两个参数:一个插入的元素值;调用node(index) 返回index 对应的node 节点
linkBefore(element, node(index));
}
// checkPositionIndex() 校验是否越界
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
// isPositionIndex() 校验是否越界
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
// Node(index) 找到此时的index 位置的节点 node
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;
}
}
// linkBefore() 在链表中指定位置 插入指定的元素
// 传入的参数:插入的元素值;指定的节点
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
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++;
}
addFirst(E element)
public void addFirst(E e) {
linkFirst(e);
}
private void linkFirst(E e) {
// 新增节点 注意这里的 各节点
final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f);
first = newNode;
// if 为空 last指向该新增节点
// else 将头结点f 的前直接点prev 指向该新增元素
if (f == null)
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}
addAll(Collection c)
/** 首先会校验index 是否越界;
* 把集合以数组对象进行存放;
* 得到要插入位置的前置节点(pred)、后继节点(succ);
* 循环遍历数据,插入到链表(头部插入,尾部插入,中间插入) ;
**/
// 某个指定的集合 插入到链表(尾部插入)
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
// addAll(int index,Collection c) : 将指定集合 从指定位置开始插入
public boolean addAll(int index, Collection<? extends E> c) {
// 检查index 是否越界
checkPositionIndex(index);
// 集合转换成Object 数组
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;
// 要插入位置的前置节点 ,后继节点
Node<E> pred, succ;
// 若index==size成立 代表位置是尾部,succ为null(后继节点),pred为last (前置节点)
// 若index==size 不成立 则通过调用Node(index) 找到后继节点,然后得到前置节点
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;
}
// 尾部插入
// else 插入到链表中间,前后进行衔接
if (succ == null) {
last = pred;
}
else {
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
get(int index)
public E get(int index) {
// 校验 index 是否越界
checkElementIndex(index);
// 调用node(index) 方法去找到对应的节点node 并返回node的值
return node(index).item;
}
remove(Object o)
// 移除(删除)指定的元素 成功 返回 true ; 否则返回false
// if 指定的元素为null 循环遍历找到元素,调用unlink() 从链表中移除目标元素
// if 指定的元素不为 null
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;
}
// unlink()
E unlink(Node<E> x) {
// assert x != null;
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(int index)
public E remove(int index) {
// 校验 index 是否越界
checkElementIndex(index);
// 调用node(index) 找到该index 的节点,调用unlink() 将该节点删除
return unlink(node(index));
}