1,认识LinkedList
LinkedList是基于双链表实现的,具有双链表特性
LinkedList内部封装的是双向链表数据结构,每个节点是一个Node对象。
Node对象中封装的是要被添加的元素,还有一个指向上一个Node对象的引用和 指向下一个Node对象的引用 , 与ArrayList容器相比,不同的容器有不同的数据结构,不同的数据结构操作起来性能不同。
链表数据结构,做插入删除的效率比较高,但查询效率比较低 。 而数组结构(线性)做查询时效率高,可以直接通过下标来直接找到元素,但插入和删除效率低, 插入或删除元素后数组其他元素要做移位操作, 当需要频繁进行增删操作,使用LinkedList效率高,当只需要查询功能时,使用ArrayList容器效率高
2,类图关系
3,走进源码
- 数据结构
//连表长度
transient int size = 0;
//头结点指针
transient Node<E> first;
//尾结点指针
transient Node<E> last;
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;
}
}
3.1,构造函数
- 空构造函数
public LinkedList() {
}
- 根据一个集合来创建一个带有指定元素的列表
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
3.2添加元素
- 添加元素到头结点
public void addFirst(E e) {
linkFirst(e);
}
linkFirst()方法
private void linkFirst(E e) {
//获取头结点
final Node<E> f = first;
//创建一个新的节点
//因为该节点是头结点,所有构造函数一个指针为null,
//将节点newNode 的后置节点指向原头结点f,连接起来
final Node<E> newNode = new Node<>(null, e, f);
//更新头结点
first = newNode;
//f==null是空链表的情况下,添加了一个元素时,链表的头指针也是尾指针
if (f == null)
last = newNode;
else
//双链表结构的特性,所以有以下连接
//原头结点的前置节点指向新的节点,newNode成为新的头结点
f.prev = newNode;
size++;
modCount++;
}
- 添加元素到尾结点
public void addLast(E e) {
linkLast(e);
}
linikLast()方法,(与linkFirst()方法的相反操作)
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++;
}
- 将指定的元素追加到此列表的末尾
public boolean add(E e) {
linkLast(e);
return true;
}
- 将指定集合中的所有元素附加到此列表的末尾,以指定集合的迭代器返回它们的顺序
如果在操作正在进行中修改了指定的集合,则该操作的行为是不确定的。 (请注意,如果指定的集合是此列表,并且是非空的,则将发生这种情况。)
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
addAll()方法,从LinkedList的指定位置开始,附加指定集合中的所有元素到链表里。
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;
//默认从连表末端位置开始附加
if (index == size) {
succ = null;
//原连表末端节点作为新链表头结点
pred = last;
} else {
//指定位置开始附加元素
succ = node(index);
pred = succ.prev;
}
for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o;
//newNode的前置节点指向pred节点
Node<E> newNode = new Node<>(pred, e, null);
if (pred == null)
first = newNode;
else
//原末端链表节点的后置节点指向新创建的newNode节点
pred.next = newNode;
//更新前置节点
pred = newNode;
}
//默认从链表的末端开始附加,会出现succ=null的情况
if (succ == null) {
//更新末端指针指向
last = pred;
} else {
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
流程草图,大概这个样子
checkPositionIndex(index) 方法
指定位置超出连表长度,则会抛出异常
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
isPositionIndex(index)方法
判断指定位置是否超出连表长度
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
3.3删除元素
- 删除头结点
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
unlinkFirst(f)方法
private E unlinkFirst(Node<E> f) {
// assert f == first && f != null;
//获取元素值
final E element = f.item;
//获取f的后置节点
final Node<E> next = f.next;
//f节点元素置空,后置指针置空
f.item = null;
f.next = null; // help GC
//更新连表头结点
first = next;
if (next == null)
last = null;
else
next.prev = null;
size--;
modCount++;
return element;
}
- removeLast()删除尾结点
跟removeFirst()极为相似 - 从此列表中删除第一次出现的指定元素,如果存在的话。
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(x)方法
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;
}
- 删除此列表中指定位置的元素
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
3.4更改元素
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
3.5.查询
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}