
public class Vector2D{ private int[][] matrix; private int row; private int col; private boolean curUse; public Vector2D(int[][] v) { matrix = v; row = 0; col = -1; curUse = true; hasNext(); } public int next() { int ans = matrix[row][col]; curUse = true; hasNext(); return ans; } public boolean hasNext() { if (row == matrix.length) { return false; } // 重复多次调用弄hasNext的时候 if (!curUse) { return true; } //(row, col)已经用过了 if (col < matrix[row].length) { col++; } else { col = 0; do { row++; } while (row < matrix.length && matrix[row].length == 0); } //新的row, col if (row != matrix.length) { curUse = false; return true; } else { return false; } }}