[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# backward traversal is not possible in enhance for loop

Posted on 2011-03-24
Medium Priority
376 Views
Hi,

I read as backward traversal is not possible in enhanced for loop. I was not clear on that. Any ideas, resources, links, sample code highly appreciated. thanks in advance.
0
Question by:gudii9

LVL 92

Accepted Solution

objects earned 1000 total points
ID: 35212261
you can't do the following with the enhanced for

for (int i=list.size()-1; i>=0; i--) {
Object next = list.get(i);
...

It only allows you to iterate from start to end
0

LVL 20

Assisted Solution

Sathish David  Kumar N earned 500 total points
ID: 35213052
This is one of the main disadatage of  enhance for loop ! we cant go backward .
0

LVL 20

Expert Comment

ID: 35213055
0

LVL 19

Assisted Solution

Jim Cakalic earned 500 total points
ID: 35223333
As stated, the enhanced for syntax allows only forward traversal. However, backward traversal isn't particularly difficult (although perhaps inefficient):

Backwards traversal of an existing collection 'c' of class 'Type':
``````List<Type> temp = new ArrayList<Type>(c);
Collections.reverse(temp);
for (Type x : temp) {
// do whatever
}
``````
Backwards traversal of an existing array 'a' of class 'Type':
``````List<Type> temp = Arrays.asList(a);
Collections.reverse(temp);
for (Type x : temp) {
// do whatever
}
``````
In each case a new List object is constructed that duplicates the references of the original collection/array. The order of the references is then reversed before using the new List as the Iterable of the for loop.

Regards,
Jim
0

LVL 7

Author Comment

ID: 35369071
>>As stated, the enhanced for syntax allows only forward traversal. However, backward traversal isn't particularly difficult (although perhaps inefficient):

0

LVL 92

Expert Comment

ID: 35370339

I showed how in my first comment
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
###### Suggested Courses
Course of the Month20 days, 12 hours left to enroll