package pkg;
interface Selector{
boolean end();
Object current();
void next();
}
public class p192 {// == Sequence
private Object[] items;
private int next=0;
public p192(int size) {items=new Object[size];}//create a size object array
public void add(Object x) {
if (next<items.length)
items[next++]=x;
}
//inner class
private class SeqSelect implements Selector{
private int i=0;
public boolean end() {return i==items.length;}//can use variables in Outer Class
public Object current() {
return items[i];
}
public void next() {
if (i<items.length)
i++;
}
}
public Selector selector() {
return new SeqSelect();// up cast
}
// ... main ...
public static void main(String[] args) {
int sn=20;
p192 seq=new p192(sn);
for (int i=0;i<sn;i++){
seq.add(Integer.toString(i));
}
Selector select=seq.selector();
while(!select.end()) {
System.out.print(select.current()+" ");
select.next();
}
}
}
OUTPUT:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19