import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class IteratorDemo {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add(new Student("小王", 10));
c.add(new Student("小李", 12));
//这里没有用while而是用的for 把it定义在循环里面,随着for的结束而消失
for (Iterator it = c.iterator(); it.hasNext();) {
Student s = (Student )it.next()
System.out.println(...........);
}
}
} |
|