diff --git a/bin/uebung09/chess/Board.class b/bin/uebung09/chess/Board.class index fe2a88c..25ad31e 100644 Binary files a/bin/uebung09/chess/Board.class and b/bin/uebung09/chess/Board.class differ diff --git a/bin/uebung09/iterator/SkipNullIterator.class b/bin/uebung09/iterator/SkipNullIterator.class index 15b794b..599c525 100644 Binary files a/bin/uebung09/iterator/SkipNullIterator.class and b/bin/uebung09/iterator/SkipNullIterator.class differ diff --git a/src/uebung09/chess/Board.java b/src/uebung09/chess/Board.java index b69232a..e50d7eb 100644 --- a/src/uebung09/chess/Board.java +++ b/src/uebung09/chess/Board.java @@ -3,6 +3,7 @@ package uebung09.chess; import java.util.Iterator; import uebung09.iterator.Array2dIterator; +import uebung09.iterator.SkipNullIterator; public class Board implements Iterable{ @@ -50,22 +51,21 @@ public class Board implements Iterable{ } public String toString() { - // StringBuilder s = new StringBuilder("["); + StringBuilder s = new StringBuilder("["); - // for(Piece p : this) { - // s.append(p.toString()); - // } + for(Piece p : this) { + s.append(p.toString()); + } - // s.append("]"); + s.append("]"); - // return s.toString(); - return ""; + return s.toString(); } @Override public Iterator iterator() { // TODO Auto-generated method stub - return new Array2dIterator<>(field); + return new SkipNullIterator<>(new Array2dIterator<>(field)); // throw new UnsupportedOperationException("Unimplemented method 'iterator'"); } } diff --git a/src/uebung09/iterator/SkipNullIterator.java b/src/uebung09/iterator/SkipNullIterator.java index 555ddef..ae2d656 100644 --- a/src/uebung09/iterator/SkipNullIterator.java +++ b/src/uebung09/iterator/SkipNullIterator.java @@ -1,26 +1,42 @@ package uebung09.iterator; import java.util.Iterator; -import java.util.List; +import java.util.NoSuchElementException; public class SkipNullIterator implements Iterator{ + private final Iterator iterator; + private T nextItem; + private boolean hasNext; - public SkipNullIterator(Iterator i){ + public SkipNullIterator(Iterator 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 public boolean hasNext() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'hasNext'"); + return hasNext; } @Override public T next() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'next'"); + if (!hasNext) { + throw new NoSuchElementException(); + } + T currentItem = nextItem; + advance(); + return currentItem; } - - - }