import java.util.*;class iterator
{
public static void main(String[] args)
{
List nodes2= new ArrayList(); nodes2.add("a6");
nodes2.add("a2");
nodes2.add("a4");
nodes2.add("a3");
nodes2.add("a5");
nodes2.add("f0"); ListIterator it2 = nodes2.listIterator();
while(it2.hasPrevious()){
String obj1 = (String)it2.previous();
System.out.println(obj1);
} }
}
{
public static void main(String[] args)
{
List nodes2= new ArrayList(); nodes2.add("a6");
nodes2.add("a2");
nodes2.add("a4");
nodes2.add("a3");
nodes2.add("a5");
nodes2.add("f0"); ListIterator it2 = nodes2.listIterator();
while(it2.hasPrevious()){
String obj1 = (String)it2.previous();
System.out.println(obj1);
} }
}
解决方案 »
- SWT TabFolder tabitem的问题
- 在windows2000下怎样用java实现“新建文件夹“功能?
- 出现Exception in thread "main" java.lang.NoClassDefFoundError:hello的错误!
- 这是为什么????总得到NULL
- 根据Applet传入的scoops变量值 的switch语句函数应该放哪里?
- 写成的java程序如何在windows2000下运行
- 哪位朋友有Borland Enterprise Server 5.01的licence?
- 在Java中如何调用Unix系统的crypt()函数?
- JBuilder 4 的菜鸟问题
- 怎么创建VisualClass?
- 自从学.net就发觉自己整天在学新技术,新名词了,很少有时间把精力集中到具体业务编程上。
- Java读取属性文件的问题
while(it2.hasNext())
{
it2.next();
}
API:
listIterator()
Returns a list iterator of the elements in this list (in proper sequence).
ListIterator it2 = nodes2.listIterator(nodes2.size()-1);但要注意,遍历的时候,需要先调用一次 it2.next(),然后再反复调用 it2.previous()-------------事实上不需要先调用一次next(), 在API doc中,ListIterator里有这么一段:
Throws:
IndexOutOfBoundsException - if the specified index is out of range (index < 0 || index > size()).
也就是说index是可以等于nodes2.size()的。而将nodes2.size()作为参数传给那个函数,所得到的迭代器指向的是最后一个元素的下一个位置,因此可以直接调用previous().