aspose file tools*
The moose likes Beginning Java and the fly likes Can a Collection be traversed in reverse order? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can a Collection be traversed in reverse order?" Watch "Can a Collection be traversed in reverse order?" New topic
Author

Can a Collection be traversed in reverse order?

Jessica James
Ranch Hand

Joined: Jan 23, 2007
Posts: 69
Hi..

I am using a List of Objects to display..
When I click a Link, it has to be displayed in the reverse order..
Is there any way to do this..

Please help me..

Thanks in advance..
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
You can reverse a List with Collections.reverse(). However, that really does reverse it permanently.

You can use a "for" loop and get(int) to go backwards through the list. This is efficient if the implementation is something like an ArrayList, but inefficient if it's something like a LinkedList.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
There is also a way to do this more efficiently using a ListIterator, without reversing the original list.


"I'm not back." - Bill Harding, Twister
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Cool. Never had to do that, but shall now endeavour to remember it, in case it comes up.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can a Collection be traversed in reverse order?