Java-LinkedList

1
2
3
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable

比ArrayList多实现了Queue和Deque 即实现了双向队列,ArrayList直接继承AbstractList,LinkedList继承于AbstractSequentialList,即顺序访问,AbstractList适合随机访问。

LinkedList底层就是链表实现,有内部类Node ,然后又first节点、last节点。

1
2
3
4
5
6
7
8
9
10
11
transient int size = 0;

/**
* Pointer to first node.
*/
transient Node<E> first;

/**
* Pointer to last node.
*/
transient Node<E> last;

Node类,item内容,前指针和后指针,双向链表

1
2
3
4
5
6
7
8
9
10
11
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;

Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}

剩下的就是对链表的一些操作以及toArray等等。