interface Iterator{
void next() ; Object current() ; boolean end() ; } class Sequence { Integer []buf ; int length =0 ; Sequence(int len) { buf=new Integer[len] ; } Iterator getIterator() { return new SequenceIterator() ; } public void add(int x){ if(length<buf.length) { buf[length++]=x ; } } class SequenceIterator implements Iterator{ private int x=0 ; @Override public void next() { x++ ; }@Override
public Object current() { return buf[x] ; }@Override
public boolean end() { return (x==buf.length) ; } } } public class MyIterator { public static void main(String []agrs){ Sequence se=new Sequence(10) ; for(int i=0;i<10;i++){ se.add(i) ; } Iterator it=se.getIterator() ; while(!it.end()){ System.out.println(it.current() ) ; it.next() ; } } }