Vector listIterator() method in Java with Examples
Last Updated :
02 Jan, 2019
Improve
public ListIterator listIterator()Parameters: This method accepts no input arguments. Return Value: This method returns a ListIterator object which can be used to traverse the Vector object. Example 1: To demonstrate forward and backward traversal using listIterator().
// Java code to illustrate listIterator()
import java.util.Vector;
import java.util.ListIterator;
public class GFG {
public static void main(String[] args)
{
// Declare empty vector
Vector<String> vt = new Vector<String>();
vt.add("Geeks");
vt.add("for");
vt.add("Geeks");
vt.add("2019");
vt.add("AComputerSciencePortalForGeeks");
// Declare list iterator
ListIterator listItr = vt.listIterator();
// Forward iterations
System.out.println("Forward Traversal:");
while (listItr.hasNext()) {
System.out.println(listItr.next());
}
// Backward iterations
System.out.println("\nBackward Traversal:");
while (listItr.hasPrevious()) {
System.out.println(listItr.previous());
}
}
}
Output:
Forward Traversal: Geeks for Geeks 2019 AComputerSciencePortalForGeeks Backward Traversal: AComputerSciencePortalForGeeks 2019 Geeks for Geeks
public ListIterator listIterator(int index)Parameters: The parameter index is an integer type value that specifies the first element to be returned from the list iterator (by a call to next()). Return Value: This method returns a ListIterator object which can be used to traverse the Vector object. Exception: This method throws IndexOutOfBoundsException, if the index is out of range (index < 0 or index > size()) Example 2: To demonstrate listIterator(int index).
// Java code to illustrate listIterator(int index)
import java.util.Vector;
import java.util.ListIterator;
public class GFG {
public static void main(String[] args)
{
// Declare empty vector
Vector<String> vt = new Vector<String>();
vt.add("Geeks");
vt.add("for");
vt.add("Geeks");
// Declare list iterator
ListIterator listItr = vt.listIterator(1);
// traversal
while (listItr.hasNext()) {
System.out.println(listItr.next());
}
}
}
Output:
Example 3: To demonstrate IndexOutOfBoundsException thrown by listIterator(int index).
for Geeks
// Java code to illustrate IndexOutOfBoundsException
// thrown by listIterator(int index)
import java.util.Vector;
import java.util.ListIterator;
public class GFG {
public static void main(String[] args)
{
// Declare empty vector
Vector<String> vt
= new Vector<String>();
vt.add("Geeks");
vt.add("for");
vt.add("Geeks");
// Declare list iterator at starting
// index greater than vector size
try {
ListIterator listItr
= vt.listIterator(5);
}
catch (IndexOutOfBoundsException e) {
// Exception handling
System.out.println("Exception: " + e);
}
}
}
Output:
Example 4: To demonstrate ConcurrentModificationException thrown by ListIterator object when Vector object is modified after creating list iterator to it.
Exception: java.lang.IndexOutOfBoundsException: Index: 5
// Java code to illustrate ConcurrentModificationException
// thrown by ListIterator object
import java.util.ConcurrentModificationException;
import java.util.Vector;
import java.util.ListIterator;
public class GFG {
public static void main(String[] args)
{
// Declare empty vector
Vector<String> vt = new Vector<String>();
vt.add("Geeks");
vt.add("for");
// Declare list iterator
ListIterator listItr = vt.listIterator();
// modify vector after creating list iterator
vt.add("Geeks");
try {
// Exception thrown here
System.out.println(listItr.next());
}
catch (ConcurrentModificationException e) {
// Exception handling
System.out.println("Exception: " + e);
}
}
}
Output:
Reference:
https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html#listIterator--
Exception: java.util.ConcurrentModificationException