Write a class named FibonacciIterator
that would be usable as a stand-alone class to implement an iterator over the Fibonacci integers.
Recall that the first two Fibonacci numbers are 0 and 1, and each following Fibonacci number is the sum of the prior two.
The first several Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
FibonacciIterator itr = new FibonacciIterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
Implement the hasNext
and next
operations;
when the remove
method is called, you can throw an UnsupportedOperationException
.
The Fibonacci numbers are an infinite sequence, so your hasNext
method can always return true
.
Your iterator should not construct any internal data structures.