/** * Returns the first element in this list. * 返回第一个节点元素 * @return the first element in this list * @throws NoSuchElementException if this list is empty */ public E getFirst() { final Node<E> f = first; if (f == null) thrownewNoSuchElementException(); return f.item; }
/** * Returns the last element in this list. * 返回末尾节点元素 * @return the last element in this list * @throws NoSuchElementException if this list is empty */ public E getLast() { final Node<E> l = last; if (l == null) thrownewNoSuchElementException(); return l.item; }
/** * Appends the specified element to the end of this list. * 将元素加入链表的末尾 * <p>This method is equivalent to {@link #addLast}. * * @param e element to be appended to this list * @return {@code true} (as specified by {@link Collection#add}) */ publicbooleanadd(E e) { linkLast(e); returntrue; } /** * Links e as last element. * add函数中调用的方法,将e连接至链表末尾 */ voidlinkLast(E e) { final Node<E> l = last; final Node<E> newNode = newNode<>(l, e, null); last = newNode; if (l == null) first = newNode; else l.next = newNode; size++; modCount++; }
链表节点的 Node 类
1 2 3 4 5 6 7 8 9 10 11
privatestaticclassNode<E> { E item; Node<E> next; Node<E> prev;