-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterator.java
More file actions
340 lines (283 loc) · 8.61 KB
/
Iterator.java
File metadata and controls
340 lines (283 loc) · 8.61 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package savetime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
class LastCheckIterator<T> implements Iterator<T> {
Iterator<T> iterator;
T current;
LastCheckIterator(Iterator<T> iterator) {
this.iterator = iterator;
}
public boolean hasNext() {
if (current != null)
return true;
if (iterator.hasNext()) {
current = iterator.next();
return true;
}
return false;
}
public T next() {
if (hasNext()) {
T toReturn = current;
if (iterator.hasNext()) {
current = iterator.next();
} else {
current = null;
}
return toReturn;
}
throw new NoSuchElementException("");
}
public boolean isLast() {
return !(iterator.hasNext());
}
public void remove() {
throw new UnsupportedOperationException("");
}
}
class PrevIterator<T> implements Iterator<T> {
Iterator<T> iterator;
T prevItem;
public PrevIterator(Iterator<T> iterator) {
this.iterator = iterator;
}
public boolean hasNext() {
return iterator.hasNext();
}
public T next() {
if (!hasNext()) {
throw new NoSuchElementException("");
}
prevItem = iterator.next();
return prevItem;
}
public void remove() {
throw new UnsupportedOperationException("");
}
public T previous() {
try {
if (prevItem == null)
throw new NoSuchElementException("");
} catch (Exception ex) {
ex.printStackTrace();
}
return prevItem;
}
}
class EvenIterator<T> implements Iterator<T> {
public Iterator<T> iterator;
T evenItem;
public EvenIterator(Iterator<T> iterator) {
this.iterator = iterator;
}
public boolean hasNext() {
if (evenItem != null)
return true;
else {
if (iterator.hasNext()) {
iterator.next();
if (iterator.hasNext()) {
evenItem = iterator.next();
return true;
}
}
return false;
}
}
public T next() {
if (evenItem == null) {
throw (new NoSuchElementException("Iterator has no elements left."));
}
T toreturn = evenItem;
evenItem = null;
if (iterator.hasNext()) {
iterator.next();
if (iterator.hasNext())
evenItem = iterator.next();
}
return toreturn;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
class RotateIterator<T> implements Iterator<T> {
public List<Iterator<T>> lit;
int index;
public RotateIterator(List<Iterator<T>> lit) {
this.lit = lit;
index = 0;
}
public boolean hasNext() {
if (lit.size() == 0) {
throw (new NoSuchElementException("Iterator has no elements left."));
}
int origin = index;
do {
if (lit.get(index).hasNext()) {
return true;
}
index = (index + 1) % (lit.size());
} while (index != origin);
return false;
}
public T next() {
if (!hasNext()) {
throw (new NoSuchElementException("Iterator has no elements left."));
}
T toreturn = lit.get(index).next();
index = (index + 1) % (lit.size());
return toreturn;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
class JumpIterator<T> implements Iterator<T> {
private final Iterator<T> iterator;
public JumpIterator(Iterator<T> iterator) {
this.iterator = iterator;
}
public boolean hasNext() {
return iterator.hasNext();
}
public T next() {
if (iterator.hasNext()) {
T toreturn = iterator.next();
if (iterator.hasNext()) {
iterator.next();
}
return toreturn;
}
throw (new NoSuchElementException("Iterator has no elements left."));
}
public void remove() {
throw new UnsupportedOperationException();
}
}
class NegativeIterator<T> implements Iterator<T> {
private Iterator<T> iterator;
private T toReturn;
public NegativeIterator(Iterator<T> iterator) {
this.iterator = iterator;
toReturn = null;
}
public boolean hasNext() {
if (toReturn != null)
return true;
while (iterator.hasNext()) {
toReturn = iterator.next();
if ((Integer) toReturn < 0)
return true;
}
toReturn = null;
return false;
}
public T next() {
if (hasNext()) {
T reserve = toReturn;
toReturn = null;
return reserve;
}
throw new NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException();
}
}
public class PeekIterator<T> implements Iterator<T> {
private final Iterator<T> iterator;
private T nextitem;
public PeekIterator(Iterator<T> iterator) {
this.iterator = iterator;
}
public boolean hasNext() {
if (nextitem != null) {
return true;
}
if (iterator.hasNext()) {
nextitem = iterator.next();
}
return nextitem != null;
}
public T next() {
if (!hasNext()) {
throw (new NoSuchElementException("Iterator has no elements left."));
}
T toReturn = nextitem;
nextitem = null;
return toReturn;
}
public T peek() {
if (!hasNext()) {
throw (new NoSuchElementException("Iterator has no elements left."));
}
return nextitem;
}
public void remove() {
throw new UnsupportedOperationException();
}
public static void main(String[] args) {
List<Integer> l = new ArrayList<Integer>();
for (int i = 0; i < 0; i++)
l.add(i);
//
// PeekIterator<Integer> pI=new PeekIterator<Integer>(l.iterator());
// while (pI.hasNext()) {
// System.out.println(pI.hasNext());
// System.out.println(pI.hasNext());
// System.out.println(pI.next());
// }
//
// System.out.println("--------------------");
//
// JumpIterator<Integer> jI=new JumpIterator<Integer>(l.iterator());
// while (jI.hasNext()) {
// System.out.println(jI.hasNext());
// System.out.println(jI.hasNext());
// System.out.println(jI.next());
// }
//
// System.out.println("--------------------");
// List<Iterator<Integer>> lit = new ArrayList<Iterator<Integer>>();
// int count = 1;
// for (int i = 0; i < 4; i++) {
// List<Integer> tmp = new ArrayList<Integer>();
// for (int j = 0; j < 3; j++)
// tmp.add(count++);
// lit.add(tmp.iterator());
// }
// RotateIterator<Integer> rI = new RotateIterator<Integer>(lit);
// while (rI.hasNext()) {
// System.out.println(rI.next());
// }
// System.out.println("--------------------");
//
// EvenIterator<Integer> eI = new EvenIterator<Integer>(l.iterator());
// while (eI.hasNext()) {
// System.out.println(eI.hasNext());
// System.out.println(eI.hasNext());
// System.out.println(eI.next());
// }
// System.out.println("--------------------");
//
// NegativeIterator<Integer> pI = new
// NegativeIterator<Integer>(l.iterator());
// while (pI.hasNext()) {
// System.out.println(pI.hasNext());
// System.out.println(pI.next());
// }
System.out.println("--------------------");
LastCheckIterator<Integer> pI = new LastCheckIterator<Integer>(
l.iterator());
System.out.println(pI.isLast());
while (pI.hasNext()) {
System.out.println(pI.hasNext());
System.out.println(pI.isLast());
System.out.println(pI.next());
}
System.out.println("end");
}
}