@w1024020103
2017-01-24T17:14:07.000000Z
字数 570
阅读 499
Java
Iterable
Iterator
Here's codes I got for my assignment for Algorithm week4, but in fact I still don't really know how to implement Iterable and Iterator.
public Iterable<Board> solution(){
Iterable<Board> iterable;
if (isSolvable()) {
iterable = new Iterable<Board>() {
@Override
public Iterator<Board> iterator() {
return new SolutionIterator();
}
}
} else {
iterable = null;
}
return iterable;
}
private class SolutionIterator implements Iterator<Board>{
int index = 0;
@Override
public boolean hasNext() {
return 0 < solutionBoards.size();
}
@Override
public Board next() {
return solutionBoards.get(index++);
}
}