解决方案 »

  1.   

    ListIterator是从第一个开始的,你一上来就问hasPrevious那当然没有。
      

  2.   

    你想要倒序,,Collections.reverse(list)这样就可以了啊
      

  3.   

    题目要求是让用ListIterator实现逆序输出。
      

  4.   

    很容易实现吧。先遍历到末尾,然后在用hasPrevious()就行了,已验证。代码:
    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());
    }
    }
    }
      

  5.   

    谢谢你,这样确实可以运行出来。原谅我没有把题目说具体:
    题目要求是:先用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());
            }
        }
    }