LinkedList 继承了AbstractSequentialList类
实现了 List<E>, Deque<E>, Cloneable, java.io.Serializable接口
LinkedList 顾名思义 一个链式的表。那么底层数据结构就是一个链表维护的。这个链表的单元就是一个一个节点。下面来分析LinkedList的节点。
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;
}
}
熟悉C/C++的人,对这个形式并不陌生。这是定义链表的必经之路,只不过这里是一个内部类,而不是结构体。
在节点类中,定义了节点对象保存的数据,以及它的前驱和后继。由此可见LinkedList中,是由双向链表进行底层数据维护。
属性
transient int size = 0;
/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient Node<E> first;
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;
在LinkedList中 有三个比较重要的属性。 size 标识当前List的大小,
first 表示当前List的头节点。恒等式(first == null && last == null) || (first.prev == null && first.item != null)
last 表示当前List的末节点。恒等式(first == null && last == null) || (last.next == null && last.item != null)
构造方法
空的构造方法
构造一个空的LinkedList
/**
* Constructs an empty list.
*/
public LinkedList() {
}
利用一个已知集合构造方法
这个构造方法是利用一个已有的集合进行构造。但是有一个约束条件,即这个集合中的元素要是当前LinkedList中定义的泛型的子类,当然这是可以理解的,不是一家人,不进一家门嘛。
/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
构造方法首先调用了本类的一个公有方法,用于从集合中添加数据到当前LinkedList。
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
这个方法调用了另一个重载的方法,它的第一个参数标志插入的位置。此处用的是size, 也就是说是在当前集合的末尾
插入方法
addAll(int index, Collection<? extends E> c) 方法
在当前列表的指定位置插入一个已知集合
/**
* Inserts all of the elements in the specified collection into this
* list, starting at the specified position. Shifts the element
* currently at that position (if any) and any subsequent elements to
* the right (increases their indices). The new elements will appear
* in the list in the order that they are returned by the
* specified collection's iterator.
*
* @param index index at which to insert the first element
* from the specified collection
* @param c collection containing elements to be added to this list
* @return {@code true} if this list changed as a result of the call
* @throws IndexOutOfBoundsException {@inheritDoc}
* @throws NullPointerException if the specified collection is null
*/
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;
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;
}
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
(1)checkPositionIndex 是用来检测index是否合法。实现方法也很简单,只需判断
<span style="font-size:18px;">index >= 0 && index <= size</span>
(2)构造了一个数组,把Collection转换为对象数组,如果这个数组为空,那么直接返回false 。插入失败。
(4)判断要插入的位置在哪。如果是size,即末尾,那么让succ为空,pred指向尾节点。否则succ指向插入的位置,pred指向插入的位置前一个节点。
(5) 遍历对象数组,每次取一个对象构造一个新的节点,并将这个节点的前驱节点置为pred。如果pred是空,则这个新的节点就是LinkedList的头节点,否则让pred的后继节点为nexNode,pred指向newNode.这一步是所有链表插入的通用步骤,很好理解。而且这种插入仅仅是通过移动指针实现的,所以说LinkedList在需要插入大量元素的时候,效率是很高的
(6) 最后是处理末节点指针指向。如果这个插入动作是在原列表的末尾插入,那么pred就是最后尾节点.last指针指向这个位置,否则就让pred与succ连接上。这里才理解succ的作用,它就好像粘合剂一样,插入的时候,把原来的列表从它这个地方断开,在插入完成的时候,又从这个地方粘上。
(7)插入工作完成,modCount++。这个变量保证LinkedList的一致性。
add(E e)
在LinkedList 末尾插入新的元素,它的实现实际用到linkLast(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++;
}
linkLast 用于将元素插入到LinkedList的末尾,它是实现与addAll中的代码类似。个人觉得应该复用
add(int index, E element)
在指定位置插入元素
/**
* Inserts the specified element at the specified position in this list.
* Shifts the element currently at that position (if any) and any
* subsequent elements to the right (adds one to their indices).
*
* @param index index at which the specified element is to be inserted
* @param element element