Vector listIterator() method in Java with Examples (original) (raw)
Last Updated : 02 Jan, 2019
java.util.Vector.listIterator()
This method returns a list iterator over the elements of a Vector object in proper sequence. It is bidirectional, so both forward and backward traversal is possible, using next() and previous() respectively. The iterator thus returned is fail-fast. This means that structurally modifying the vector after the iterator is created, in any way except through the iterator’s own remove or add methods (using Vector.add(), for example), will cause iterator to throw ConcurrentModificationException.
Syntax:
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().
import
java.util.Vector;
import
java.util.ListIterator;
public
class
GFG {
`` public
static
void
main(String[] args)
`` {
`` Vector<String> vt =
new
Vector<String>();
`` vt.add(
"Geeks"
);
`` vt.add(
"for"
);
`` vt.add(
"Geeks"
);
`` vt.add(
"2019"
);
`` vt.add(
"AComputerSciencePortalForGeeks"
);
`` ListIterator listItr = vt.listIterator();
`` System.out.println(
"Forward Traversal:"
);
`` while
(listItr.hasNext()) {
`` System.out.println(listItr.next());
`` }
`` 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
java.util.Vector.listIterator(int index)
This method is used to return a list iterator by specifying starting index. Also bidirectional and fail-fast.
Syntax:
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).
import
java.util.Vector;
import
java.util.ListIterator;
public
class
GFG {
`` public
static
void
main(String[] args)
`` {
`` Vector<String> vt =
new
Vector<String>();
`` vt.add(
"Geeks"
);
`` vt.add(
"for"
);
`` vt.add(
"Geeks"
);
`` ListIterator listItr = vt.listIterator(
1
);
`` while
(listItr.hasNext()) {
`` System.out.println(listItr.next());
`` }
`` }
}
Example 3: To demonstrate IndexOutOfBoundsException thrown by listIterator(int index).
import
java.util.Vector;
import
java.util.ListIterator;
public
class
GFG {
`` public
static
void
main(String[] args)
`` {
`` Vector<String> vt
`` =
new
Vector<String>();
`` vt.add(
"Geeks"
);
`` vt.add(
"for"
);
`` vt.add(
"Geeks"
);
`` try
{
`` ListIterator listItr
`` = vt.listIterator(
5
);
`` }
`` catch
(IndexOutOfBoundsException e) {
`` System.out.println(
"Exception: "
+ e);
`` }
`` }
}
Output:
Exception: java.lang.IndexOutOfBoundsException: Index: 5
Example 4: To demonstrate ConcurrentModificationException thrown by ListIterator object when Vector object is modified after creating list iterator to it.
import
java.util.ConcurrentModificationException;
import
java.util.Vector;
import
java.util.ListIterator;
public
class
GFG {
`` public
static
void
main(String[] args)
`` {
`` Vector<String> vt =
new
Vector<String>();
`` vt.add(
"Geeks"
);
`` vt.add(
"for"
);
`` ListIterator listItr = vt.listIterator();
`` vt.add(
"Geeks"
);
`` try
{
`` System.out.println(listItr.next());
`` }
`` catch
(ConcurrentModificationException e) {
`` System.out.println(
"Exception: "
+ e);
`` }
`` }
}
Output:
Exception: java.util.ConcurrentModificationException
Reference:
https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html#listIterator–