public interface Iterator {
boolean hasNext();
Object next();
void remove();
}
依赖前两个方法就能完成遍历,典型的代码如下:
for(Iterator it = c.iterator(); it.hasNext(); ) {
Object o = it.next();
// 对o的操作...
}
具体案例:
import java.util.*;
public static String s;
int i;
public static List<String> list=new ArrayList<String>();
List extensions = new ArrayList() {{add("action");add("aacc");}};
public static void main(String args[]){
s="aaa";
list.add(s);
s="bbb";
list.add(s);
/*
System.out.println(list.getClass());
System.out.println(list.size());
System.out.println(list.indexOf("bbb"));
*/
System.out.println(list.isEmpty());
System.out.println(list);
String x="";
String y="";
for(Iterator it = list.iterator(); it.hasNext(); ) {
x = (String) it.next();
System.out.println(x);
it.remove();
}
System.out.println(list);
}
}