LinkedList是一个实现了List接口和Deque接口的双端链表。 
有关索引的操作可能从链表头开始遍历到链表尾部,也可能从尾部遍历到链表头部,这取决于看索引更靠近哪一端。 
LinkedList不是线程安全的,如果想使LinkedList变成线程安全的,可以使用如下方式:

List list=Collections.synchronizedList(new LinkedList(...));

iterator()和listIterator()返回的迭代器都遵循fail-fast机制。 
LinkedList的继承关系如下图所示: 
 
从上图可以看出LinkedList与ArrayList的不同之处,ArrayList直接继承自AbstractList,而LinkedList继承自AbstractSequentialList,然后再继承自AbstractList。另外,LinkedList还实现了Dequeu接口,双端队列。

内部结构

LinkedList内部是一个双端链表的结构,结构如下图: 

从上图可以看出,LinkedList内部是一个双端链表结构,有两个变量,first指向链表头部,last指向链表尾部。 
LinkedtList内部的成员变量如下:

transient int size = 0;

transient Node<E> first;

transient Node<E> last;

其中size表示当前链表中的数据个数。下面是Node节点的定义,Node类LinkedList的静态内部类。

  1. private static class Node<E> {
  2. E item;
  3. Node<E> next;
  4. Node<E> prev;
  5.  
  6. Node(Node<E> prev, E element, Node<E> next) {
  7. this.item = element;
  8. this.next = next;
  9. this.prev = prev;
  10. }
  11. }

从Node的定义可以看出链表是一个双端链表的结构。

构造方法

LinkedList有两个构造方法,一个用于构造一个空的链表,一个用已有的集合创建链表。如下:

  1. public LinkedList() {
  2. }
  3.  
  4. public LinkedList(Collection<? extends E> c) {
  5. this();
  6. addAll(c);//添加集合中所有元素
  7. }

当使用第二个构造方法时,会调用addAll()方法将集合中的元素添加到链表中,添加的操作后面会详细介绍。

添加操作

因为LinkedList即实现了List接口,又实现了Deque接口,所以LinkedList既可以添加将元素添加到尾部,也可以将元素添加到指定索引位置,还可以添加添加整个集合;另外既可以在头部添加,又可以在尾部添加。下面我们分别从List接口和Deque接口分别介绍。

List接口的添加操作

add(E e)

add(E e)用于将元素添加到链表尾部,实现如下:

  1. public boolean add(E e) {
  2. linkLast(e);
  3. return true;
  4. }
  5.  
  6. void linkLast(E e) {
  7. final Node<E> l = last;//指向链表尾部
  8. final Node<E> newNode = new Node<>(l, e, null);//以尾部为前驱节点创建一个新节点
  9. last = newNode;//将链表尾部指向新节点
  10. if (l == null)//如果链表为空,那么该节点既是头节点也是尾节点
  11. first = newNode;
  12. else//链表不为空,那么将该结点作为原链表尾部的后继节点
  13. l.next = newNode;
  14. size++;//增加尺寸
  15. modCount++;
  16. }

从上面代码可以看到,linkLast方法中就是一个链表尾部添加一个双端节点的操作,但是需要注意对链表为空时头节点的处理。

add(int index,E e)

add(int index,E e)用于在指定位置添加元素。实现如下:

  1. public void add(int index, E element) {
  2. checkPositionIndex(index); //检查索引是否处于[0-size]之间
  3.  
  4. if (index == size)//添加在链表尾部
  5. linkLast(element);
  6. else//添加在链表中间
  7. linkBefore(element, node(index));
  8. }

从上面代码可以看到,主要分为3步: 
1. 检查index的范围,否则抛出异常 
2. 如果插入位置是链表尾部,那么调用linkLast方法 
3. 如果插入位置是链表中间,那么调用linkBefore方法

linkLast方法前面已经讨论了,下面看一下linkBefore的实现。在看linkBefore之前,先看一下node(int index)方法,该方法返回指定位置的节点,实现如下:

  1. Node<E> node(int index) {
  2. // assert isElementIndex(index);
  3.  
  4. //如果索引位置靠链表前半部分,从头开始遍历
  5. if (index < (size >> 1)) {
  6. Node<E> x = first;
  7. for (int i = 0; i < index; i++)
  8. x = x.next;
  9. return x;
  10. }
  11. //否则,从尾开始遍历
  12. else {
  13. Node<E> x = last;
  14. for (int i = size - 1; i > index; i--)
  15. x = x.prev;
  16. return x;
  17. }
  18. }

从上面可以看到,node(int index)方法将根据index是靠近头部还是尾部选择不同的遍历方向。一旦得到了指定索引位置的节点,再看linkBefore()方法,实现如下:

  1. void linkBefore(E e, Node<E> succ) {
  2. // assert succ != null;
  3. final Node<E> pred = succ.prev;
  4. final Node<E> newNode = new Node<>(pred, e, succ);
  5. succ.prev = newNode;
  6. if (pred == null)
  7. first = newNode;
  8. else
  9. pred.next = newNode;
  10. size++;
  11. modCount++;
  12. }

linkBefore()方法在第二个参数节点之前插入一个新节点。示意图如下: 



从上图以及代码可以看到linkBefore主要分三步: 
1. 创建newNode节点,将newNode的后继指针指向succ,前驱指针指向pred 
2. 将succ的前驱指针指向newNode 
3. 根据pred是否为null,进行不同操作。 
- 如果pred为null,说明该节点插入在头节点之前,要重置first头节点 
- 如果pred不为null,那么直接将pred的后继指针指向newNode即可

addAll方法

addAll有两个重载方法,一个参数的方法表示将集合元素添加到链表尾部,而两个参数的方法指定了开始插入的位置。实现如下:

  1. //将集合插入到链表尾部,即开始索引位置为size
  2. public boolean addAll(Collection<? extends E> c) {
  3. return addAll(size, c);
  4. }
  5.  
  6. //将集合从指定位置开始插入
  7. public boolean addAll(int index, Collection<? extends E> c) {
  8. //Step 1:检查index范围
  9. checkPositionIndex(index);
  10.  
  11. //Step 2:得到集合的数据
  12. Object[] a = c.toArray();
  13. int numNew = a.length;
  14. if (numNew == 0)
  15. return false;
  16.  
  17. //Step 3:得到插入位置的前驱节点和后继节点
  18. Node<E> pred, succ;
  19. //如果插入位置为尾部,前驱节点为last,后继节点为null
  20. if (index == size) {
  21. succ = null;
  22. pred = last;
  23. }
  24. //否则,调用node()方法得到后继节点,再得到前驱节点
  25. else {
  26. succ = node(index);
  27. pred = succ.prev;
  28. }
  29.  
  30. //Step 4:遍历数据将数据插入
  31. for (Object o : a) {
  32. @SuppressWarnings("unchecked") E e = (E) o;
  33. //创建新节点
  34. Node<E> newNode = new Node<>(pred, e, null);
  35. //如果插入位置在链表头部
  36. if (pred == null)
  37. first = newNode;
  38. else
  39. pred.next = newNode;
  40. pred = newNode;
  41. }
  42.  
  43. //如果插入位置在尾部,重置last节点
  44. if (succ == null) {
  45. last = pred;
  46. }
  47. //否则,将插入的链表与先前链表连接起来
  48. else {
  49. pred.next = succ;
  50. succ.prev = pred;
  51. }
  52.  
  53. size += numNew;
  54. modCount++;
  55. return true;
  56. }

从上面的代码可以看到,addAll方法主要分为4步: 
1. 检查index索引范围 
2. 得到集合数据 
3. 得到插入位置的前驱和后继节点 
4. 遍历数据,将数据插入到指定位置

Deque接口的添加操作

addFirst(E e)方法

addFirst()方法用于将元素添加到链表头部,其实现如下:

  1. public void addFirst(E e) {
  2. linkFirst(e);
  3. }
  4.  
  5. private void linkFirst(E e) {
  6. final Node<E> f = first;
  7. final Node<E> newNode = new Node<>(null, e, f);//新建节点,以头节点为后继节点
  8. first = newNode;
  9. //如果链表为空,last节点也指向该节点
  10. if (f == null)
  11. last = newNode;
  12. //否则,将头节点的前驱指针指向新节点
  13. else
  14. f.prev = newNode;
  15. size++;
  16. modCount++;
  17. }

从上面的代码看到,实现就是在头节点插入一个节点使新节点成为新节点,但是和linkLast一样需要注意当链表为空时,对last节点的设置。

addLast(E e)方法

addLast()方法用于将元素添加到链表尾部,与add()方法一样。所以实现也一样,如下:

  1. public void addLast(E e) {
  2. linkLast(e);
  3. }

offer(E e)方法

offer(E e)方法用于将数据添加到链表尾部,其内部调用了add(E e)方法,如下:

  1. public boolean offer(E e) {
  2. return add(e);
  3. }

offerFirst(E e)方法

offerFirst()方法用于将数据插入链表头部,与addFirst的区别在于该方法可以返回特定的返回值,而addFirst的返回值为void。

  1. public boolean offerFirst(E e) {
  2. addFirst(e);
  3. return true;
  4. }

offerLast(E e)方法

offerLast()与addLast()的区别和offerFirst()和addFirst()的区别一样,所以这儿就不多说了。

添加操作总结

LinkedList由于实现了List和Deque接口,所以有多种添加方法,下面总结了一下。 
- 将数据插入到链表尾部 
- boolean add(E e): 
- void addLast(E e) 
- boolean offerLast(E e) 
- 将数据插入到链表头部 
- void addFirst(E e) 
- boolean offerFirst(E e) 
- 将数据插入到指定索引位置 
- boolean add(int index,E e)

检索操作

根据位置取数据

获取任意位置的get(int index)方法

get(int index)方法根据指定索引返回数据,如果索引越界,那么会抛出异常。实现如下:

  1. public E get(int index) {
  2. //检查边界
  3. checkElementIndex(index);
  4. return node(index).item;
  5. }

从上面的代码可以看到分为2步: 
1. 检查index边界,index>=0&&index

获得位置为0的头节点数据

LinkedList中有多种方法可以获得头节点的数据,实现大同小异,区别在于对链表为空时的处理,是抛出异常还是返回null。主要方法有getFirst()、element()、peek()、peekFirst()、方法。其中getFirst()和element()方法将会在链表为空时,抛出异常,它们的实现如下:

  1. public E getFirst() {
  2. final Node<E> f = first;
  3. if (f == null)
  4. throw new NoSuchElementException();
  5. return f.item;
  6. }
  7. public E element() {
  8. return getFirst();
  9. }

从代码可以看到,element()方法的内部就是使用getFirst()实现的。它们会在链表为空时,抛出NoSuchElementException。下面再看peek()和peekFirst()的实现:

  1. public E peek() {
  2. final Node<E> f = first;
  3. return (f == null) ? null : f.item;
  4. }
  5.  
  6. public E peekFirst() {
  7. final Node<E> f = first;
  8. return (f == null) ? null : f.item;
  9. }

从代码可以看到,当链表为空时,peek()和peekFirst()方法返回null。

获得位置为size-1的尾节点数据

获得尾节点数据的方法有getLast()和peekLast()。getLast()的实现如下:

  1. public E getLast() {
  2. final Node<E> l = last;
  3. if (l == null)
  4. throw new NoSuchElementException();
  5. return l.item;
  6. }

可以看到,getLast()方法在链表为空时,会抛出NoSuchElementException,而peekLast()则不会,只是会返回null。实现如下:

  1. public E peekLast() {
  2. final Node<E> l = last;
  3. return (l == null) ? null : l.item;
  4. }

根据对象得到索引

根据对象得到索引分为两种,一种是第一个匹配的索引,一个是最后一个匹配的索引,实现的在于一个从前往后遍历,一个从后往前遍历。下面先看idnexOf()方法的实现:

  1. //返回第一个匹配的索引
  2. public int indexOf(Object o) {
  3. int index = 0;
  4. if (o == null) {
  5. //从头往后遍历
  6. for (Node<E> x = first; x != null; x = x.next) {
  7. if (x.item == null)
  8. return index;
  9. index++;
  10. }
  11. } else {
  12. //从头往后遍历
  13. for (Node<E> x = first; x != null; x = x.next) {
  14. if (o.equals(x.item))
  15. return index;
  16. index++;
  17. }
  18. }
  19. return -1;
  20. }

从上面的代码可以看到,LinkedList可以包含null元素,遍历方式都是从前往后,一旦匹配了,就返回索引。 
lastIndexOf()方法返回最后一个匹配的索引,实现为从后往前遍历,源码如下:

  1. //返回最后一个匹配的索引
  2. public int lastIndexOf(Object o) {
  3. int index = size;
  4. if (o == null) {
  5. //从后向前遍历
  6. for (Node<E> x = last; x != null; x = x.prev) {
  7. index--;
  8. if (x.item == null)
  9. return index;
  10. }
  11. } else {
  12. //从后向前遍历
  13. for (Node<E> x = last; x != null; x = x.prev) {
  14. index--;
  15. if (o.equals(x.item))
  16. return index;
  17. }
  18. }
  19. return -1;
  20. }

检查链表是否包含某对象

contains(Object o)方法检查对象o是否存在于链表中,其实现如下:

  1. public boolean contains(Object o) {
  2. return indexOf(o) != -1;
  3. }

从代码可以看到contains()方法调用了indexOf()方法,只要返回结果不是-1,那就说明该对象存在于链表中

检索操作总结

检索操作分为按照位置得到对象以及按照对象得到位置两种方式,其中按照对象得到位置的方法有indexOf()和lastIndexOf();按照位置得到对象有如下方法: 
- 根据任意位置得到数据的get(int index)方法,当index越界会抛出异常 
- 获得头节点数据 
- getFirst()和element()方法在链表为空时会抛出NoSuchElementException 
- peek()和peekFirst()方法在链表为空时会返回null 
- 获得尾节点数据 
- getLast()在链表为空时会抛出NoSuchElementException 
- peekLast()在链表为空时会返回null

删除操作

删除操作分为按照位置删除和按照对象删除,其中按照位置删除的方法又有区别,有的只是返回是否删除成功的标志,有的还需要返回被删除的元素。下面分别讨论。

删除指定对象

当删除指定对象时,只需调用remove(Object o)即可,不过该方法一次只会删除一个匹配的对象,如果删除了匹配对象,返回true,否则false。其实现如下:

  1. public boolean remove(Object o) {
  2. //如果删除对象为null
  3. if (o == null) {
  4. //从前向后遍历
  5. for (Node<E> x = first; x != null; x = x.next) {
  6. //一旦匹配,调用unlink()方法和返回true
  7. if (x.item == null) {
  8. unlink(x);
  9. return true;
  10. }
  11. }
  12. } else {
  13. //从前向后遍历
  14. for (Node<E> x = first; x != null; x = x.next) {
  15. //一旦匹配,调用unlink()方法和返回true
  16. if (o.equals(x.item)) {
  17. unlink(x);
  18. return true;
  19. }
  20. }
  21. }
  22. return false;
  23. }

从代码可以看到,由于LinkedList可以存储null元素,所以对删除对象以是否为null做区分。然后从链表头开始遍历,一旦匹配,就会调用unlink()方法将该节点从链表中移除。下面是unlink()方法的实现:

  1. E unlink(Node<E> x) {
  2. // assert x != null;
  3. final E element = x.item;
  4. final Node<E> next = x.next;//得到后继节点
  5. final Node<E> prev = x.prev;//得到前驱节点
  6.  
  7. //删除前驱指针
  8. if (prev == null) {
  9. first = next;如果删除的节点是头节点,令头节点指向该节点的后继节点
  10. } else {
  11. prev.next = next;//将前驱节点的后继节点指向后继节点
  12. x.prev = null;
  13. }
  14.  
  15. //删除后继指针
  16. if (next == null) {
  17. last = prev;//如果删除的节点是尾节点,令尾节点指向该节点的前驱节点
  18. } else {
  19. next.prev = prev;
  20. x.next = null;
  21. }
  22.  
  23. x.item = null;
  24. size--;
  25. modCount++;
  26. return element;
  27. }

上面的代码可以用如下示意图来解释: 

第一步:得到待删除节点的前驱节点和后继节点 

第二步:删除前驱节点 

第三步:删除后继节点 
经过三步,待删除的结点就从链表中脱离了。需要注意的是删除位置是头节点或尾节点时候的处理,上面的示意图没有特别指出。

按照位置删除对象

删除任意位置的对象

boolean remove(int index)方法用于删除任意位置的元素,如果删除成功将返回true,否则返回false。实现如下:

  1. public E remove(int index) {
  2. //检查index范围
  3. checkElementIndex(index);
  4. //将节点删除
  5. return unlink(node(index));
  6. }

从上面可以看到remove(int index)操作有两步: 
1. 检查index范围,属于[0,size) 
2. 将索引出节点删除

删除头节点的对象

删除头节点的对象的方法有很多,包括remove()、removeFirst()、pop()、poll()、pollFirst(),其中前三个方法在链表为空时将抛出NoSuchElementException,后两个方法在链表为空时将返回null。 
remove()、pop()、removeFirst()的实现如下:

  1. public E remove() {
  2. return removeFirst();
  3. }
  4.  
  5. public E pop() {
  6. return removeFirst();
  7. }
  8.  
  9. public E removeFirst() {
  10. final Node<E> f = first;
  11. if (f == null)
  12. throw new NoSuchElementException();
  13. return unlinkFirst(f);
  14. }

从上面代码可以看到,remove()和pop()内部调用了removeFirst()方法,而removeFirst()在链表为空时将抛出NoSuchElementException。 
下面是poll()和pollFirst()的实现:

  1. public E poll() {
  2. final Node<E> f = first;
  3. return (f == null) ? null : unlinkFirst(f);
  4. }
  5.  
  6. public E pollFirst() {
  7. final Node<E> f = first;
  8. return (f == null) ? null : unlinkFirst(f);
  9. }

可以看到poll()和pollFirst()的实现代码是相同的,在链表为空时将返回null。

删除尾节点的对象

删除尾节点的对象的方法有removeLast()和pollLast()。removeLast的实现如下:

  1. public E removeLast() {
  2. final Node<E> l = last;
  3. if (l == null)
  4. throw new NoSuchElementException();
  5. return unlinkLast(l);
  6. }

可以看到removeLast()在链表为空时将抛出NoSuchElementException。而pollLast()方法则不会,如下:

  1. public E pollLast() {
  2. final Node<E> l = last;
  3. return (l == null) ? null : unlinkLast(l);
  4. }

可以看到pollLast()在链表为空时会返回null,而不是抛出异常。

删除操作总结

删除操作由很多种方法,有: 
- 按照指定对象删除:boolean remove(Object o),一次只会删除一个匹配的对象 
- 按照指定位置删除 
- 删除任意位置的对象:E remove(int index),当index越界时会抛出异常 
- 删除头节点位置的对象 
- 在链表为空时抛出异常:E remove()、E removeFirst()、E pop() 
- 在链表为空时返回null:E poll()、E pollFirst() 
- 删除尾节点位置的对象 
- 在链表为空时抛出异常:E removeLast() 
- 在链表为空时返回null:E pollLast()

迭代器操作

LinkedList的iterator()方法内部调用了其listIterator()方法,所以可以只分析listIterator()方法。listIterator()提供了两个重载方法。iterator()方法和listIterator()方法的关系如下:

  1. public Iterator<E> iterator() {
  2. return listIterator();
  3. }
  4.  
  5. public ListIterator<E> listIterator() {
  6. return listIterator(0);
  7. }
  8.  
  9. public ListIterator<E> listIterator(int index) {
  10. checkPositionIndex(index);
  11. return new ListItr(index);
  12. }

从上面可以看到三者的关系是iterator()——>listIterator(0)——>listIterator(int index)。最终都会调用listIterator(int index)方法,其中参数表示迭代器开始的位置。在ArrayList源码分析中提到过ListIterator是一个可以指定任意位置开始迭代,并且有两个遍历方法。下面直接看ListItr的实现:

  1. private class ListItr implements ListIterator<E> {
  2. private Node<E> lastReturned;
  3. private Node<E> next;
  4. private int nextIndex;
  5. private int expectedModCount = modCount;//保存当前modCount,确保fail-fast机制
  6.  
  7. ListItr(int index) {
  8. // assert isPositionIndex(index);
  9. next = (index == size) ? null : node(index);//得到当前索引指向的next节点
  10. nextIndex = index;
  11. }
  12.  
  13. public boolean hasNext() {
  14. return nextIndex < size;
  15. }
  16.  
  17. //获取下一个节点
  18. public E next() {
  19. checkForComodification();
  20. if (!hasNext())
  21. throw new NoSuchElementException();
  22.  
  23. lastReturned = next;
  24. next = next.next;
  25. nextIndex++;
  26. return lastReturned.item;
  27. }
  28.  
  29. public boolean hasPrevious() {
  30. return nextIndex > 0;
  31. }
  32.  
  33. //获取前一个节点,将next节点向前移
  34. public E previous() {
  35. checkForComodification();
  36. if (!hasPrevious())
  37. throw new NoSuchElementException();
  38.  
  39. lastReturned = next = (next == null) ? last : next.prev;
  40. nextIndex--;
  41. return lastReturned.item;
  42. }
  43.  
  44. public int nextIndex() {
  45. return nextIndex;
  46. }
  47.  
  48. public int previousIndex() {
  49. return nextIndex - 1;
  50. }
  51.  
  52. public void remove() {
  53. checkForComodification();
  54. if (lastReturned == null)
  55. throw new IllegalStateException();
  56.  
  57. Node<E> lastNext = lastReturned.next;
  58. unlink(lastReturned);
  59. if (next == lastReturned)
  60. next = lastNext;
  61. else
  62. nextIndex--;
  63. lastReturned = null;
  64. expectedModCount++;
  65. }
  66.  
  67. public void set(E e) {
  68. if (lastReturned == null)
  69. throw new IllegalStateException();
  70. checkForComodification();
  71. lastReturned.item = e;
  72. }
  73.  
  74. public void add(E e) {
  75. checkForComodification();
  76. lastReturned = null;
  77. if (next == null)
  78. linkLast(e);
  79. else
  80. linkBefore(e, next);
  81. nextIndex++;
  82. expectedModCount++;
  83. }
  84.  
  85. public void forEachRemaining(Consumer<? super E> action) {
  86. Objects.requireNonNull(action);
  87. while (modCount == expectedModCount && nextIndex < size) {
  88. action.accept(next.item);
  89. lastReturned = next;
  90. next = next.next;
  91. nextIndex++;
  92. }
  93. checkForComodification();
  94. }
  95.  
  96. final void checkForComodification() {
  97. if (modCount != expectedModCount)
  98. throw new ConcurrentModificationException();
  99. }
  100. }

在ListIterator的构造器中,得到了当前位置的节点,就是变量next。next()方法返回当前节点的值并将next指向其后继节点,previous()方法返回当前节点的前一个节点的值并将next节点指向其前驱节点。由于Node是一个双端节点,所以这儿用了一个节点就可以实现从前向后迭代和从后向前迭代。另外在ListIterator初始时,exceptedModCount保存了当前的modCount,如果在迭代期间,有操作改变了链表的底层结构,那么再操作迭代器的方法时将会抛出ConcurrentModificationException。

例子

由于LinkedList是一个实现了Deque的双端队列,所以LinkedList既可以当做Queue,又可以当做Stack,下面的例子将LinkedList成Stack,代码如下:

  1. public class LinkedStack<E> {
  2.  
  3. private LinkedList<E> linkedList;
  4.  
  5. public LinkedStack() {
  6. linkedList = new LinkedList<E>();
  7. }
  8.  
  9. //压入数据
  10. public void push(E e) {
  11. linkedList.push(e);
  12. }
  13.  
  14. //弹出数据,在Stack为空时将抛出异常
  15. public E pop() {
  16. return linkedList.pop();
  17. }
  18.  
  19. //检索栈顶数据,但是不删除
  20. public E peek() {
  21. return linkedList.peek();
  22. }
  23.  
  24. }

在将LinkedList当做Stack时,使用pop()、push()、peek()方法需要注意的是LinkedList内部是将链表头部当做栈顶,链表尾部当做栈底,也就意味着所有的压入、摊入操作都在链表头部进行。

总结

LinkedList是基于双端链表的List,其内部的实现源于对链表的操作,所以适用于频繁增加、删除的情况;该类不是线程安全的;另外,由于LinkedList实现了Queue接口,所以LinkedList不止有队列的接口,还有栈的接口,可以使用LinkedList作为队列和栈的实现。

出处:http://blog.csdn.net/qq_19431333/article/details/54572876

【Java深入研究】2、JDK 1.8 LinkedList源码解析的更多相关文章

  1. Java集合:LinkedList源码解析

    Java集合---LinkedList源码解析   一.源码解析1. LinkedList类定义2.LinkedList数据结构原理3.私有属性4.构造方法5.元素添加add()及原理6.删除数据re ...

  2. LinkedList源码解析

    LinkedList是基于链表结构的一种List,在分析LinkedList源码前有必要对链表结构进行说明.1.链表的概念链表是由一系列非连续的节点组成的存储结构,简单分下类的话,链表又分为单向链表和 ...

  3. Java 集合系列07之 Stack详细介绍(源码解析)和使用示例

    概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ...

  4. 【转】 Java 集合系列07之 Stack详细介绍(源码解析)和使用示例

    概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ...

  5. Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例

    概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...

  6. Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例

    概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...

  7. 【转】Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例

    概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...

  8. 给jdk写注释系列之jdk1.6容器(2)-LinkedList源码解析

    LinkedList是基于链表结构的一种List,在分析LinkedList源码前有必要对链表结构进行说明.   1.链表的概念      链表是由一系列非连续的节点组成的存储结构,简单分下类的话,链 ...

  9. Java集合---LinkedList源码解析

    一.源码解析1. LinkedList类定义2.LinkedList数据结构原理3.私有属性4.构造方法5.元素添加add()及原理6.删除数据remove()7.数据获取get()8.数据复制clo ...

随机推荐

  1. GPU、CPU、FPGA

    一.计算核心增加 二者都由寄存器.控制器.逻辑单元构成,但比例很大不同,决定了CPU擅长指令处理,函数调用上:GPU在数据处理(算数运算/逻辑运算)强力很多. NIVIDA基于Maxwell构架的GP ...

  2. android-glsurfaceview Activity框架程序

    两个基本的类让我们使用OpenGL ES API来创建和操纵图形:GLSurfaceView和 GLSurfaceView.Renderer. 1. GLSurfaceView: 这是一个视图类,你可 ...

  3. Python logging模块简介

    logging模块提供logger,handler,filter,formatter. logger:提供日志接口,供应用代码使用.logger最长用的操作有两类:配置和发送日志消息.可以通过logg ...

  4. bash编程-cut、printf

    1. cut cut命令用于从行中截取字符串. SYNOPSIS cut OPTION... [FILE]... ** OPTION** -d CHAR:以指定的字符为分隔符(不要用空格作为分隔符): ...

  5. 一个友盟BUG的思考和分析:Invalid update

    1.友盟错误信息 Invalid update: invalid number of rows . The number of rows contained ) must be equal to th ...

  6. VS从数据库表生成Model代码

    1.工具——扩展和更新——安装下列插件 2.如图所示,在项目或者MODEL文件夹下添加 3.如图所示,生成了一个datanase.11 4.打开该文件后,将数据库连接字符串改为你自己项目中WebCof ...

  7. 7.ASP.NET MVC 5.0中的Routing【路由】

    大家好,这一篇向大家介绍ASP.NET MVC路由机制.[PS:上一篇-->6. ASP.NET MVC 5.0中的HTML Helpers[HTML帮助类] ] 路由是一个模式匹配系统,它确保 ...

  8. Media Queries 媒体查询常见设备断点

    按需调整断点 一.谷歌后摘抄的一部分媒体查询 /*#region SmartPhones */ /* SmartPhones */@media only screen and (min-device- ...

  9. 在虚拟机上安装redis集群,redis使用版本为4.0.5,本机通过命令客户端可以连接访问,外部主机一直访问不了

    在虚拟机上安装了redis 4 ,启动后本机客户端可以连接访问,但是外部主机一直访问不了,在使用java代码连接redis集群时报:no reachable node in cluster,原因:在r ...

  10. 安装 composer 并启动 yii2 项目

    环境 MacOS 10.12.6 PHP 5.6.30 yii2.0 一.composer (类似 node's npm) 1.安装 php -r "copy('https://getcom ...