Solved

backward traversal is not possible in enhance for loop

Posted on 2011-03-24
6
353 Views
Last Modified: 2012-05-11
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
Comment
Question by:gudii9
6 Comments
 
LVL 92

Accepted Solution

by:
objects earned 250 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

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

Expert Comment

by:Sathish David Kumar N
ID: 35213055
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 19

Assisted Solution

by:Jim Cakalic
Jim Cakalic earned 125 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
}

Open in new window

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
}

Open in new window

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

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

can we do reverse traversal. please advise
0
 
LVL 92

Expert Comment

by:objects
ID: 35370339
> can we do reverse traversal. please advise

I showed how in my first comment
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now