reverse iterator error

I was expecting the code below to output the elements of the list but in the reverse order. Hoever the compiler error that happens at the foreach loop is beyond me. Am I declaring the foreach loop wrong? Thanks for suggestions.

import java.util.*;

publicclass Test{

publicstatic Iterator reverse(List list){

Collections.reverse(list);

return list.iterator();

}

publicstaticvoid main(String[] args){

List list =new ArrayList();

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

for(Object obj:reverse(list))//ERROR: foreach not applicable to expression type

System.out.print(obj);

}

}

[1294 byte] By [DeChristoa] at [2007-11-26 18:11:44]
# 1
Give it an Iterable, not an Iterator.JLS ?4.14.2:The Expression must either have type Iterable or else it must be of an array type (?0.1), or a compile-time error occurs.~
yawmarka at 2007-7-9 5:44:23 > top of Java-index,Java Essentials,New To Java...
# 2
The for each takes a Iterable (I think) not a Iterator.Plus is it you intention to alter the original list?Edit: :shakefist: To slow.
mlka at 2007-7-9 5:44:23 > top of Java-index,Java Essentials,New To Java...
# 3
Um, could be a little be more specific if possible? Thanks. You mean give as return type a collection instead of an iterator?
DeChristoa at 2007-7-9 5:44:23 > top of Java-index,Java Essentials,New To Java...
# 4
> list.iterator();You don't need to the .iterator(); bit. the for each loop takes a Iterable (something a Collection, and so a List already is).
mlka at 2007-7-9 5:44:23 > top of Java-index,Java Essentials,New To Java...
# 5

Thanks. I got it:

public static List reverse(List list){

Collections.reverse(list);

return list; //.iterator();

}

Thats what you meant right? Ok it works now. Thanks.

DeChristoa at 2007-7-9 5:44:23 > top of Java-index,Java Essentials,New To Java...
# 6

> Um, could be a little be more specific if possible?

> Thanks. You mean give as return type a collection

> instead of an iterator?

import java.util.*;

public class Test {

public static List reverse(List list){

Collections.reverse(list);

return list;

}

public static void main(String[] args) {

List<Integer> list = new ArrayList<Integer>();

list.addAll(Arrays.asList(1, 2, 3));

System.out.println(list);

for(Object obj : reverse(list)) System.out.print(obj); // 321

}

}

Don't forget mlk's excellent point about altering the original list...

~

yawmarka at 2007-7-9 5:44:23 > top of Java-index,Java Essentials,New To Java...
# 7
> Thats what you meant right? Ok it works now. Thanks.Just to check, you do know that the original list will be reversed. You may wish to create a second list and reverse that.Edit: :shakefist: To slow. Again.
mlka at 2007-7-9 5:44:23 > top of Java-index,Java Essentials,New To Java...
# 8
Yes. The point was to make a list and reverse it. Thanks.
DeChristoa at 2007-7-9 5:44:23 > top of Java-index,Java Essentials,New To Java...
# 9
> Yes. The point was to make a list and reverse it.It leads to the question: Why not just use Collections.reverse()?~
yawmarka at 2007-7-9 5:44:23 > top of Java-index,Java Essentials,New To Java...
# 10

> > Yes. The point was to make a list and reverse

> it.

>

> It leads to the question: Why not just use

> Collections.reverse()?

>

> ~

..but, I am using it , right? Collections.reverse(list);

and passing the list as a parameter...

DeChristoa at 2007-7-9 5:44:23 > top of Java-index,Java Essentials,New To Java...
# 11
> ..but, I am using it , right?Yes. My point is that there is little - if anything in this case - to be gained by wrapping Collections.reverse() in another method.~
yawmarka at 2007-7-9 5:44:23 > top of Java-index,Java Essentials,New To Java...
# 12

> > ..but, I am using it , right?

>

> Yes. My point is that there is little - if anything

> in this case - to be gained by wrapping

> Collections.reverse() in another method.

>

> ~

Oh, I see what you mean, just try that directly:

import java.util.*;

public class Test {

public static void main(String[] args) {

List list = new ArrayList();

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

for(Object obj:Collections.reverse(list)) //ERROR: foreach not applicable to expression type

System.out.print(obj);

}

}

...Just tried that but I guess I did something wrong, and got another compiler error at the same line again.

DeChristoa at 2007-7-9 5:44:23 > top of Java-index,Java Essentials,New To Java...
# 13

> ...Just tried that but I guess I did something wrong...

The reverse() method returns void.

import java.util.*;

public class Test {

public static void main(String[] args) {

List list = new ArrayList();

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

Collections.reverse(list);

for(Object obj : list)

System.out.print(obj);

}

}

~

yawmarka at 2007-7-9 5:44:23 > top of Java-index,Java Essentials,New To Java...
# 14
Got it. Thanks. I appreciate your help.
DeChristoa at 2007-7-9 5:44:23 > top of Java-index,Java Essentials,New To Java...
# 15

And since you're using the enhanced for loop from Java 5, you may as well check out some other new features as well...

import java.util.*;

public class Test {

public static void main(String[] args) {

List<Integer> list = new ArrayList<Integer>(); // <--generics

list.addAll(Arrays.asList(1, 2, 3)); // <-- varargs, autoboxing

Collections.reverse(list);

for(Object obj : list)

System.out.print(obj);

}

}

yawmarka at 2007-7-21 17:17:15 > top of Java-index,Java Essentials,New To Java...
# 16
> Got it. Thanks. I appreciate your help.You're welcome.Cheers~
yawmarka at 2007-7-21 17:17:15 > top of Java-index,Java Essentials,New To Java...
# 17

You can do a reverse iterator without reverse the list:

public static <T> Iterable<T> reverseIterable(final List<T> list){

return new Iterable<T>(){

public Iterator<T> iterator(){

return reverseIterator(list);

}

};

}

public static <T> Iterator<T> reverseIterator(final List<T> list){

return new Iterator<T>(){

ListIterator<T> iterator=list.listIterator(list.size());

public boolean hasNext(){

return iterator.hasPrevious();

}

public T next(){

return iterator.previous();

}

public void remove(){

iterator.remove();

}

};

}

govisagod512a at 2007-7-21 17:17:15 > top of Java-index,Java Essentials,New To Java...