List list=new ArrayList();
list.add(new Student("list1",16));
list.add(new Studetn("list2",17));
ListIterator it=list.ListIterator();
//hasNext()返回的值
System.out.println(it.hasNext());
//正向遍历列表
while(it.hasNext()){
Student stu=(Student)it.next();
if(stu.getName().equals("list1"))
it.add(new Student("list2",17));
System.out.println(stu);
}
/*
it.add(new Student("list4"),19);
System.out.println(it.hasPrevious());
*/
//逆向遍历列表
while(it.hasPrevious){
Student stu=(Student)it.previous();
System.out.println(stu);
}
这段代码,运行结果为:
true
Student [name=list1,age=16]
Student [name=list2,age=17]
Student [name=list3,age=18]
//如果保留那段注释,程序到这里就结束了
//如果去掉注释,那么
true
Student [name=list4,age=19]
所以为什么正向迭代返回true,逆向需要再用迭代器add一次后才可以返回
true