How to Browse a Vector in Java
You can browse a Vector in both directions forward and backward using the class ListIterator. Indeed, several other operations can be performed with the methods of the class. ListIterator. For example, displaying the indices of the previous and next item, changing the value of an item, deleting items while traversing, and so on.This example shows how to browse a Vector in both directions using ListIterator.
import java.util.ListIterator;Runtime:
import java.util.Vector;
public class ListIteratorVector {
public static void main(String[] args) {
Vector< String> vec = new Vector< String> ();
//insert elements
vec.add("e1");
vec.add("e2");
vec.add("e3");
vec.add("e4");
ListIterator litr = vec.listIterator();
System.out.println("cross in ascending order:");
while(litr.hasNext())
{
System.out.println(litr.next());
}
System.out.println("cross in descending order:");
while(litr.hasPrevious())
{
System.out.println(litr.previous());
}
}
}
cross in ascending order:
e1
e2
e3
e4
cross in descending order:
e4
e3
e2
e1