7 Replies - 40850 Views - Last Post: 19 August 2009 - 11:12 AM Rate Topic: -----

#1 DecafJava  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 18-May 09

How to Iterate through a collection in reverse?

Posted 18 August 2009 - 11:00 AM

I am trying to Iterate through a collection in reverse order.

Iterator iter = myList.iterator();

while(iter.hasNext()) {
	 iter.next();
}



The simple way would be to just reverse the elements in myList first and then iterate through the items. However, my professor says we can't do that for this assignment and we should do it by only manipulating the pointing system of the hasNext()/next() functions. Kinda stomped! Any ideas?

This post has been edited by DecafJava: 18 August 2009 - 11:01 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to Iterate through a collection in reverse?

#2 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: How to Iterate through a collection in reverse?

Posted 18 August 2009 - 11:18 AM

you would decrement:


for(int i=max;max>=0;i--){

//iteration sequence

]




hope that was helpful :)
Was This Post Helpful? 0
  • +
  • -

#3 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: How to Iterate through a collection in reverse?

Posted 18 August 2009 - 11:34 AM

uhm.. he's using an iterator, not a for-loop..

I think you're best option is to first loop until hasNext returns false and then keep track of how many elements there were. Alternatively, create a copy of the list that is reversed and loop through that.
Was This Post Helpful? 0
  • +
  • -

#4 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: How to Iterate through a collection in reverse?

Posted 18 August 2009 - 11:58 AM

View PostGloin, on 18 Aug, 2009 - 10:34 AM, said:

uhm.. he's using an iterator, not a for-loop..

I think you're best option is to first loop until hasNext returns false and then keep track of how many elements there were. Alternatively, create a copy of the list that is reversed and loop through that.



oh. shhhh....

can he just do that while making another list at the same time?
Was This Post Helpful? 0
  • +
  • -

#5 DecafJava  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 18-May 09

Re: How to Iterate through a collection in reverse?

Posted 18 August 2009 - 12:22 PM

that's the problem...using a for-loop and keeping a separate copy of a list which will be reversed is the easy way to go...but the professor wants us to reimplement the code for an Iterator so that we can go in reverse by doing the next() function...ideally i should create a new class that implements the Iterator interface and override the hasNext() and next() functions so that it does it in reverse, but i am not sure how to go about doing that
Was This Post Helpful? 0
  • +
  • -

#6 DecafJava  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 18-May 09

Re: How to Iterate through a collection in reverse?

Posted 18 August 2009 - 01:37 PM

alright i figured it out! it turns out i need to use a ListIterator instead of a regular Iterator...now all i need to figure out is how to have the iterator start with the last element...from there on i can keep doing:

while(hasPrevious())
   previous();

Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: How to Iterate through a collection in reverse?

Posted 18 August 2009 - 02:52 PM

Use the listiterator constructor that takes an index to start at:

ListIterator myIter = myList.listIterator(myList.size()); //returns a list iterator at the index
//a call to previous would give size-1, which is what you want


This post has been edited by KYA: 18 August 2009 - 02:53 PM

Was This Post Helpful? 1
  • +
  • -

#8 DecafJava  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 18-May 09

Re: How to Iterate through a collection in reverse?

Posted 19 August 2009 - 11:12 AM

View PostKYA, on 18 Aug, 2009 - 01:52 PM, said:

Use the listiterator constructor that takes an index to start at:

ListIterator myIter = myList.listIterator(myList.size()); //returns a list iterator at the index
//a call to previous would give size-1, which is what you want



Beautiful!! Just what I was looking for though couldn't find it for some reason...hehe

Thanks!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1