前言
LinkedList内部实现是一个双链表,linkedList除了实现了list相关的接口外,还实现了Queue、Dequeue接口,所以它有着双端队列、list、栈的功能
注意LinkedList没有实现RandomAccess接口,这意味着LinkedList没有提供快速随机访问功能
属性
//链表数据长度
transient int size = 0;//链表首指针
transient Nodefirst;//链表尾指针
transient Node last;
private static class Node {
E item;
Node next;
Node prev;
Node(Node prev, E element, Node next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
构造器
publicLinkedList() {
}public LinkedList(Collection extends E>c) {this();
addAll(c);
}public boolean addAll(Collection extends E>c) {//复用了在指定索引批量添加元素的方法, 只不过这里size在构造器调用时为0
returnaddAll(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;
Nodepred, succ;if (index ==size) {
succ= null;
pred=last;
}else{
succ=node(index);
pred=succ.prev;
}for(Object o : a) {
@SuppressWarnings("unchecked&