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:

import java.util.LinkedList; 

public class ListIterator {

public static void main(String[] args) {

// creating linkedlist
LinkedList llist = 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.ListIterator lIterator = 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());
}
}
}
Output

Forward
string 1
string 2
string 3
string 4

Back
string 4< br />string 3
string 2
string 1
Reference
Javadoc: ListIterator