finished uebung09

This commit is contained in:
Johannes Schmelz 2024-06-15 16:20:39 +02:00
parent 62da4a9ea5
commit ac1cc19a88
4 changed files with 33 additions and 17 deletions

Binary file not shown.

View File

@ -3,6 +3,7 @@ package uebung09.chess;
import java.util.Iterator; import java.util.Iterator;
import uebung09.iterator.Array2dIterator; import uebung09.iterator.Array2dIterator;
import uebung09.iterator.SkipNullIterator;
public class Board implements Iterable<Piece>{ public class Board implements Iterable<Piece>{
@ -50,22 +51,21 @@ public class Board implements Iterable<Piece>{
} }
public String toString() { public String toString() {
// StringBuilder s = new StringBuilder("["); StringBuilder s = new StringBuilder("[");
// for(Piece p : this) { for(Piece p : this) {
// s.append(p.toString()); s.append(p.toString());
// } }
// s.append("]"); s.append("]");
// return s.toString(); return s.toString();
return "";
} }
@Override @Override
public Iterator<Piece> iterator() { public Iterator<Piece> iterator() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return new Array2dIterator<>(field); return new SkipNullIterator<>(new Array2dIterator<>(field));
// throw new UnsupportedOperationException("Unimplemented method 'iterator'"); // throw new UnsupportedOperationException("Unimplemented method 'iterator'");
} }
} }

View File

@ -1,26 +1,42 @@
package uebung09.iterator; package uebung09.iterator;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.NoSuchElementException;
public class SkipNullIterator<T> implements Iterator<T>{ public class SkipNullIterator<T> implements Iterator<T>{
private final Iterator<T> iterator;
private T nextItem;
private boolean hasNext;
public SkipNullIterator(Iterator i){ public SkipNullIterator(Iterator<T> iterator) {
this.iterator = iterator;
advance();
}
private void advance() {
hasNext = false;
nextItem = null;
while (iterator.hasNext()) {
nextItem = iterator.next();
if (nextItem != null) {
hasNext = true;
break;
}
}
} }
@Override @Override
public boolean hasNext() { public boolean hasNext() {
// TODO Auto-generated method stub return hasNext;
throw new UnsupportedOperationException("Unimplemented method 'hasNext'");
} }
@Override @Override
public T next() { public T next() {
// TODO Auto-generated method stub if (!hasNext) {
throw new UnsupportedOperationException("Unimplemented method 'next'"); throw new NoSuchElementException();
}
T currentItem = nextItem;
advance();
return currentItem;
} }
} }