szjane 发表于 2007-6-10 16:59

请教一道310-055的Java题

import java.util.*;
public class Test59 {

        /**
       * @param args
       */
        public static Iterator reverse(List list){
                Collections.reverse(list);
                return list.iterator();
        }
       
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                List list = new ArrayList();
                list.add("1");
                list.add("2");
                list.add("3");
               
                for (Object obj : reverse(list)){
                        System.out.print((obj)+", ");
                }
        }

}

答案是:Compilation fails.

出错信息为:Can only iterate over an array or an instance of java.lang.Iterable

不明白为什么错了,怎么修改才能运行结果为 3, 2,1

pamagic 发表于 2007-6-10 18:05

原帖由 szjane 于 2007-6-10 17:59 发表 http://www.dolc.de/forum/images/common/back.gif
import java.util.*;
public class Test59 {

        /**
       * @param args
       */
        public static Iterator reverse(List list){
                Collections.reverse(list);
                return list.iterator();
        }
       
        public sta ...



答案就在编译错误里面阿
这样修改就可以了
public static List reverse(List list){
                Collections.reverse(list);
                return list;
        }

szjane 发表于 2007-6-10 19:38

非常感谢。
回头在看一遍书去 $汗$

greenflute 发表于 2007-6-11 22:15

其实觉得for each的语法有点儿鸡肋......$汗$ $汗$
页: [1]
查看完整版本: 请教一道310-055的Java题