就是Iterator 遍历用
interface Iterator{
public Object next();
public boolean hasNext();
}
class ConcreteIterator implements Iterator{
private List list = new ArrayList();
private int cusor =0;
public ConcreteIterator(List list){
this.list=list;
}
public boolean hasNext(){
if(cusor == list.size()){
return false;
}
return true;
}
public Object next(){
Object obj =null;
if(this.hasNext()){
obj=this.list.get(cusor++);
}
return obj;
}
}
interface Aggregate{
public void add(Object obj);
public void remove(Object obj);
public Iterator iterator();
}
class ConcreteAggregate implements Aggregate{
private List list = new ArrayList();
public void add(Object obj){
list.add(obj);
}
public Iterator iterator(){
return new ConcreteIterator(list);
}
public void remove(Object obj){
list.remove(obj);
}
}
public class IteratorTest {
public static void main(String[] args){
Aggregate ag = new ConcreteAggregate();
ag.add("A");
ag.add("B");
ag.add("C");
Iterator it =ag.iterator();
while (it.hasNext()){
String str =(String) it.next();
System.out.println(str);
}
}
}