LinkedList和ArrayList都实现了List,可是二者的存储结构不同:LinkedList是链表,ArrayList是数组,那就决定了二者特性不同:java
LinkedList插入删除方便效率高,可是根据索引查找就须要遍历,比较慢了,而ArrayList偏偏相反node
主要有以下方法:数组
E unlink(Node<E> x) { // assert x != null; final E element = x.item; final Node<E> next = x.next; final Node<E> prev = x.prev; if (prev == null) { first = next; } else { prev.next = next; x.prev = null; } if (next == null) { last = prev; } else { next.prev = prev; x.next = null; } x.item = null; size--; modCount++; return element; }
删除某个节点数据:把该节点的先后数据的关系从新创建便可设计
/** * Inserts element e before non-null Node succ. */ void linkBefore(E e, Node<E> succ) { // assert succ != null; final Node<E> pred = succ.prev; final Node<E> newNode = new Node<>(pred, e, succ); succ.prev = newNode; if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++; }
在succ节点以前插入数据code
Node<E> node(int index) { // assert isElementIndex(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; } }
根据索引查找Node节点,此处有个小巧的设计:若是查找的索引小于size的通常则从前日后找,不然从后往前找;从代码中能够看出,根据索引查找节点须要遍历索引
因为LinkedList也实现了Deque接口,因此LinkedList也是一种队列实现接口
public E peek() { final Node<E> f = first; return (f == null) ? null : f.item; }
获取队列头数据队列
public E poll() { final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); }
获取队列头数据并同时移除头部数据element