/** * 就是集合c转换成LinkedList吧,addAll方法后面再说 */ public LinkedList(Collection<? extends E> c) { this(); addAll(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; }
/** * 在下标index后,添加集合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; }
addFirst
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** * 实现的办法是这个私有的,就是在表头加链表 */ private void linkFirst(E e) { final Node<E> f = first; final Node<E> newNode = new Node<>(null, e, f); first = newNode; if (f == null) last = newNode; else f.prev = newNode; size++; modCount++; }
addLast
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** * 实现的办法是这个私有的,就是在表尾加链表 */ 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++; }
clear
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** * 清除所有链表 */ public void clear() { for (Node<E> x = first; x != null; ) { Node<E> next = x.next; x.item = null; x.next = null; x.prev = null; x = next; } first = last = null; size = 0; modCount++; }
/** * 是否包含此元素 */ public boolean contains(Object o) { return indexOf(o) != -1; } /** * 判断是否存在此元素,存在返回下标,否则返回-1 */ public int indexOf(Object o) { int index = 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; } /** * 从尾部开始循环,判断是否存在此元素,存在返回下标,否则返回-1 */ public int lastIndexOf(Object o) { int index = 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 get(int index) { checkElementIndex(index); return node(index).item; } /** * 返回链表头的元素 */ public E getFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return f.item; } /** * 返回链表尾的元素 */ public E getLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return l.item; }
listIterator
1 2 3 4 5 6 7
/** * 就是根据下标new一个内部迭代类 */ public ListIterator<E> listIterator(int index) { checkPositionIndex(index); return new ListItr(index); }
offer、offerFirst、offerLast
这个跟add一样的,只不过返回一个true的结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14
public boolean offer(E e) { return add(e); } public boolean offerFirst(E e) { addFirst(e); return true; } public boolean offerLast(E e) { addLast(e); return true; }
/** * 根据下标设置对应链表的元素 */ public E set(int index, E element) { checkElementIndex(index); Node<E> x = node(index); E oldVal = x.item; x.item = element; return oldVal; }
toArray
1 2 3 4 5 6 7 8 9 10
/** * 转换成数组 */ public Object[] toArray() { Object[] result = new Object[size]; int i = 0; for (Node<E> x = first; x != null; x = x.next) result[i++] = x.item; return result; }