• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem while using Listiterator nextIndex () method

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following is the piece of my code:



I wish to have a pointer 'i' that iterates over a List and a pointer 'j' that will start its iteration from i+1 location.
However the above code outputs the same value of 'i' and 'j' for each iteration inspite of my initailizing 'j' to i.nextIndex() at begin of each outer while iteration.
Please Help.
 
Ranch Hand
Posts: 121
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see where your call i.next() (or i.previous()) in your outer loop. So i-iterator does not change it's position. Then you initializes j using same index on each iteration. So values of j.nextIndex() are also same on each iteration
 
Manas Saxena
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I justed edited the code please check now
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to move to next element in Iterator you call next().
This updates special cursor variable to refer to index after index of element returned by next().
After you called next() on index say 5 subsequent call of nextIndex() returns 6.
This line initializes ListIterator with index returned by i.nextIndex() :

This means that two ListIterators i and j currently point to the same index returned by i.nextIndex().
In your case ListIterators i and j are created from the same collection and after the above line in [code] you have both i and j pointing by the same index to the same element.

You will never ask such question if you look at source code of ListIterator of collection you are using.
Finally I recommend you to investigate Java collections profoundly in my tutorials.
 
reply
    Bookmark Topic Watch Topic
  • New Topic