设计一个迭代器来实现摊平二维向量的功能
样例
例1:
输入:[[1,2],[3],[4,5,6]]
输出:[1,2,3,4,5,6]
例2:
输入:[[7,9],[5]]
输出:[7,9,5]
思路
使用一个 list 存储即可
public class Vector2D implements Iterator<Integer> {
private List<Integer> list;
private int count;
public Vector2D(List<List<Integer>> vec2d) {
// Initialize your data structure here
list = new ArrayList<>();
for (List<Integer> integers : vec2d) {
for (Integer item : integers) {
list.add(item);
}
}
}
@Override
public Integer next() {
// Write your code here
return list.get(count++);
}
@Override
public boolean hasNext() {
// Write your code here
return count < list.size();
}
@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();
*/