7

Possible Duplicate:
Can one do a for each loop in java in reverse order?

For a forward iteration:

for (int i=0; i<pathElements.length; i++){
    T pathElem = pathElements[i];
    .......
}

I can code it as a foreach:

for(T pathElem : pathElements){
    .......
}

Is there a switch so that a foreach could iterate in reverse?

for (int i=pathElements.length-1; i>=0; i--){
    T pathElem = pathElements[i];
    .......
}

Is there a reverse iteration switch in foreach?

(If not, don't you think it would be an exciting idea that JDK 8, 9, etc, includes this feature?)

Community
  • 1
  • 1
Blessed Geek
  • 21,058
  • 23
  • 106
  • 176
  • 8
    http://stackoverflow.com/questions/1098117/can-one-do-a-for-each-loop-in-java-in-reverse-order – oopbase Feb 17 '12 at 15:51

5 Answers5

13

It's fundamentally impossible because the foreach loop is based on the Iterable and Iterator interfaces, and those don't have a notion of reverse iteration.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • An excellent example of this would be infinite sequences. Since there does not exist a "last" element, it is mathematically impossible to reverse the order. – emory Feb 17 '12 at 18:35
  • 2
    I see [this](http://stackoverflow.com/questions/1098117/can-one-do-a-for-each-loop-in-java-in-reverse-order) link and think we can do that – Esterlinkof Mar 25 '15 at 00:11
  • 1
    @saman: you can do it for things that are sorted (like a `TreeSet`) or have the concept of an index (like a `List` or array), and admittedly those are most of the things we typically use the for loop on, but it's still not possible for a general `Iterable`. – Michael Borgwardt Mar 06 '16 at 12:29
11

Using Guava:

for (T elem : Lists.reverse(Arrays.asList(array))) {
  ...
}

iterates over a reversed view of the array as a list. (So there's only a constant overhead, and you don't change the underlying array.)

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
5

(If not, don't you think it would be an exciting idea that JDK 8, 9, etc, includes this feature?)

Not sure I see the value in such a feature.

For now, you can simply use Collections.reverse on a copy of the array that has been converted into a list using Arrays.asList and iterate through that.

mre
  • 43,520
  • 33
  • 120
  • 170
  • 1
    Well there's the value right there. instead of all those extra lines and potential processing you could (ideally) have a single word added to the foreach line. – Joseph Knight Apr 27 '15 at 06:08
  • 1
    Or at least a ReverseIterator built in to the JDK so you don't have to roll your own or use a 3rd party every time... – rogerdpack Dec 09 '15 at 16:00
1

No, there is not. The closest you'll get is reversing the array first.

ArrayUtils.reverse(array);
for (Item x : array){}
Thomas
  • 5,074
  • 1
  • 16
  • 12
-1

If pathElements is your custom class than you can define how it should be iterated by implementing Iterable, otherwise you can just simply encapsulate pathElements and override Iterable implementation methods.

You can also make something like this and override Iterable interface methods:

public class pathElementsReversed extends pathElements {}

And you can use for each like this:

foreach (pathElement p : (pathElementsReversed) pathElements) {...}
Ata S.
  • 890
  • 7
  • 12