How to reset Iterator in java
Is it possible to make iterator to point first element ( reset ) again
Is it possible to reverse iterator
How to reverse the iterator
Code:
It's not possible to reset iterator using in build functions.
No such things available. But it's easy to make.
public interface ExtendedIterator extends Iterator {
public void reset();
}
public class ResetableIterator implements ExtendedIterator {
// factory method
public static ExtendedIterator getIterator(Collection c) {
return new ResetableIterator(c).i;
}
private Iterator i;
private Collection c;
private ResetableIterator(Collection c) {
this.c= c;
this.i= c.iterator();
}
// Iterator implementation (delegation)
public boolean hasNext() { return i.hasNext(); }
public Object next() { return i.next(); }
public void remove() { return i.remove(); }
// extension implementation
public void reset() { i= c.iterator(); }
}
Reference : http://forums.sun.com/thread.jspa?threadID=587562
What is ListIterator?
Current pointer position in listIterator
public interface ListIterator
extends Iterator
An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list. A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next(). In a list of length n, there are n+1 valid index values, from 0 to n, inclusive.
Element(0) Element(1) Element(2) ... Element(n)
^ ^ ^ ^ ^
Index: 0 1 2 3 n+1
http://download.oracle.com/javase/1.4.2/docs/api/java/util/ListIterator.html
No comments :
Post a Comment