For Statement Iterable

Question

ADDQUESTION

Solution

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Example 70 - Using the Enhanced for Statement on an Iterable
 */
public class ForStatementIterable {

    public static void main(String[] args) {
        for(int i: fromTo(13, 17))
            System.out.println(i);
    }

    public static Iterable<Integer> fromTo(final int m, final int n) {
        class FromToIterator implements Iterator<Integer> {
            private int i = m;

            @Override
            public boolean hasNext() {
                return i <= n;
            }

            @Override
            public Integer next() {
                if (i <= n)
                    return i++;
                else
                    throw new NoSuchElementException();
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        }

        class FromToIterable implements Iterable<Integer> {

            @Override
            public Iterator<Integer> iterator() {
                return new FromToIterator();
            }
        }

        return new FromToIterable();
    }
}
Run this
Comments by Disqus