39 lines
782 B
Java
39 lines
782 B
Java
package iterator;
|
|
|
|
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() {
|
|
if (row < array.length) {
|
|
if (col >= array[row].length) {
|
|
col = 0;
|
|
row++;
|
|
}
|
|
}
|
|
if (row < array.length && col < array[row].length) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public T next() {
|
|
if (!hasNext()) {
|
|
throw new NoSuchElementException();
|
|
}
|
|
|
|
return array[row][col++];
|
|
}
|
|
}
|