// ********** IteratorDemo.java ********** interface IteratorIF { public Object next(); public boolean hasNext(); } class IteratorCollection { public static final int MAX_SIZE = 100000; private Integer array[] = new Integer[MAX_SIZE]; private int total = 0; public void addItem(Integer item) { if (total < MAX_SIZE) array[total++] = item; } private class InnerIterator implements IteratorIF { private int index = -1; public Object next() { if (index < total) return array[index]; else return null; } public boolean hasNext() { if (index < total-1) { index ++; return true; } else return false; } } public IteratorIF createIterator() { return new InnerIterator(); } } public class IteratorDemo { public static void main (String args[]) { long t1 = System.currentTimeMillis(); for (int j=0; j<5; j++) { IteratorCollection collection = new IteratorCollection(); for (int i=0; i<IteratorCollection.MAX_SIZE; i++) collection.addItem(new Integer(i)); IteratorIF iterator = collection.createIterator(); while (iterator.hasNext()) { Integer item = (Integer)iterator.next(); //System.out.println(item.intValue()); } } long t2 = System.currentTimeMillis(); System.out.print((t2-t1)+" "); System.out.flush(); } }