1、结构体系
LinkedList的结构体系比ArrayList要复杂的多,因为LinkedList不仅可用作链表,还能作为双端队列、栈,本次仅分析链表的使用
2、基本参数
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;
}
}
LinkedList中的成员变量较少,Node类是LinkedList的内部类,当做链表节点,从这三个成员变量中就能知道LinkedList是一个双向列表
3、构造方法
LinkedList中共有两个构造方法
//first
public LinkedList() {
}
//second
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
第二构造函数的参数为Collection<? extends E> c
,这意味可添加E元素或者E的子元素,不论是Collection接口的实现是LinkedList还是ArrayList或者Collection接口的其他实现。
看一下addAll方法
public boolean addAll(int index, Collection<? extends E> c) {
//检查下标是否越界
checkPositionIndex(index);
//转成数组
Object[] a = c.toArray();
int numNew = a.length;
//要加入的元素个数为0,直接返回
if (numNew == 0)
return false;
//前驱结点,后继节点
Node<E> pred, succ;
if (index == size) {
//index==size,说明当前要在集合末尾添加元素,后继节点succ就是null
succ = null;
pred = last;
} else {
//index!=size,说明从某个下标出添加元素
//找到下标为index的元素,并将index位置的元素设置成后继节点
succ = node(index);
//将index位置的前一个元素设置为前驱结点
pred = succ.prev;
}
//将要加入的元素构造成node节点
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;
}
3、add方法
add方法有两个
其一,直接向集合的末尾添加元素
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)
//l为空说明没有头结点,要么是第一次添加元素,要么是元素都被移除了
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
直接添加元素就是直接在集合末尾添加元素,比较容易理解
其二
public void add(int index, E element) {
//检测下标是否越界
checkPositionIndex(index);
//根据index和size相同时,说明是在尾部添加元素
if (index == size)
//上面分析过了
linkLast(element);
else
//从node方法中找到下标为index的节点,在从该节点前面插入元素
linkBefore(element, node(index));
}
//找到下标为index的node节点
Node<E> node(int index) {
// assert isElementIndex(index);
//index小于集合总元素个数的一半,从前往后遍历
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
//index大于等于集合总元素个数的一半,从后向前遍历
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
//将元素e构造成Node节点,并插入到相应节点中
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++;
}
在指定index出添加节点元素,
- 若index和size相同,则等价于add(E e)方法,即直接在集合末尾添加元素
- 若index和size不相同,会先找出下标为index的节点,这里有一个小小的优化,根据index和size的大小,判断是从前向后遍历还是从后向前遍历
- 找出下标为index的节点后,将新元素插入到其中即可
4、remove方法
public boolean remove(Object o) {
if (o == null) {
//如果o为null,从前往后遍历,找出第一个为null的元素,将其移除
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
//如果o不为null,同样从前往后遍历,找到元素o后,将其移除
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}
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;
}
删除的操作主要是操作node节点的prev和next节点
5、set方法
public E set(int index, E element) {
//检查下标是否越界
checkElementIndex(index);
//找到下标为index的节点,上面已分析过,不再赘述
Node<E> x = node(index);
E oldVal = x.item;
//设置为新值
x.item = element;
//返回节点原来的元素值
return oldVal;
}
6、get方法
public E get(int index) {
//检查下标是否越界
checkElementIndex(index);
//找打index节点元素并返回值,node方法在上面分析过,不再赘述
return node(index).item;
}