java - How to make listiterator back to back next() and previous() not return the same value? -
this question has answer here:
this test program returns values in comments { 1, 2, 2, 3, 3 }
, change so, receive { 1, 2, 1, 2, 3, 2 }
. found, listiterator because cursor in between values this:
^1*2*3*4*
the asterisk *
the cursor , ^
is. how change it?
public class linkedlistiteratortest { public static void main(string[] args) { linkedlist<integer> backforw = new linkedlist<integer>(); listiterator<integer> bfi; backforw.add(1); backforw.add(2); backforw.add(3); backforw.add(4); bfi = backforw.listiterator(); system.out.println(bfi.next()); // 1 system.out.println(bfi.next()); // 2 system.out.println(bfi.previous()); // 2, how make return 1? system.out.println(bfi.next()); // 2, if last 1 1, // should 2. system.out.println(bfi.next()); // 3 system.out.println(bfi.previous()); // 3, how make return 2? } }
using notation, when have first call bfi.next() location of cursor is:
*1^2*3*4*
this returns 1 because cursor passed on 1 while moving forward. after next call bfi.next() location of cursor is:
*1*2^3*4*
as result, when call bfi.previous(), cursor moves one, passing on , returning 2 , resulting in following position:
*1^2*3*4*
this correct behavior linkedlistiterator. calling next() , calling previous() should return same value because iterating on same element.
Comments
Post a Comment