I need to know how to "reset" LinkedList iterator to its first element.
For example:
LinkedList<String> list;
Iterator iter=list.listIterator;
iter.next();
iter.next();
Over and over again and after many moves of the iterator, I need to "reset" the position of the iterator.
I want to ask how I can "reset" my iterator to the first element.
I know that I can get list iterator of the first element in this way:
iter= list.listIterator(1);
Is this the best solution? Or maybe I missed something in Oracle docs?
Best would be not using
LinkedListat all, usually it is slower in all disciplines, and less handy. (When mainly inserting/deleting to the front, especially for big arrays LinkedList is faster)Use
ArrayList, and iterate withReset is trivial, just loop again.
If you insist on using an iterator, then you have to use a new iterator:
(I saw only once in my life an advantage of LinkedList: i could loop through whith a while loop and remove the first element)