list - Writing a ListIterator that mirrors Java's built in Iterator -
so i'm attempting mirror functionality of java's listiterator. thing i'm having trouble wrapping head around getting previous() method working correctly.
here's have.
public listiterator300<item> listiterator() { return new listiterator300<item>() { private node<item> n = first; public boolean hasnext() { return n.next != last; } public item next() { n = n.next; return n.data; } public void remove() { } public boolean hasprevious() { return n.previous != first; } public item previous() { n = n.previous; return n.data; } }; } so, issue i'm running having previous() , next() methods, when called subsequently, return same number. i've read built in listiterator uses cursor. there general tips how implement code?
for example
[1 2 3 4]
next() -> 1
previous() -> 1
next() -> 1
next() -> 2
instead of checking:
n.next != last; check:
n != last; and same for:
n.previous != first; replace with:
n != first; do see why ?
Comments
Post a Comment