class Demo_ListIterator{ public static void main(String[] args){
List list = new ArrayList(); //创建子类对象
list.add(new(Person("张根硕",32))); //向集合中添加元素(我这里添加的是Person对象,很简单的,我就不写了) list.add(new(Person("古巨基",42))); //注意Person中要重写toString()方法。
list.add(new(Person("阳顶天",45)));
list.add(new(Person("赵日天",36)));
/*
for(int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
*/
//方法二
ListIterator it = list.listIteratior();
while(it.hasNext()){
Person p = (Person)it.next();
System.out.println(p.getName()+", "+p.getAge());
}
}
}
|
|