Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. public FibonacciIterator(Integer n) {
  2. this.n = n;
  3. current = 0;
  4. }
  5.  
  6. @Override
  7. public boolean hasNext() {
  8. return (current < n);
  9. }
  10.  
  11. @Override
  12. public Integer next() {
  13. if (!hasNext()) {
  14. throw new NoSuchElementException();
  15. } else if(n == 1) {
  16. current = 1;
  17. return 0;
  18. } else {
  19. int r = runningValue;
  20. int p = previousValue;
  21. previousValue = runningValue;
  22. runningValue = p + previousValue;
  23. runningValue++;
  24. return r;
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement