Flatten 2D Vector
Implement an iterator to flatten a 2d vector.
For example, Given 2d vector =
[
[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].
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.
To write correct code, think about the invariant to maintain. What is it?
The invariant is x and y must 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.
Tips:
就是判断有没有到这一行的尾部以及有没有到最后。注意边界。
Code:
public class Vector2D implements Iterator<Integer> {
int x;
int y;
List<List<Integer>> vec2d;
public Vector2D(List<List<Integer>> vec2d) {
y = 0;
x = 0;
this.vec2d = vec2d;
}
@Override
public Integer next() {
return vec2d.get(y).get(x++);
}
@Override
public boolean hasNext() {
while (y < vec2d.size()) {
if (x < vec2d.get(y).size()) {
return true;
} else {
y++;
x = 0;
}
}
return false;
}
}
/**
* Your Vector2D object will be instantiated and called as such:
* Vector2D i = new Vector2D(vec2d);
* while (i.hasNext()) v[f()] = i.next();
*/