Reverse Order of All Elements of Java Vector
Last Updated :
20 Sep, 2021
Vector class Implements a dynamic array means it can shrink and expand its size as required just likely having the same operations like that in the arrays. Don’t confuse it with ArrayList as there is a thin line between vector and ArrayList, where the vector is synchronized rest the insertion order remains the same in both of them. Vector is present in java.util package and implements the list interface
Illustration: Reversing the order of elements in a vector
Input : [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
Input : [“GEEKS”, “FOR”, “geeks”]
Output: [“geeks”, “FOR”, “GEEKS”]
Methods:
- Using for-loop (Naive Approach)
- Using Collection.reverse() method
- Using listIterator() method
Method 1: Using for loop to print the element of vector in reverse order.
Java
import java.io.*;
import java.util.Vector;
import java.util.Collections;
import java.util.ListIterator;
public class GFG {
public static void main(String[] args)
{
Vector<Integer> v1 = new Vector<Integer>();
v1.add( 1 );
v1.add( 2 );
v1.add( 3 );
v1.add( 4 );
v1.add( 5 );
System.out.println( "Before reverse of vector : " );
System.out.println(v1);
System.out.println( "After reverse of vector : " );
for ( int i = v1.size() - 1 ; i >= 0 ; i--) {
System.out.println(v1.get(i));
}
}
}
|
Output
Before reverse of vector :
[1, 2, 3, 4, 5]
After reverse of vector :
5
4
3
2
1
Time complexity: O(n) of n time, where n is a number of elements in the vector.
Method 2: Collection.reverse() method as the name suggests is a method of Collection class.
Syntax:
Collections.reverse(vector) ;
Parameters: Vector object to be reversed
Return value: It returns the reverse of the vector element.
Java
import java.io.*;
import java.util.Vector;
import java.util.Collections;
public class GFG {
public static void main(String[] args)
{
Vector<String> v = new Vector<String>();
v.add( "GFG" );
v.add( "EarlierGreen" );
v.add( "NowBlack" );
System.out.println(
"Before Reverse Order, Vector Contains : " + v);
Collections.reverse(v);
System.out.println(
"After Reverse Order, Vector Contains : " + v);
}
}
|
Output
Before Reverse Order, Vector Contains : [GFG, EarlierGreen, NowBlack]
After Reverse Order, Vector Contains : [NowBlack, EarlierGreen, GFG]
Method 3: Using listIterator() method
Syntax:
public ListIterator listIterator()
Parameters: This method accepts no input arguments.
Return Value: This method returns a ListIterator object which can be used to traverse the Vector object. This object can be used to traverse the Vector object. It is bidirectional, so both forward and backward traversal is possible, using next() and previous() respectively.
Return Type: A ListIterator
Java
import java.util.Vector;
import java.util.ListIterator;
public class GFG {
public static void main(String[] args)
{
Vector<String> v1 = new Vector<String>();
v1.add( "Geeks" );
v1.add( "for" );
v1.add( "Geeks" );
v1.add( "is" );
v1.add( "Best" );
System.out.print( "Before: " );
System.out.println(v1);
ListIterator<String> l_itr
= v1.listIterator(v1.size());
System.out.println( "After: " );
while (l_itr.hasPrevious())
System.out.println(l_itr.previous());
}
}
|
Output
Before: [Geeks, for, Geeks, is, Best]
After:
Best
is
Geeks
for
Geeks
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...