|
1 |
| -classVector2DimplementsIterator<Integer> { |
| 1 | +classVector2D { |
| 2 | +intvectorIdx; |
| 3 | +intcurrIdx; |
| 4 | +int[][]v; |
| 5 | +publicVector2D(int[][]v) { |
| 6 | +vectorIdx =0; |
| 7 | +currIdx =0; |
| 8 | +this.v =v; |
| 9 | + } |
2 | 10 |
|
3 |
| -Iterator<List<Integer>>listIterator; |
4 |
| -Iterator<Integer>iterator; |
5 |
| -booleanflag; |
| 11 | +publicintnext() { |
| 12 | +hasNext(); |
| 13 | +returnv[vectorIdx][currIdx++]; |
| 14 | + } |
6 | 15 |
|
7 |
| -publicVector2D(List<List<Integer>>vec2d) { |
8 |
| -listIterator =vec2d.iterator(); |
9 |
| -check(); |
10 |
| - } |
11 |
| - |
12 |
| -privatevoidcheck() { |
13 |
| -while (listIterator.hasNext()) { |
14 |
| -List<Integer>list =listIterator.next(); |
15 |
| -if (list.size() >0) { |
16 |
| -iterator =list.iterator(); |
17 |
| -break; |
18 |
| - } |
19 |
| - } |
20 |
| - |
21 |
| -flag =iterator !=null; |
22 |
| - } |
23 |
| - |
24 |
| -@Override |
25 |
| -publicIntegernext() { |
26 |
| -intnum =iterator.next(); |
27 |
| -if (!iterator.hasNext()) { |
28 |
| -iterator =null; |
29 |
| -check(); |
30 |
| - } |
31 |
| - |
32 |
| -returnnum; |
33 |
| - } |
34 |
| - |
35 |
| -@Override |
36 |
| -publicbooleanhasNext() { |
37 |
| -returnflag; |
| 16 | +publicbooleanhasNext() { |
| 17 | +while (vectorIdx <v.length) { |
| 18 | +if (currIdx <v[vectorIdx].length) { |
| 19 | +returntrue; |
| 20 | + } |
| 21 | +vectorIdx++; |
| 22 | +currIdx =0; |
38 | 23 | }
|
| 24 | +returnfalse; |
| 25 | + } |
39 | 26 | }
|
| 27 | + |
40 | 28 | /**
|
41 | 29 | * Your Vector2D object will be instantiated and called as such:
|
42 |
| - * Vector2D i = new Vector2D(vec2d); |
43 |
| - * while (i.hasNext()) v[f()] = i.next(); |
| 30 | + * Vector2D obj = new Vector2D(v); |
| 31 | + * int param_1 = obj.next(); |
| 32 | + * boolean param_2 = obj.hasNext(); |
44 | 33 | */
|