uebung09
This commit is contained in:
38
uebung09/src/iterator/Array2dIterator.java
Normal file
38
uebung09/src/iterator/Array2dIterator.java
Normal file
@@ -0,0 +1,38 @@
|
||||
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++];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user