Vector

Vector ListIterator example

This is an example of how to obtain a Vector ListIterator. Obtaining a ListIterator of a Vector implies that you should:

  • Create a new Vector.
  • Populate the vector with elements, with add(E e) API method of Vector.
  • Invoke listIterator() API method of Vector, to get the ListIterator.
  • Use hasNext() and next() methods of ListIterator for forward iteration over the collection elements.
  • Use hasPrevious() and previous() methods of ListIterator for backward iteration over the collection elements.
  • Invoke nextIndex() and previousIndex() to get next and previous index from the current position in the list.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;
 
import java.util.Vector;
import java.util.ListIterator;
 
public class VectorListIteratorExample {
 
  public static void main(String[] args) {
 
    // Create a Vector and populate it with elements
    Vector vector = new Vector();
    vector.add("element_1");
    vector.add("element_2");
    vector.add("element_3");
    vector.add("element_4");
    vector.add("element_5");
  
    // The ListIterator object is obtained using listIterator() method
    ListIterator it = vector.listIterator();
  
    // For forward iteration over the collection elements we can use hasNext() and next() methods of ListIterator
    System.out.println("Forward iteration :");
    while(it.hasNext())

System.out.println(it.next());
  
    // For backward iteration over the collection elements we can use hasPrevious() and previous() methods of ListIterator
    System.out.println("Backward iteration :");
    while(it.hasPrevious())

System.out.println(it.previous());

    // nextIndex and previousIndex return next and previous index from the current position in the list
    System.out.println("Previous Index is : " + it.previousIndex());   
    System.out.println("Next Index is : " + it.nextIndex());

    // We get the next element in forward order
    System.out.println("Next element is : " + it.next());
    
    // nextIndex and previousIndex return next and previous index from the current position in the list
    System.out.println("Previous Index is : " + it.previousIndex());   
    System.out.println("Next Index is : " + it.nextIndex());

    // Add an element just before the next element
    it.add("element_6");

    System.out.println("After inserting element_6, Vector contains : ");
    for(int i = 0; i < vector.size(); i++)

System.out.println(vector.get(i));

    // We get the next element in forward order
    System.out.println("Next element is : " + it.next());

    // void remove() method removes the last element returned by next or previous methods
    it.remove();

    System.out.println("After removing element_2, Vector contains : ");
    for(int i = 0; i < vector.size(); i++)

System.out.println(vector.get(i));

    // We get the next element in forward order
    System.out.println("Next element is : " + it.next());
    
    /*

void set(Object o) method replaces the last element returned 

by next or previous methods. The set method can only be called 

if neither add or remove methods are called after last call of 

next or previous methods
    */
    it.set("element_7");

    System.out.println("After replacing element_3, Vector contains : ");
    for(int i = 0; i < vector.size(); i++)

System.out.println(vector.get(i)); 
 
  }
}

Output:

Forward iteration :
element_1
element_2
element_3
element_4
element_5
Backward iteration :
element_5
element_4
element_3
element_2
element_1
Previous Index is : -1
Next Index is : 0
Next element is : element_1
Previous Index is : 0
Next Index is : 1
After inserting element_6, Vector contains : 
element_1
element_6
element_2
element_3
element_4
element_5
Next element is : element_2
After removing element_2, Vector contains : 
element_1
element_6
element_3
element_4
element_5
Next element is : element_3
After replacing element_3, Vector contains : 
element_1
element_6
element_7
element_4
element_5

 
This was an example of how to obtain a Vector ListIterator in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button