beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Vector ListIterator example in Java

By Chaitanya Singh | Filed Under: Java Collections

We can traverse a Vector in forward and Backward direction using ListIterator. Along with this we can perform several other operation using methods of ListIterator API like displaying the indexes of next and previous elements, replacing the element value, remove elements during iteration etc.

Example

Here we have a Vector of Strings and we are iterating it in both the directions using ListIterator.

import java.util.Vector;
import java.util.ListIterator;
public class VectorListIteratorDemo {
  public static void main(String[] args) {
     // Create a Vector
     Vector<String> vector = new Vector<String>();
 
     //Adding elements to the Vector
     vector.add("Item1");
     vector.add("Item2");
     vector.add("Item3");
     vector.add("Item4");
     vector.add("Item5");
 
     ListIterator litr = vector.listIterator();
     System.out.println("Traversing in Forward Direction:");
     while(litr.hasNext())
     {
       System.out.println(litr.next());
     }
 
     System.out.println("\nTraversing in Backward Direction:");
     while(litr.hasPrevious())
     {
       System.out.println(litr.previous());
     }
  }
}

Output:

Traversing in Forward Direction:
Item1
Item2
Item3
Item4
Item5

Traversing in Backward Direction:
Item5
Item4
Item3
Item2
Item1

References:
ListIterator javadoc
Vector Javadoc

Enjoyed this post? Try these related posts

  1. Deque Interface in Java Collections
  2. Get size of Hashtable example in Java
  3. HashSet Class in Java with example
  4. Java ArrayList set() Method example
  5. Java ArrayList lastIndexOf(Object 0bj) Method example
  6. How to loop ArrayList in Java

Comments

  1. raghul says

    October 25, 2018 at 8:15 AM

    If i give backward direction alone output is empty, but if i give forward direction first and then backward direction am getting the proper output

    Reply
    • Chaitanya Singh says

      October 29, 2018 at 1:49 PM

      This is because initially the iterator’s current position is at the start of the vector, so when you move backward direction you get an empty output because there is no element before the first element of vector.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap