Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

listiterator
vector
java
communitycreator

What is the Vector.listIterator(index) method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Vector class is a growable array of objects. The elements of Vector can be accessed using an integer index and the size of a Vector can be increased or decreased. Read more about Vector here.

The listIterator(index) method of the Vector class will return a list iterator over the elements in this vector, starting at the specified position in the list. The returned list iterator will return the element at the specified index on calling the next method. When the previous method is called, the element at the specified index - 1 will be returned.

The ListIterator is an iterator which we can use to traverse the List in both directions (forward and backward), modify the list, and get the index of the current position of the iterator. Also, it doesn’t have the currentElement like Iterator. Read more about ListIterator here.

Syntax

public ListIterator<E> listIterator(int index)

Argument

The index of the first element is to be returned by the list iterator, upon calling the next method.

Code

The below code demonstrates how to use the listIterator method:

import java.util.Vector;
import java.util.ListIterator;
class ListIteratorExample {
public static void main( String args[] ) {
Vector<Integer> vector = new Vector<>();
vector.add(1);
vector.add(2);
vector.add(3);
vector.add(4);
ListIterator<Integer> listIterator = vector.listIterator(2);
System.out.println("Calling listIterator.next() : " + listIterator.next());
}
}
Calling listIterator

In the above code:

  • In line number 1 and 2, we imported the Vector and ListIterator classes.

  • In line number 5, we created a new Vector object with the name vector.

  • From line number 6 to 9, we added four elements (1,2,3,4) to the vector object using the add() method.

  • In line number 10, we used the listIterator(index) method with 2 as the argument. This method will return a ListIterator. Upon calling the next method on the returned ListIterator, we will get the element at the index 2.

RELATED TAGS

listiterator
vector
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring