4 Replies - 365 Views - Last Post: 20 October 2011 - 02:20 AM Rate Topic: -----

#1 gemoney  Icon User is online

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Iterate list backward

Posted 19 October 2011 - 11:24 PM

I am trying to iterate a list backward using hasPrevious() and previous(). I think I need IF statement to make it work
but not sure how to code it because the one I got here isn't work. Here is what I got:
 public LinkedList<Integer>  newList (LinkedList<Integer> myList)
     {
            ListIterator<Integer> itr = myList.listIterator(myList.size());
                boolean list = false;
                    while(itr.hasPrevious()){
                        itr.previous();
                  }
            return myList;
	} // method 


Is This A Good Question/Topic? 0
  • +

Replies To: Iterate list backward

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Iterate list backward

Posted 20 October 2011 - 12:36 AM

How do you know it's not working? Please describe what that means. What are your inputs, what output are you expecting, what output are you actually getting? For best results, post runnable code.
Was This Post Helpful? 0
  • +
  • -

#3 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 144
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: Iterate list backward

Posted 20 October 2011 - 12:36 AM

The code works, yet you don't do anything with the iterator, resp. with the return value of itr.previous().
Try doing something like:

while(itr.hasPrevious()){
  Integer foo = itr.previous();
  System.out.println(foo);
}


No need for any ifs if you just want to iterate.

This post has been edited by n8schatten: 20 October 2011 - 12:36 AM

Was This Post Helpful? 0
  • +
  • -

#4 gemoney  Icon User is online

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Re: Iterate list backward

Posted 20 October 2011 - 02:13 AM

I already tested, but it isn't returning the values in reverse
here is the original values : [0, 4, 4, -999, 12, 8, 20, -999, 12, 28, 16, -999, 36]
here is the values using
System.out.print(list + " ");
36 -999 16 28 12 -999 20 8 12 -999 4 4 0
and here is the returning values using
return myList;
I like this to be returned backward also..
[0, 4, 4, -999, 12, 8, 20, -999, 12, 28, 16, -999, 36]
here is my code:
 public LinkedList<Integer>  newList (LinkedList<Integer> myList)
     {
            ListIterator<Integer> itr = myList.listIterator(myList.size());
   
                    while(itr.hasPrevious()){
                      Integer list =  itr.previous();
                       System.out.print(list + " ");
                  }
                  return myList;
	} // method 

Was This Post Helpful? 0
  • +
  • -

#5 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 144
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: Iterate list backward

Posted 20 October 2011 - 02:20 AM

Iterating backwards is not the same as reversing the list.
If you want to reverse the entire list, have a look at Collections.reverse(List).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1