publicbooleanaddAll(Collection<? extends E> c) { return addAll(size, c); }
addAll(int index, Collection c): 将集合从指定位置开始插入
publicbooleanaddAll(int index, Collection<? extends E> c) { //1:检查index范围是否在size之内 checkPositionIndex(index);
//2:toArray()方法把集合的数据存到对象数组中 Object[] a = c.toArray(); intnumNew= a.length; if (numNew == 0) returnfalse;
//3:得到插入位置的前驱节点和后继节点 Node<E> pred, succ; //如果插入位置为尾部,前驱节点为last,后继节点为null if (index == size) { succ = null; pred = last; } //否则,调用node()方法得到后继节点,再得到前驱节点 else { succ = node(index); pred = succ.prev; }
// 4:遍历数据将数据插入 for (Object o : a) { @SuppressWarnings("unchecked")Ee= (E) o; //创建新节点 Node<E> newNode = newNode<>(pred, e, null); //如果插入位置在链表头部 if (pred == null) first = newNode; else pred.next = newNode; pred = newNode; }
//如果插入位置在尾部,重置last节点 if (succ == null) { last = pred; } //否则,将插入的链表与先前链表连接起来 else { pred.next = succ; succ.prev = pred; }
size += numNew; modCount++; returntrue; }
上面可以看出addAll方法通常包括下面四个步骤:
检查index范围是否在size之内
toArray()方法把集合的数据存到对象数组中
得到插入位置的前驱和后继节点
遍历数据,将数据插入到指定位置
addFirst(E e): 将元素添加到链表头部
publicvoidaddFirst(E e) { linkFirst(e); }
privatevoidlinkFirst(E e) { final Node<E> f = first; final Node<E> newNode = newNode<>(null, e, f);//新建节点,以头节点为后继节点 first = newNode; //如果链表为空,last节点也指向该节点 if (f == null) last = newNode; //否则,将头节点的前驱指针指向新节点,也就是指向前一个元素 else f.prev = newNode; size++; modCount++; }
addLast(E e): 将元素添加到链表尾部,与 add(E e) 方法一样
publicvoidaddLast(E e) { linkLast(e); }
根据位置取数据的方法
get(int index)::根据指定索引返回数据
public E get(int index) { //检查index范围是否在size之内 checkElementIndex(index); //调用Node(index)去找到index对应的node然后返回它的值 return node(index).item; }
获取头节点(index=0)数据方法:
public E getFirst() { final Node<E> f = first; if (f == null) thrownewNoSuchElementException(); return f.item; } public E element() { return getFirst(); } public E peek() { final Node<E> f = first; return (f == null) ? null : f.item; }
public E peekFirst() { final Node<E> f = first; return (f == null) ? null : f.item; }
public E getLast() { final Node<E> l = last; if (l == null) thrownewNoSuchElementException(); return l.item; } public E peekLast() { final Node<E> l = last; return (l == null) ? null : l.item; }
publicintindexOf(Object o) { intindex=0; if (o == null) { //从头遍历 for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else { //从头遍历 for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) return index; index++; } } return -1; }
int lastIndexOf(Object o): 从尾遍历找
publicintlastIndexOf(Object o) { intindex= size; if (o == null) { //从尾遍历 for (Node<E> x = last; x != null; x = x.prev) { index--; if (x.item == null) return index; } } else { //从尾遍历 for (Node<E> x = last; x != null; x = x.prev) { index--; if (o.equals(x.item)) return index; } } return -1; }
public E pop() { return removeFirst(); } public E remove() { return removeFirst(); } public E removeFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(f); }
removeLast(),pollLast(): 删除尾节点
public E removeLast() { final Node<E> l = last; if (l == null) thrownewNoSuchElementException(); return unlinkLast(l); } public E pollLast() { final Node<E> l = last; return (l == null) ? null : unlinkLast(l); }