public class Test { public static void main(String[] args) { LinkedList<String> colors1 = new LinkedList<String>(); colors1.add("red"); colors1.add("blue"); colors1.add("yellow"); colors1.add("orange");
System.out.println("The colors are:"); Iterator it=colors1.iterator(); while (it.hasNext()) { System.out.println(it.next()); }
import java.util.LinkedList;
import java.util.ListIterator;public class Test {
public static void main(String[] args) {
LinkedList<String> colors1 = new LinkedList<String>();
colors1.add("red");
colors1.add("blue");
colors1.add("yellow");
colors1.add("orange"); System.out.println("The reverse order:"); ListIterator<String> it = colors1.listIterator();
while (it.hasNext()) {
it.next();
}
while (it.hasPrevious()) {
System.out.println(it.previous());
}
}
}
题目要求是:先用Iterator迭代器方法把所有的正序输出出来,然后再用ListIterator把所有的逆序输出出来,结果我的运行结果时ListIterator的迭代器没有发生作用;不知道为什么?
下面是我的代码和运行截图:
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
public class Test {
public static void main(String[] args) {
LinkedList<String> colors1 = new LinkedList<String>();
colors1.add("red");
colors1.add("blue");
colors1.add("yellow");
colors1.add("orange");
System.out.println("The colors are:");
Iterator it=colors1.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("The reverse order:");
ListIterator<String> lit = colors1.listIterator();
while (lit.hasPrevious())
{
System.out.println(lit.previous());
}
}
}