Παράδειγμα: διάσχιση της ακολουθίας Fibonacci

import java.util.Iterator;
import java.math.BigInteger;

/**
 * An Iterable interface over the Fibonacci sequence.
 * @author Diomidis Spinellis
 */
class FibonacciSequence implements Iterable<BigInteger> {

    /** The iterator over the Fibonacci sequence. */
    private class  FibonacciIterator implements Iterator<BigInteger> {
        /** Integer n-2 of the series. */
        private BigInteger n0 = null;
        /** Integer n-1 of the series. */
        private BigInteger n1 = null;

        /**
         * Return true.
         * The FibonacciSequence sequence is infinite.
         */
        public boolean hasNext() { return true; }

        /** Return the next FibonacciSequence integer. */
        public BigInteger next() {
            if (n0 == null) {
                n0 = BigInteger.ZERO;
                return n0;
            } else if (n1 == null) {
                n1 = BigInteger.ONE;
                return n1;
            } else {
                BigInteger r = n0.add(n1);
                n0 = n1;
                n1 = r;
                return r;
            }
        }

        /**
         * Remove an element.
         * Nothing to see here; move on.
         */
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }

    /** Return an iterator for the FibonacciSequence series. */
    public Iterator<BigInteger> iterator() {
        return new FibonacciIterator();
    }

    /** A simple test harness. */
    public static void main(String argv[]) {
        var fib = new FibonacciSequence();

        for (BigInteger i : fib)
            System.out.println(i);
    }
}
Βλέπε και το βίντεο για την εμφάνιση της ακολουθίας στη φύση.