• Post Reply Bookmark Topic Watch Topic
  • New Topic

enhanced for loop backward traversal  RSS feed

 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's true. But what can possibly be unclear about that? Can't be done, sounds pretty clear to me. What kinds of links were you looking for to expand on that? And what kind of sample code could possibly be written for something which can't be done?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well there is no forward or backward traversal for the enhanced for-loop. An enhanced for loop uses an Iterator to traversal through a data-source. It doesn't matter if the Iterator goes through the data-source forward, backward or criss cross.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this can be done pretty easily with Guava:

The object returned by Lists.reverse() is a view of the underlying list. If you look at how it implements iterator(), you should find that it returns an Iterator that is actually using a ListIterator and is using it in reverse.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!