package uebung09.iterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class Array2dIterator<T> implements Iterator<T>{

    private T[][] array;
    private int row = 0;
    private int col = 0;

    public Array2dIterator(T[][] array) {
        this.array = array;
    }

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

    @Override
    public T next() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        return array[row][col++];
    }
}
