您的位置:首页 > 编程语言 > Java开发

内功心法 -- java.util.LinkedList<E> (8)

2017-02-15 22:00 585 查看
写在前面的话:读书破万卷,编码如有神
--------------------------------------------------------------------
下文主要对java.util.LinkedList<E>的Iterator操作和数组、克隆操作进行介绍,主要内容包括:

1、LinkedList常用的Iterator操作介绍

2、LinkedList常用的数组和克隆操作进行介绍

参考内容:

1、JDK源码(1.7)

--------------------------------------------------------------------

1、LinkedList常用的Iterator操作介绍

(1) ListIterator<E> listIterator(int index)

功能: 返回此双端队列的ListIterator对象

源代码如下:

1     /*
2         返回此双端队列的ListIterator对象
3     */
4     public ListIterator<E> listIterator(int index) {
5         //检查参数index是合法
6         checkPositionIndex(index);
7         //创建一个ListItr对象
8         return new ListItr(index);
9     }
10
11     /*
12         内部类ListItr
13     */
14     private class ListItr implements ListIterator<E> {
15         //记录最后一次返回的Node节点
16         private Node<E> lastReturned = null;
17         //记录下一次要返回的Node节点
18         private Node<E> next;
19         //记录下一次要返回Node节点的索引
20         private int nextIndex;
21         //fast-fail机制
22         private int expectedModCount = modCount;
23
24         //构造函数
25         ListItr(int index) {
26             // assert isPositionIndex(index);
27             next = (index == size) ? null : node(index);
28             nextIndex = index;
29         }
30
31         //判断是否还有下一个节点
32         public boolean hasNext() {
33             return nextIndex < size;
34         }
35
36         //返回下一个Node节点
37         public E next() {
38             //检查fast-fail机制
39             checkForComodification();
40             //如果没有下一个节点了,则抛出异常
41             if (!hasNext())
42                 throw new NoSuchElementException();
43             //记录最后返回的节点
44             lastReturned = next;
45             //指向下一个节点
46             next = next.next;
47             //索引下标加1
48             nextIndex++;
49             //返回节点
50             return lastReturned.item;
51         }
52
53         //判断是否有上一个节点
54         public boolean hasPrevious() {
55             return nextIndex > 0;
56         }
57
58         //返回上一个Node节点
59         public E previous() {
60             //检查fast-fail机制
61             checkForComodification();
62             //如果没有上一个节点了,则抛出异常
63             if (!hasPrevious())
64                 throw new NoSuchElementException();
65             //记录最后返回的节点
66             lastReturned = next = (next == null) ? last : next.prev;
67             //索引下标减1
68             nextIndex--;
69             //返回节点
70             return lastReturned.item;
71         }
72
73         //返回下一个返回节点的索引下标
74         public int nextIndex() {
75             return nextIndex;
76         }
77
78          //返回上一个返回节点的索引下标
79         public int previousIndex() {
80             return nextIndex - 1;
81         }
82
83         //删除最后返回的Node节点
84         public void remove() {
85             //检查fast-fail机制
86             checkForComodification();
87             //如果没有返回过Node节点,则抛出异常
88             if (lastReturned == null)
89                 throw new IllegalStateException();
90
91             //删除节点,并且重新设置连接
92             Node<E> lastNext = lastReturned.next;
93             unlink(lastReturned);
94             if (next == lastReturned)
95                 next = lastNext;
96             else
97                 nextIndex--;
98             lastReturned = null;
99             expectedModCount++;
100         }
101
102         //将最后返回的Node节点的值设置为e
103         public void set(E e) {
104             if (lastReturned == null)
105                 throw new IllegalStateException();
106             checkForComodification();
107             lastReturned.item = e;
108         }
109
110         //将元素e添加到此双端队列中
111         public void add(E e) {
112             //检查fast-fail机制
113             checkForComodification();
114             lastReturned = null;
115             if (next == null)
116                 linkLast(e);
117             else
118                 linkBefore(e, next);
119             nextIndex++;
120             expectedModCount++;
121         }
122         //检查fast-fail机制
123         final void checkForComodification() {
124             if (modCount != expectedModCount)
125                 throw new ConcurrentModificationException();
126         }
127     }


(2) Iterator<E> descendingIterator()

功能: 返回此双端队列的Iteraotr对象(逆序)

源代码如下:

1     /*
2         功能: 返回此双端队列的Iterator对象(逆序)
3     */
4     public Iterator<E> descendingIterator() {
5         //创建一个DescendingIterator对象
6         return new DescendingIterator();
7     }
8
9     /**
10      * 返回此双端队列的Iterator对象(逆序)
11      */
12     private class DescendingIterator implements Iterator<E> {
13         //创建一个ListItr对象
14         private final ListItr itr = new ListItr(size());
15
16         //判断是否还有下一个元素
17         public boolean hasNext() {
18             //逆序
19             return itr.hasPrevious();
20         }
21
22         //返回下一个元素
23         public E next() {
24             return itr.previous();
25         }
26
27         //删除元素
28         public void remove() {
29             itr.remove();
30         }
31     }


--------------------------------------------------------------------

2、LinkedList常用的数组和克隆操作进行介绍

(1) Object clone()

功能: 复制此双端队列的对象

源代码如下:

1     /*
2        返回一个此双端队列的浅复制
3     */
4     public Object clone() {
5         LinkedList<E> clone = superClone();
6
7         // Put clone into "virgin" state
8         clone.first = clone.last = null;
9         clone.size = 0;
10         clone.modCount = 0;
11
12         // Initialize clone with our elements
13         for (Node<E> x = first; x != null; x = x.next)
14             clone.add(x.item);
15
16         return clone;
17     }
18
19     /*
20        返回一个此双端队列的浅复制
21     */
22     @SuppressWarnings("unchecked")
23     private LinkedList<E> superClone() {
24         try {
25             return (LinkedList<E>) super.clone();
26         } catch (CloneNotSupportedException e) {
27             throw new InternalError();
28         }
29     }


(2) Object[] toArray()

功能: 将此双端队列中的元素以数组的形式返回

源代码如下:

1     /*
2        将此双端队列中的元素以数组的形式返回
3     */
4     public Object[] toArray() {
5         //创建一个Object类型的数组
6         Object[] result = new Object[size];
7         int i = 0;
8         //循环为数组赋值
9         for (Node<E> x = first; x != null; x = x.next)
10             result[i++] = x.item;
11         return result;
12     }


(3) T[] toArray(T[] a)

功能: 将此双端队列中的元素以数组的形式返回

源代码如下:

1     @SuppressWarnings("unchecked")
2     public <T> T[] toArray(T[] a) {
3         if (a.length < size)
4             a = (T[])java.lang.reflect.Array.newInstance(
5                                 a.getClass().getComponentType(), size);
6         int i = 0;
7         Object[] result = a;
8         for (Node<E> x = first; x != null; x = x.next)
9             result[i++] = x.item;
10
11         if (a.length > size)
12             a[size] = null;
13
14         return a;
15     }


--------------------------------------------------------------------

java.util.LinkedList<E>系列文章

java.util.LinkedList<E>(1)  java.util.LinkedList<E>(2)  java.util.LinkedList<E>(3)

java.util.LinkedList<E>(4)  java.util.LinkedList<E>(5)  java.util.LinkedList<E>(6)

java.util.LinkedList<E>(7)  java.util.LinkedList<E>(8)  

--------------------------------------------------------------------

相关知识

java.util.Collection<E>   java.util.AbstractCollection<E>   java.util.List<E>

java.util.AbstractList<E>   java.util.Iterator<E>   java.util.ListIterator<E>

Java中的标记接口   迭代器模式   Java中的深拷贝和浅拷贝 java.util.Arrays

java.util.Queue<E>  java.util.Deque<E>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: