11. public static Iterator reverse(List list) {
12. Collections.reverse(list);
13. return list.iterator();
14. }
15. public static void main(String[] args) {
16. List list = new ArrayList();
17. list.add(” 1”); list.add(”2”); list.add(”3”);
18. for (Object obj: reverse(list))
19. System.out.print(obj + “,”);
20. }
‘What is the result?
A. 3,2, 1,
B. 1, 2, 3,
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
Answer: C11. public static Collection get() {
12. Collection sorted = new LinkedList();
13. sorted.add(’B”); sorted.add(”C”); sorted.add(”A”);
14. return sorted;
15. }
16. public static void main(String[] args) {
17. for (Object obj: get()) {
18. System.out.print(obj + “, “);
19. }
20. }
What is the result?
A. A, B, C,
B. B, C, A,
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
Answer: B谁能帮我讲讲这两道题的异同和相关的知识点???
    谢谢~~

解决方案 »

  1.   

    11. public static Iterator reverse(List list) {
    12. Collections.reverse(list);
    13. return list.iterator();
    14. }
    15. public static void main(String[] args) {
    16. List list = new ArrayList();
    17. list.add(” 1”); list.add(”2”); list.add(”3”);
    18. for (Object obj: reverse(list))  //不能遍历一个Iterator
    19. System.out.print(obj + “,”);
    20. }
    ‘What is the result?
    A. 3,2, 1,
    B. 1, 2, 3,
    C. Compilation fails.
    D. The code runs with no output.
    E. An exception is thrown at runtime.
    Answer: C11. public static Collection get() {
    12. Collection sorted = new LinkedList();
    13. sorted.add(’B”); sorted.add(”C”); sorted.add(”A”);
    14. return sorted;
    15. }
    16. public static void main(String[] args) {
    17. for (Object obj: get()) {   //遍历一个Collection,正确
    18. System.out.print(obj + “, “);  
    19. }
    20. }
    What is the result?
    A. A, B, C,
    B. B, C, A,
    C. Compilation fails.
    D. The code runs with no output.
    E. An exception is thrown at runtime.
    Answer: B
      

  2.   

    for (Object obj: reverse(list))  
    这种写法,好像只能遍历实现了Iterable的接口
      

  3.   

    import java.util.*;public class TestIterator{
    public static void main(String args[]){
    ArrayList<Integer> list = new ArrayList<Integer>();

    list.add(1);
    list.add(2);

    Iterator iterator = list.iterator();

    for(int i : iterator)
    System.out.print(i +" ");
    }
    }
    楼主敲一下这个代码就知道为什么了   是因为Iterator不支持新的for循环的这种写法
    至于对的就没什么好说的需要注意的是Collection sorted = new LinkedList();这个你如何加到sorted里去的  就是如何输出的 即FIFO
      

  4.   

           
    帮忙讲详细一点Iterator和Collection遍历之间的区别吧,为什么一个可以遍历,另一个不能遍历。
        越详细越好~~大侠~~~
     
      

  5.   


    原来是不支持的原因。。谢谢~~~FIFO是先进先出的意思吧??谢谢了
      

  6.   

    新的语法规定就是这样的,
    LinkedList不支持排序,所以照原样输出~~
      

  7.   

    for (Object obj: Objs)语法后面的Objs要是一个list或者是可变历的东东,jdk得解释是:Can only iterate over an array or an instance of java.lang.Iterable
    你看看Iterator,它名字虽然是Iterator, 但是它不是一个Iterable的。
    linkedlist于排序没有关系,它支持类似队列的操作。