Computer Science, asked by sreeja511, 1 year ago

Given :
public static Iterator reverse(List list) {
Collections.reverse(list);
return list.iterator();
public static void main(String[] args) {
List list = new ArrayList;
list.add("1");
list.add("2");
list.add("3");
for (Object obj: reverse (list))
System.out.print(obj + ", ");
What is the result?​

Answers

Answered by qwtiger
6

Answer:

It will face compilation error At the line

for (Object obj: reverse (list))

Because you cannot use 'for' to an iterator.

The correct code will be:

import java.util.*;

public class Test1 {

   public static Iterator reverse(List list) {

       Collections.reverse(list);

       return list.iterator();

   }

  public static void main(String[] args) {

       List list = new ArrayList();

       list.add("1");

       list.add("2");

       list.add("3");

       Iterator it = reverse(list);

       while(it.hasNext()) {

           System.out.println(it.next());

       }

   }

}

Similar questions