This example shows how to iterate Vector in reverse direction in Java. This example also shows how to iterate Vector in reverse or backward direction using the ListIterator and for loop.
How to iterate Vector in reverse direction in Java?
There are several ways using which we can iterate Java Vector in the backward direction as given below.
1. Using the for loop
We can use the simple for loop starting from the (vector size – 1) index to (0) index to iterate over elements of the Vector object in the reverse direction. We can get elements from Vector using the get
method and an index.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Vector<String> vColors = new Vector<String>(); vColors.add("Red"); vColors.add("Green"); vColors.add("Blue"); /* * Start iterating from index (vector size - 1) * to 0 and decrement counter by 1 using the * for loop */ for( int index = vColors.size() - 1 ; index >= 0 ; index-- ){ System.out.println( vColors.get(index) ); } |
Output
1 2 3 |
Blue Green Red |
2. Using the ListIterator
The Vector class provides the listIterator
method that accepts the starting position.
1 |
public ListIterator<E> listIterator(int index) |
It returns a ListIterator over the vector elements starting at the specified index.
In order to iterate the vector in a backward direction, we need to pass the vector size to this method to position the cursor after the last element of the vector. So that when we call the previous
method, the first element that is returned by it will be the last element of the vector object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Vector<String> vColors = new Vector<String>(); vColors.add("Red"); vColors.add("Green"); vColors.add("Blue"); /* * Get the ListIterator and position it at * the end of the vector. */ ListIterator<String> listIterator = vColors.listIterator( vColors.size() ); /* * Iterate using the hasPrevious and * previous methods */ while( listIterator.hasPrevious() ){ System.out.println( listIterator.previous() ); } |
Output
1 2 3 |
Blue Green Red |
3. Using the Apache Commons
If you are using the Apache Commons library in your project, you can also use the ReverseListIterator class to iterate the vector object in the reverse direction as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Vector<String> vColors = new Vector<String>(); vColors.add("Red"); vColors.add("Green"); vColors.add("Blue"); /* * Create new object of the ReverseListIterator class * using the vector object */ ReverseListIterator<String> reverseIterator = new ReverseListIterator<String>( vColors ); /* * Iterate using the hasNext and next methods * of the reverse list iterator and a while loop */ while( reverseIterator.hasNext() ){ System.out.println( reverseIterator.next() ); } |
Output
1 2 3 |
Blue Green Red |
Please also visit how to iterate a List in reverse direction to know about more ways to do this.
This example is a part of the Vector in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Vector Documentation