Solved

backward traversal is not possible in enhance for loop

Posted on 2011-03-24
6
352 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

707 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

16 Experts available now in Live!

Get 1:1 Help Now