Implement an iterator to flatten a 2d vector.

For example,
Given 2d vector =

1
2
3
4
5
[
[1,2],
[3],
[4,5,6]
]

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,2,3,4,5,6].

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
public class Vector2D implements Iterator<Integer> {
Iterator<List<Integer>> iter;
Iterator<Integer> cur;

public Vector2D(List<List<Integer>> vec2d) {
iter = vec2d.iterator();
cur = iter.hasNext() ? iter.next().iterator() : null;
}

@Override
public Integer next() {
hasNext();
return cur.next();
}

@Override
public boolean hasNext() {
// using while is neccesary for case like [[], [], [-1]]
// note double paranthesis below
while ((cur == null || !cur.hasNext()) && iter.hasNext()){
cur = iter.next().iterator();
}
return cur != null && cur.hasNext();
}
}

/**
* Your Vector2D object will be instantiated and called as such:
* Vector2D i = new Vector2D(vec2d);
* while (i.hasNext()) v[f()] = i.next();
*/