Solved

backward traversal is not possible in enhance for loop

Posted on 2011-03-24
6
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

705 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