Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

stack
listiterator
java
communitycreator

What is the Stack.listIterator(index) method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Stack class is a Last-In-First-Out (LIFO)The element inserted first is processed last and the element inserted last is processed first. stack of objects.

The Stack class’s list iterator(index) method will return a list iterator over the elements in this stack starting at the specified position in the list. The returned list iterator will return the element at the specified index on calling the next method. The element at the specified index - 1 will return when the previous process is called.

The ListIterator is an iterator using which we can traverse the list in both directions (forward and backward), modify the list, and get the index of the Iterator’s current position. Also, it doesn’t have the currentElement like Iterator. Read more about ListIterator here.

Syntax

public ListIterator<E> listIterator(int index)

Argument

This index of the first element is to be returned by the list iterator on calling the next method.

Code

The code below demonstrates how to use the listIterator method:

import java.util.Stack;
import java.util.ListIterator;
class ListIteratorExample {
    public static void main( String args[] ) {
        Stack<Integer> stack = new Stack<>();  
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        ListIterator<Integer> listIterator = stack.listIterator(2);
        System.out.println("Calling listIterator.next() : " + listIterator.next());
    }
}

In the above code,

  • In line number 1 and 2: Imported the Stack and ListIterator classes.

  • In line number 5: Created a new Stack object with the name stack.

  • From line number 6 to 9: Added four elements(1,2,3,4) to the stack object using the push method.

  • In line number 10: Used the listIterator(index) method with 2 as the argument. This method will return a ListIterator. On calling the next method on the returned ListIterator we will get the element at the index 2.

RELATED TAGS

stack
listiterator
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring