3.具体的源码解析
  a:构造函数

 

public LinkedList() {
}
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;
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;
}<br>

  第一个构造函数是构造了一个空的链表,第二个构造函数是通过集合构造了一个链表相当于直接初始化的过程。第二个过程比较复杂,我们做个简单的说明。其实第二个构造方法只需要把addAll()方法讲明白了说名了这个构造函数的具体实现。在addAll()方法中首先判断传入的索引是否合法;然后将集合转化成我们需要的Object类型的数组(注意是数组);
  Node<E> pred, succ;
  if (index == size) {
  succ = null;
  pred = last;
  } else {
  succ = node(index);
  pred = succ.prev;
  }
  这个判断很重要,如果索引和链表的大小一致说明插入的集合只能放到链表尾部,这个时候讲尾指针赋值给pred;如果不相等说明只能把集合插入到链表的中部的某一个位置。这个是需要获取这个节点的元素才行: