forked from zouldapp/JavaWorkshopCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIterator.java
executable file
·44 lines (39 loc) · 945 Bytes
/
Iterator.java
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
public class Iterator {
private int first;
private int step;
private int index;
public Iterator(int first, int step, int index) {
this.first = first;
this.step = step;
this.index = index;
}
public void next() {
if ( over() ) {
return;
}
first += step;
index -= 1;
}
public void prev() {
if ( over() ) {
return;
}
first -= step;
index -= 1;
}
public boolean over() {
return index == 0;
}
public int value() {
return first;
}
public static void main(String[] args) {
Iterator iterator = new Iterator(10, 2, 4);
for ( ; !iterator.over(); iterator.next() ) {
System.out.println(iterator.value());
}
for ( ; !iterator.over(); iterator.prev() ) {
System.out.println(iterator.value());
}
}
}