在 stackoverflow上面回答是
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. 嵌套类是其外围类的成员。非静态内部类(内部类)可以访问封装类的其他成员,即使它们被声明为private。静态嵌套类不具有访问封闭类的其他成员。 There is no need for LinkedList.Entry to be top-level class as it is only used by LinkedList (there are some other interfaces that also have static nested classes named Entry, such as Map.Entry - same concept). And since it does not need access to LinkedList’s members, it makes sense for it to be static - it’s a much cleaner approach. 没有必要为LinkedList.Node是顶层类,因为它仅由LinkedList的(也有一些其他接口也有一个名为Node静态嵌套类,如Map.Entry的 - 同一概念)。而且因为它并不需要访问LinkedList的成员,它是有道理的它是静态的 - 这是一个更清洁的方式。
LinkedList 与 ArrayList 一样实现 List 接口 ArrayList 是 List 接口大小可变数组的实现 LinkedList是 List 接口链表的实现。 基于链表实现的方式使得 LinkedList 在插入和删除时更优于 ArrayList,而随机访问则比 ArrayList 逊色些 可见以下源码发现实现需要进行迭代返回 数组实现的话 不需要进行迭代返回点击查看数组在内存的位置
/** * Returns the element at the specified position in this list. * 返回列表中指定位置的元素 * @param index index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E get(int index) { checkElementIndex(index); return node(index).item; } /** * Returns the (non-null) Node at the specified element index. */ Node<E> node(int index) { //如果下标小于大小的一半从前往后找,否则从后往前找 if (index < (size >> 1)) { Node<E> x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }一个线性 collection,支持在两端插入和移除元素。名称 deque 是“double ended queue(双端队列)”的缩写,通常读为“deck”。大多数 Deque 实现对于它们能够包含的元素数没有固定限制,但此接口既支持有容量限制的双端队列,也支持没有固定大小限制的双端队列。
此接口定义在双端队列两端访问元素的方法。提供插入、移除和检查元素的方法。每种方法都存在两种形式:一种形式在操作失败时抛出异常,另一种形式返回一个特殊值(null 或 false,具体取决于操作)。插入操作的后一种形式是专为使用有容量限制的 Deque 实现设计的;在大多数实现中,插入操作不能失败。
次借口扩展可queue接口
Queue 方法等效 Deque 方法作用add(e)addLast(e)在不违反容量限制的情况下, 将指定元素插入此双端队列的末尾offer(e)offerLast(e)在不违反容量限制的情况下,将指定的元素插入此双端队列的末尾。remove()removeFirst()获取并移除此双端队列第一个元素。poll()pollFirst()获取并移除此双端队列的第一个元素;如果此双端队列为空,则返回 null。element()getFirst()获取,但不移除此双端队列的第一个元素。peek()peekFirst()获取,但不移除此双端队列的第一个元素;如果此双端队列为空,则返回 null。 /** * 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) throw new NoSuchElementException(); 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) throw new NoSuchElementException(); return l.item; } /** * Removes and returns the first element from this list. * * @return the first element from this list * @throws NoSuchElementException if this list is empty */ public E removeFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(f); } /** * Removes and returns the last element from this list. * * @return the last element from this list * @throws NoSuchElementException if this list is empty */ public E removeLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return unlinkLast(l); } /** * Inserts the specified element at the beginning of this list. * * @param e the element to add */ public void addFirst(E e) { linkFirst(e); } /** * Appends the specified element to the end of this list. * * <p>This method is equivalent to {@link #add}. * * @param e the element to add */ public void addLast(E e) { linkLast(e); }