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
Solved

backward traversal is not possible in enhance for loop

Posted on 2011-03-24
6
356 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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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:
This video teaches viewers about errors in exception handling.

828 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