listIterator(int): This method is available in java.util.Vector class of Java.
Syntax:
ListIterator<K> java.util.Vector.listIterator(int index)
This method takes one argument. This method returns a list iterator over the elements in this list starting at the specified position in the list.
Parameters: One parameter is required for this method.
index: index of the first element to be returned from the list iterator.
Returns: a list iterator over the elements in this list starting at the specified position in the list.
Throws:
IndexOutOfBoundsException - if the index is out of range(index < 0 || index > size())
Approach 1: When no exception
Java
import java.util.ListIterator;import java.util.Vector;public class VectorlistIterator2 {public static void main(String[] args) {Vector<String> vector = new Vector<>();vector.add("Hello");vector.add("Java");vector.add("C++");vector.add("Program");int index = 1;ListIterator<String> listIterator =vector.listIterator(index);listIterator.forEachRemaining((n ->System.out.print(n + " ")));}}
Output:
Java C++ Program
Approach 2: IndexOutOfBoundsException
Java
import java.util.ListIterator;import java.util.Vector;public class VectorlistIterator2 {public static void main(String[] args) {Vector<String> vector = new Vector<>();vector.add("Hello");vector.add("Java");vector.add("C++");vector.add("Program");int index = 5;ListIterator<String> listIterator =vector.listIterator(index);listIterator.forEachRemaining((n ->System.out.print(n + " ")));}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5 at java.base/java.util.Vector.listIterator(Vector.java:1205)
No comments:
Post a Comment