Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public FibonacciIterator(Integer n) {
- this.n = n;
- current = 0;
- }
- @Override
- public boolean hasNext() {
- return (current < n);
- }
- @Override
- public Integer next() {
- if (!hasNext()) {
- throw new NoSuchElementException();
- } else if(n == 1) {
- current = 1;
- return 0;
- } else {
- int r = runningValue;
- int p = previousValue;
- previousValue = runningValue;
- runningValue = p + previousValue;
- runningValue++;
- return r;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement