36 lines
723 B
Java
36 lines
723 B
Java
package oving5.stringgrid;
|
|
|
|
public class StringGridIterator {
|
|
private StringGrid grid;
|
|
private boolean rowMajor;
|
|
private int i = -1;
|
|
|
|
public StringGridIterator(StringGrid grid, boolean rowMajor) {
|
|
this.grid = grid;
|
|
this.rowMajor = rowMajor;
|
|
}
|
|
|
|
public boolean hasNext() {
|
|
return (i + 1) < (grid.getRowCount() * grid.getColumnCount());
|
|
}
|
|
|
|
|
|
|
|
public String next() {
|
|
if (hasNext()) {
|
|
i += 1;
|
|
if (rowMajor) {
|
|
return grid.getElement(i / grid.getColumnCount(), i % grid.getColumnCount());
|
|
} else {
|
|
return grid.getElement(i % grid.getRowCount(), i / grid.getRowCount());
|
|
}
|
|
}
|
|
throw new IllegalArgumentException();
|
|
}
|
|
|
|
public void remove() {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
|
|
}
|