Skip to content
Related Articles

Related Articles

Improve Article
Stack listIterator(int) method in Java with Example
  • Last Updated : 24 Dec, 2018

The listIterator(int) method of Stack Class is used to return a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one. The returned list iterator is fail-fast.

Syntax:

public ListIterator listIterator(int index)

Parameters: This method takes the index of the first element as a parameter to be returned from the list iterator (by a call to next)

Return Value: This method returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

Exception: This method throws IndexOutOfBoundsException if the index is out of range (index size()).



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);
  
            // getting iterated value starting from index 2
            // using listIterator() method
            ListIterator<String>
                iterator = stack.listIterator(2);
  
            // Printing the iterated value
            System.out.println("\nUsing ListIterator"
                               + " from Index 2:\n");
            while (iterator.hasNext()) {
                System.out.println("Value is : "
                                   + iterator.next());
            }
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
Output:
Stack: [A, B, C, D]

Using ListIterator from Index 2:

Value is : C
Value is : D

Example 2: For IndexOutOfBoundsException




// Java program to demonstrate
// listIterator() method
// for IndexOutOfBoundsException
  
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);
            System.out.println("Size of Stack: "
                               + stack.size());
  
            // Get ListIterator from index 7
            // using listIterator() method
            System.out.println("Trying to getListIterator"
                               + " from index 7\n");
  
            ListIterator<String>
                iterator = stack.listIterator(7);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
Output:
Stack: [A, B, C, D]
Size of Stack: 4
Trying to getListIterator from index 7

Exception thrown : java.lang.IndexOutOfBoundsException: Index: 7

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :