Home » Java Vector listIterator() Method with Examples

Java Vector listIterator() Method with Examples

by Online Tutorials Library

Java Vector listIterator() Method

The listIterator() Java Vector class method returns a list iterator over the elements in the given list in a proper sequence. There is two different types of Java listIterator() method which can be differentiated depending on its parameter. These are:

  1. Java Vector listIterator() Method
  2. Java Vector listIterator(int index) Method

listIterator() Method:

This method is used to get a list iterator over the elements in the given list in a proper sequence.

listIterator(int index) Method:

This method is used to get a list iterator over the elements in the given list in a proper sequence. The returned list starts at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next.

Syntax

Following is the declaration of listIterator() method:

Parameter

DataType Parameter Description Required/Optional
int index It is an index of the first element to be returned from the list iterator. Required

Return

The listIterator() method returns a list iterator over the elements in this list in a proper sequence.

Exceptions

IndexOutOfBoundsException– This method has thrown an exception if the index is out of range i.e. (index < 0 || index > size()).

Compatibility Version

Java 1.2 and above

Example 1

Test it Now

Output:

Iterate elements are:   Two  Three  Four  Five  

Example 2

Test it Now

Output:

Iterate elements are:   Orange  Mango  Banana  Apple  

Example 3

Test it Now

Output:

Iterate elements are:   1  2  3  4  5  

Example 4

Test it Now

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 25  at java.base/java.util.Vector.listIterator(Vector.java:1234)  at myPackage.VectorListIteratorExample4.main(VectorListIteratorExample4.java:12)  

Next TopicJava Vector

You may also like