public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
Itr() {}
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
图解中可以看出 LinkedList 有好多的 Node,并且还有 first 和 last 这两个变量保存头部和尾部节点的信息。
需要注意:LinkedList 不是一个循环的双向链表,因为他前后都是 null。
源码分析
变量
/**
* 集合元素的数量
*/
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;
构造方法
/**
* Constructs an empty list.
* 无参构造
*/
public LinkedList() {
}
/**
* 将集合C中的所有的元素都插入到链表中
* @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);
}
/**
* 将集合插入到链表的尾部
*/
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);
// 获取目标集合转为数组
Object[] a = c.toArray();
// 新增元素的数量
int numNew = a.length;
// 如果新增元素为0,则不添加,并且返回false
if (numNew == 0)
return false;
// 定义index节点的前置节点,后置节点
Node<E> pred, succ;
// 判断是不是链表的尾部,如果是,那么就在链表尾部追加数据
// 尾部的后置节点一定是null,前置节点是队尾
if (index == size) {
succ = null;
pred = last;
} else {
// 如果不是在链表的末尾而是在中间位置的话,
// 取出index节点,作为后继节点
succ = node(index);
// index节点的前节点,作为前驱的节点
pred = succ.prev;
}
// 链表批量的增加,去循环遍历原数组,依次去插入节点的操作
for (Object o : a) {
@SuppressWarnings("unchecked")
// 类型转换
E e = (E) o;
// 前置节点为pred,后置节点为null,当前节点值为e的节点newNode
Node<E> newNode = new Node<>(pred, e, null);
// 如果前置节点为空, 则newNode为头节点,否则为pred的next节点
if (pred == null)
first = newNode;
else
pred.next = newNode;
// 将新节点标记为前置节点,之后在此节点后添加新节点
pred = newNode;
}
// 循环结束后,如果后置节点是null,说明此时是在队尾追加的
if (succ == null) {
last = pred;
} else {
// 否则是在队中插入的节点 ,更新前置节点 后置节点
pred.next = succ;
succ.prev = pred;
}
// 修改数量size
size += numNew;
// 修改modCount
modCount++;
return true;
}
addFist
将 e 元素添加到链表并且设置其为头节点 Fist:
public void addFirst(E e) {
linkFirst(e);
}
/**
* Links e as first element.
* 将e元素弄成链接列表的第一个元素
*/
private void linkFirst(E e) {
final Node<E> f = first;
// 链表开头前驱为空,值为e,后继为f
final Node<E> newNode = new Node<>(null, e, f);
first = newNode;
// 若f为空,则表明列表中还没有元素,last也应该指向newNode
if (f == null)
last = newNode;
else
// 否则,前first的前驱指向newNode
f.prev = newNode;
size++;
modCount++;
}
addLast
将元素添加到链表,并且设置为尾部的节点 next:
public void addLast(E e) {
linkLast(e);
}
/**
* Links e as last element.
* 将e元素弄成链接列表的last元素
*/
void linkLast(E e) {
final Node<E> l = last;
// 前驱为前last,值为e,后继为null
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
// 最后一个节点为空,说明列表中无元素
if (l == null)
// first同样指向此节点
first = newNode;
else
// 否则,前last的后继指向当前节点
l.next = newNode;
size++;
modCount++;
}