Iterating List using ListIterator

In this tutorial, We will show you how to perform Iterating List using ListIterator Java Example. The example Java Program has beed tested with environment and output is attached in the same post.

IteratingListUsingListIterator.java

package com.dineshkrish;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class IteratingListUsingListIterator {
public static void main(String[] args) {
// Defining the ArrayList Object
List<String> strList = new ArrayList<String>();
// Adding the elements to List Object
strList.add("One");
strList.add("Two");
strList.add("Three");
strList.add("Four");
strList.add("Five");
strList.add("Six");
// Defining the ListIterator Object
ListIterator<String> listIterator = strList.listIterator();
System.out.println("Iterating the List using ListIterator Top to Bottom");
System.out.println("----------------------------------------------------");
// Iterating the List Object from Top to Bottom using 'next()' method
while(listIterator.hasNext()) {
System.out.println(listIterator.next());
}
System.out.println("\nIterating the List using ListIterator Bottom to Top");
System.out.println("----------------------------------------------------");
// Iterating the List Object from Bottom to Top using 'previous()' method
while(listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
}
}

Output

—————–
Iterating the List using ListIterator Top to Bottom
—————————————————-
One
Two
Three
Four
Five
Six

Iterating the List using ListIterator Bottom to Top
—————————————————-
Six
Five
Four
Three
Two
One

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *