-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListIterator.java
More file actions
107 lines (92 loc) · 2.27 KB
/
ArrayListIterator.java
File metadata and controls
107 lines (92 loc) · 2.27 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
package Algorithms;
import java.util.ArrayList;
import java.util.List;
/*
* An iterator class for list of list
*
* It will be calld like below:
*
* ArrayListIterator iter = new ArrayListIterator(input);
* while (iter.hasNext()) {
* int num = iter.next();
* if (num % 2 == 0) {
* iter.remove(); //it will remove the former element
* }
* }
*/
class ArrayListIterator {
List<List<Integer>> input = new ArrayList<List<Integer>>();
int x, y;
class EmptyArrayListException extends Exception {
}
ArrayListIterator(List<List<Integer>> input) {
this.input = input;
this.x = 0;
this.y = 0;
}
public boolean hasNext() {
while (x < input.size()) {
if (y < input.get(x).size()) {
return true;
} else {
x ++;
y = 0;
}
}
return false;
}
public int next() throws EmptyArrayListException {
if (hasNext()) {
y ++;
return input.get(x).get(y - 1);
} else {
throw new EmptyArrayListException();
}
}
public void remove() throws EmptyArrayListException {
int tmp_x = x, tmp_y = y - 1;
while (tmp_x >= 0) {
if (tmp_y >= 0) {
input.get(tmp_x).remove(tmp_y);
break;
} else {
tmp_x --;
tmp_y = input.get(tmp_x).size() - 1;
}
}
if (tmp_x == x) {
y --;
}
if (tmp_x == -1) {
throw new EmptyArrayListException();
}
}
public static void main(String[] args) throws EmptyArrayListException {
List<List<Integer>> input = new ArrayList<List<Integer>>();
List<Integer> tmp1 = new ArrayList<Integer>();
List<Integer> tmp2 = new ArrayList<Integer>();
List<Integer> tmp3 = new ArrayList<Integer>();
List<Integer> tmp4 = new ArrayList<Integer>();
tmp1.add(1);
tmp3.add(2);
tmp3.add(3);
tmp4.add(4);
input.add(tmp1);
input.add(tmp2);
input.add(tmp3);
input.add(tmp4);
ArrayListIterator test = new ArrayListIterator(input);
while (test.hasNext()) {
int num = test.next();
if (num % 2 == 0) {
test.remove();
}
}
ArrayListIterator test1 = new ArrayListIterator(input);
while (test1.hasNext()) {
System.out.println(test1.next());
}
//throw an excpetion in this case
test.next();
}
}