finished uebung09
This commit is contained in:
parent
62da4a9ea5
commit
ac1cc19a88
Binary file not shown.
Binary file not shown.
@ -3,6 +3,7 @@ package uebung09.chess;
|
||||
import java.util.Iterator;
|
||||
|
||||
import uebung09.iterator.Array2dIterator;
|
||||
import uebung09.iterator.SkipNullIterator;
|
||||
|
||||
|
||||
public class Board implements Iterable<Piece>{
|
||||
@ -50,22 +51,21 @@ public class Board implements Iterable<Piece>{
|
||||
}
|
||||
|
||||
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<Piece> iterator() {
|
||||
// TODO Auto-generated method stub
|
||||
return new Array2dIterator<>(field);
|
||||
return new SkipNullIterator<>(new Array2dIterator<>(field));
|
||||
// throw new UnsupportedOperationException("Unimplemented method 'iterator'");
|
||||
}
|
||||
}
|
||||
|
@ -1,26 +1,42 @@
|
||||
package uebung09.iterator;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
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
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user