【学习篇】java-集合类-LinkedList
1.LinkedList类
引用官方文档:http://docs.oracle.com/javase/7/docs/api/
双向链表实现的接口列表和deque容器。实现所有的可选列表操作,可保存所有元素(包括空值)。
所有的操作都可以作为一个双联列表来执行。使用索引查找系统会从第一个往后或者最后一个往前查找。
注意,此实现是不同步的。如果多个线程同时访问同一个链表,其中一个在结构上修改了列表,必须要和其他线程同步。(修改是指增加 或者添加一个或多个元素,或者调整支持阵列;仅仅修改一个元素的值不是一个结构修改)。如果该对象不存在,该清单应“包装”使用collections.synchronizedlist方法。最好的做法是在创建时,防止意外的非同步访问列表。如下:
List list = Collections.synchronizedList(new LinkedList(...));
The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs
小结:
1. LinkedList是通过双向链表实现
2. 可保持null值
3. 可以通过index查找
4. 线程不安全。可以通过 collections.synchronizedlist 实现线程安全。
5. fail-fast 机制
2.LinkedList类
transient int size = 0; //元素个数 transient Node<E> first; //存放第一个元素 transient Node<E> last; //存放最后一个元素
2个Node变量是可以方便找到最前一个或者最后一个操作元素(一般都是添加)。
3.构造方法
public LinkedList() { } //可以传入 Collection 对象,保存到LinkedList中 public LinkedList(Collection<? extends E> c) { this(); addAll(c); }
4.常用方法实现
add( E e ),add(int index , E element),addFirst(E e),addLast( E e ), get(int index) 以及其他
举例其中一个说明如何实现链表。
//添加一个元素 public boolean add(E e) { linkLast(e); return true; } //默认向后node追加 void linkLast(E e) { //获取最后存放元素的node final Node<E> l = last; //创建一个新的Node,新node的上一个node是last,下一个是null final Node<E> newNode = new Node<>(l, e, null); //设置最后一个node是新node last = newNode; if (l == null) //只有第一次赋值的进入这个代码 first = newNode; else //将倒数第二个和最后关联,实现双向关联 l.next = newNode; //元素数量+1 size++; //fail-fast 机制用到 modCount++; } //Node 类 定义 private static class Node<E> { E item; //记录当前元素 Node<E> next; //下一个存放元素的node Node<E> prev; //上一个存放元素的node Node(Node<E> prev, E element, Node<E> next) { this.item = element; this.next = next; this.prev = prev; } }
-------------------------华丽的分割线------------------------------
public E get(int index) { //判断index是否不在范围内 checkElementIndex(index); return node(index).item; } private void checkElementIndex(int index) { if (!isElementIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } Node<E> node(int index) { // assert isElementIndex(index); // 判断 index 是否 < siez 的一半,如果是从 first 开始循环查找。 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; } }
5.Fail-Fast机制:
待续......

浙公网安备 33010602011771号