> This is the "normal" behavior of an Iterator
but is it normal?
I don't want to get the same element repeatedly...
Main Topics
Browse All TopicsIn ListIterator alternating calls to next and previous will return the same element repeatedly.
Any other iterator acts "normally"? or I should implement my one iterator?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
When u was next method in a iterator, as also in ListIterator, the iterator "pops" the top element from its list. therefor the current element wil be another element - the one after it in the list. when using the previous method - the previous element to the current one is the one u just poped - the same one !
-gkern
look at thes previous method documentation:
"Returns the previous element in the list. This method may be called repeatedly to iterate through the list backwards, or intermixed withcalls to <tt>next</tt> to go back and forth. (Note that alternating calls to <tt>next</tt> and <tt>previous</tt> will return the same element repeatedly.)" - form java.util.ListIterator
"only if the previous call was next - very tricky! "
Actually, if you just keep a temporary variable for what was last retrieved from the iterator this is very easy. For example:
Object last = null;
Iterator it = xxx.iterator();
last = it.next();
Object value = it.previous();
if (last == value)
{
value = it.previous();
}
Just a rough idea, and the same concept can be applied to next calls also.
Think of your "cursor" as being between elements, not on an element. If you call next() it jumps forward over an element and returns it. If you call previous() it jumps backward over an element and returns it. Notice sequential calls to next() and previous will be jumping over the same element.
If you want to "peek" at an element without jumping over it you can use list.get(iter.nextIndex())
So:
list.get(iter.nextIndex())
list.get(iter.previousInde
will return elements on either side of the cursor. (2 separate elements.
May I suggest using a loop using the indexes of the list instead of an iterator for this type of forward and backward movement.
s_lavie:
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
Business Accounts
Answer for Membership
by: gkernPosted on 2002-12-18 at 09:13:02ID: 7602419
This is the "normal" behavior of an Iterator