-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIteratorTest.java
More file actions
164 lines (148 loc) · 3.24 KB
/
Copy pathIteratorTest.java
File metadata and controls
164 lines (148 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package Iterator;
import java.lang.reflect.Array;
/**
* 迭代模式测试
* @author baitp
*
*/
public class IteratorTest {
public static void main(String[] args) {
/*ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("ni");
arrayList.add("hao");
AbIterator<String> it = arrayList.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}*/
LinkList<String> linkList = new LinkList<>();
linkList.add("ni");
linkList.add("hao");
AbIterator<String> it2 = linkList.iterator();
while(it2.hasNext()) {
System.out.println(it2.next());
}
}
}
/**
* 抽象集合,相当于Aggregate角色
* @author baitp
*/
interface AbList<T>{
public AbIterator<T> iterator();
}
/**
* 抽象迭代器,相当于Iterator角色
* @author baitp
*/
interface AbIterator<T>{
public boolean hasNext();
public T next();
}
/**
* 具体集合1,相当于ConcreteAggregate角色
* 内部使用数组
* @author baitp
*/
class ArrayList<T> implements AbList<T>{
private T[] ts;
private int defaultSize = 10;
private int realSize = 0;
private int addIndex = 0;
public ArrayList() {}
public ArrayList(int size) {
realSize = size;
}
@SuppressWarnings("unchecked")
public void add(T t) {
if(null == ts) {
ts = (T[])Array.newInstance(t.getClass(), 0 == realSize?defaultSize:realSize);
}
ts[addIndex++] = t;
addIndex %= ts.length;
}
@Override
public AbIterator<T> iterator() {
return new ArrayListIterator<T>();
}
/**
* 可以访问到具体集合的迭代器,一般使用内部类实现
* 不一定要持有集合,只要可以访问到集合对象即可,jdk的ArrayList也是使用此种方式实现。
* @author baitp
*/
class ArrayListIterator<E> implements AbIterator<E>{
private int index = 0;
@Override
public boolean hasNext() {
return index != ts.length-1 && null != ts[index];
}
@SuppressWarnings("unchecked")
@Override
public E next() {
return (E) ts[index++];
}
}
}
/**
* 具体集合2,相当于ConcreteAggregate角色
* 内部使用链表
* @author baitp
*/
class LinkList<T> implements AbList<T>{
Node<T> head;
Node<T> index;
public void add(T value) {
if(null == head) {
index = (head = new Node<T>(value,null));
}else {
index.next = new Node<T>(value,null);
index = index.next;
}
}
@Override
public AbIterator<T> iterator() {
return new LinkListIterator<T>();
}
/**
* 可以访问到具体集合的迭代器,一般使用内部类实现
* @author baitp
*/
class LinkListIterator<E> implements AbIterator<E>{
@SuppressWarnings("unchecked")
private Node<E> itIndex = (LinkList<T>.Node<E>) head;
@Override
public boolean hasNext() {
return itIndex != null;
}
@Override
public E next() {
E value = itIndex.getValue();
itIndex = itIndex.next;
return value;
}
}
/**
* 链表的节点类
* @author baitp
*/
class Node<E>{
E value;
Node<E> next;
public E getValue() {
return value;
}
public void setValue(E value) {
this.value = value;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
public Node(E value, LinkList<T>.Node<E> next) {
super();
this.value = value;
this.next = next;
}
}
}