Saturday, September 27, 2025
HomeLanguagesJavaStack listIterator() method in Java with Example

Stack listIterator() method in Java with Example

The listIterator() method of Java.util.Stack class is used to return a list iterator over the elements in this stack (in proper sequence). The returned list iterator is fail-fast.

Syntax:

public ListIterator listIterator()

Return Value: This method returns a list iterator over the elements in this stack (in proper sequence).

Below are the examples to illustrate the listIterator() method.

Example 1:




// Java program to demonstrate
// listIterator() method
// for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // Creating object of Stack<Integer>
            Stack<String>
                stack = new Stack<String>();
  
            // adding element to stack
            stack.add("A");
            stack.add("B");
            stack.add("C");
            stack.add("D");
  
            // print stack
            System.out.println("Stack: "
                               + stack);
  
            // Creating object of ListIterator<String>
            // using listIterator() method
            ListIterator<String>
                iterator = stack.listIterator();
  
            // Printing the iterated value
            System.out.println("\nUsing ListIterator:\n");
            while (iterator.hasNext()) {
                System.out.println("Value is : "
                                   + iterator.next());
            }
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Stack: [A, B, C, D]

Using ListIterator:

Value is : A
Value is : B
Value is : C
Value is : D

Program 2:




// Java code to illustrate lastIndexOf()
import java.util.*;
  
public class StackDemo {
    public static void main(String args[])
    {
  
        // Creating an empty Stack
        Stack<Integer> stack = new Stack<Integer>();
  
        // Use add() method to add elements in the Stack
        stack.add(1);
        stack.add(2);
        stack.add(3);
        stack.add(10);
        stack.add(20);
  
        // Displaying the Stack
        System.out.println("Stack: " + stack);
  
        // Creating object of ListIterator<String>
        // using listIterator() method
        ListIterator<Integer>
            iterator = stack.listIterator();
  
        // Printing the iterated value
        System.out.println("\nUsing ListIterator:\n");
        while (iterator.hasNext()) {
            System.out.println("Value is : "
                               + iterator.next());
        }
    }
}


Output:

Stack: [1, 2, 3, 10, 20]

Using ListIterator:

Value is : 1
Value is : 2
Value is : 3
Value is : 10
Value is : 20
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32323 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6690 POSTS0 COMMENTS
Nicole Veronica
11857 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11913 POSTS0 COMMENTS
Shaida Kate Naidoo
6806 POSTS0 COMMENTS
Ted Musemwa
7073 POSTS0 COMMENTS
Thapelo Manthata
6763 POSTS0 COMMENTS
Umr Jansen
6768 POSTS0 COMMENTS