Browse LinkedList with ListIterator in Java in both directions
This tutorial explains how browse a LinkedList using ListIterator. ListIterator allows you to LinkedList in both directions(forward and backward). You can also modify the list during the journey and get the current position of Iterator in the list.
Here is an example of a LinkedList containing String values. We can browse the list in both directions:
Javadoc: ListIterator
Here is an example of a LinkedList containing String values. We can browse the list in both directions:
import java.util.LinkedList;Output
public class ListIterator {
public static void main(String[] args) {
// creating linkedlist
LinkedListllist = new LinkedList ();
// adding the elements
llist.add("string 1");
llist.add("string 2");
llist.add("string 3");
llist.add("string 4");
// retrieve the ListIterator
java.util.ListIteratorlIterator = llist.listIterator();
// browse in the ascending direction of the indexes
System.out.println("Forward Path");
while(lIterator.hasNext()){
System.out.println(lIterator.next());
}
// browse in descending indexes
System.out.println("\nBackward Scan");
while(lIterator.hasPrevious()){
System.out.println(lIterator.previous());
}
}
}
ForwardReference
string 1
string 2
string 3
string 4
Back
string 4< br />string 3
string 2
string 1
Javadoc: ListIterator