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
More from my site

Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.
No responses yet