[关闭]
@w1024020103 2017-01-24T17:14:07.000000Z 字数 570 阅读 499

How to actually use Iterable and Iterator?

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.

  1. public Iterable<Board> solution(){
  2. Iterable<Board> iterable;
  3. if (isSolvable()) {
  4. iterable = new Iterable<Board>() {
  5. @Override
  6. public Iterator<Board> iterator() {
  7. return new SolutionIterator();
  8. }
  9. }
  10. } else {
  11. iterable = null;
  12. }
  13. return iterable;
  14. }
  15. private class SolutionIterator implements Iterator<Board>{
  16. int index = 0;
  17. @Override
  18. public boolean hasNext() {
  19. return 0 < solutionBoards.size();
  20. }
  21. @Override
  22. public Board next() {
  23. return solutionBoards.get(index++);
  24. }
  25. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注