package iterator; import java.util.*; public class Multiple implements Iterable { private int multiplesOf, maximum; public Multiple(int multiplesOf, int maximum) { this.multiplesOf = multiplesOf; this.maximum = maximum; } public Iterator iterator() { return new TheIterator(); } /* Inner class for iterator */ public class TheIterator implements Iterator { /* Notice: accessing outer class values */ private int currentValue = multiplesOf; public boolean hasNext() { return currentValue<=maximum; } public Integer next() { int toReturn = currentValue; currentValue += multiplesOf; return toReturn; } public void remove() { throw new UnsupportedOperationException("Iterator remove is not implemented"); } } public static void main(String[] args) { Multiple m3 = new Multiple(3, 100); for (Integer i : m3) System.out.print(i + " "); System.out.println(); Multiple m4 = new Multiple(4, 100); for (Integer i : m4) System.out.print(i + " "); /* What would happen if we remove implements Iterable */ } }