By calling next repeatedly until hasNext returns false, the order of elements returned by next should be:[1,2,3,4,5,6].
2.Code
public class Vector2D implements Iterator<Integer> {
public Vector2D(List<List<Integer>> vec2d) {
// Initialize your data structure here
}
@Override
public Integer next() {
// Write your code here
}
@Override
public boolean hasNext() {
// Write your code here
}
@Override
public void remove() {}
}
/**
* Your Vector2D object will be instantiated and called as such:
* Vector2D i = new Vector2D(vec2d);
* while (i.hasNext()) v[f()] = i.next();
*/
Hint:
How many variables do you need to keep track?
Two variables is all you need. Try with x and y
Beware of empty rows. It could be the first few rows.
The invariant is xand ymust always point to a valid point in the 2d vector. Should you maintain your invariant
ahead of time _or _right when you need it?
Not sure? Think about how you would implement hasNext(). Which is more complex?
Common logic in two different places should be refactored into a common method.