Stack listIterator(int) method in Java with Example
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.
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.
Stack: [A, B, C, D] Using ListIterator from Index 2: Value is : C Value is : D
Example 2: For IndexOutOfBoundsException
Stack: [A, B, C, D] Size of Stack: 4 Trying to getListIterator from index 7 Exception thrown : java.lang.IndexOutOfBoundsException: Index: 7
Please Login to comment...