Iterator для двумерного массива метод hasnext

Пишу итератор для двумерного массива int никак не пойму как надо написать метод hasnext() буду благодарен за помощь

public class MatrixIt implements Iterator<Integer> {

    private int[][] array;
    private int row;
    private int col;

    public MatrixIt(int[][] array) {
        this.array = array;
    }

    @Override
    public boolean hasNext() {
        return false;
    }

    @Override
    public Integer next() {
        if (array[row].length == 0) {
            while (array[row].length == 0) {
                row++;
                col = 0;
            }
        }
        Integer result = array[row][col++];
        if (col >= array[row].length) {
            row++;
            col = 0;
        }
        return result;
    }
}

тесты

 @Test
    public void when4El() {
        int[][] in = {{1}};
        MatrixIt it = new MatrixIt(in);
        assertThat(it.next(), is(1));
    }

    @Test
    public void whenFirstEmptyThenNext() {
        int[][] in = {{}, {1}};
        MatrixIt it = new MatrixIt(in);
        assertThat(it.next(), is(1));
    }

    @Test
    public void whenFirstEmptyThenHashNext() {
        int[][] in = {{}, {1}};
        MatrixIt it = new MatrixIt(in);
        assertThat(it.hasNext(), is(true));
    }

    @Test
    public void whenRowHasDiffSize() {
        int[][] in = {{1}, {2, 3}};
        MatrixIt it = new MatrixIt(in);
        assertThat(it.next(), is(1));
        assertThat(it.next(), is(2));
        assertThat(it.next(), is(3));
    }

    @Test
    public void whenFewEmpty() {
        int[][] in = {{1}, {}, {}, {}, {2}};
        MatrixIt it = new MatrixIt(in);
        assertThat(it.next(), is(1));
        assertThat(it.next(), is(2));
    }

    @Test
    public void whenEmpty() {
        int[][] in = {{}};
        MatrixIt it = new MatrixIt(in);
        assertThat(it.hasNext(), is(false));
    }

    @Test(expected = NoSuchElementException.class)
    public void whenEmptyThenNext() {
        int[][] in = {{}};
        MatrixIt it = new MatrixIt(in);
        it.next();
    }

    @Test
    public void whenMultiHashNext() {
        int[][] in = {{}, {1}};
        MatrixIt it = new MatrixIt(in);
        assertThat(it.hasNext(), is(true));
        assertThat(it.hasNext(), is(true));
    }

    @Test
    public void whenNoElements() {
        int[][] in = {{}, {}, {}};
        MatrixIt it = new MatrixIt(in);
        assertThat(it.hasNext(), is(false));
    }

Ответы (2 шт):

Автор решения: Daniil Loban

Создать локальные переменные, если они не выходят за границы массива при next,

значит hasNext() == true;

  @Override
  public boolean hasNext() {
    int locRow = row;
    int locCol = col;
    if (array[locRow].length == 0) {
        while (array[locRow].length == 0) {
            locRow++;
            locCol = 0;
        }
    }
    if (++locCol >= array[locRow].length) {
        locRow++;
        locCol = 0;
    } 
    return (locRow < array.length && locCol < array[locRow].length);
  }

и тест:

  @Test
  public void whenEmpty() {
      int[][] in = {{1}, {1}};
      MatrixIt it = new MatrixIt(in);
      assertThat(it.hasNext(), is(true));
      it.next();
      assertThat(it.hasNext(), is(false));
  }
→ Ссылка
Автор решения: nPaBllla
@Override
public boolean hasNext() {
    while (row < array.length && array[row].length == col) {
        row++;
        col = 0;
    }
    return row < array.length;
}

@Override
public Integer next() {
    if (!hasNext()) {
        throw new NoSuchElementException();
    }
    return array[row][col++];
}
→ Ссылка