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
// Java program to print vector element
// in reverse using for loop
// Importing Vector, Collection & ListIterator classes
// and generic java input output class
import java.io.*;
import java.util.Vector;
import java.util.Collections;
import java.util.ListIterator;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating vector of integer
Vector<Integer> v1 = new Vector<Integer>();
// Adding element to vector
// Custom inputs
v1.add(1);
v1.add(2);
v1.add(3);
v1.add(4);
v1.add(5);
// Display message
System.out.println("Before reverse of vector : ");
// Printing all elements of vector before reversing
System.out.println(v1);
// Display message
System.out.println("After reverse of vector : ");
// Iterating from last index of vector to 0
// index = vector.size()-1 (last index)
for (int i = v1.size() - 1; i >= 0; i--) {
// Printing elements of vector after reversing
System.out.println(v1.get(i));
}
}
}
OutputBefore 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
// Java Program to reverse order of elements of vector
// Importing generic java input/output classes
import java.io.*;
// Importing Vector and Collection class
// from java.util package
import java.util.Vector;
import java.util.Collections;
// Main Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Create a Vector object
Vector<String> v = new Vector<String>();
// Add elements to Vector
v.add("GFG");
v.add("EarlierGreen");
v.add("NowBlack");
// Display vector element before reversing
System.out.println(
"Before Reverse Order, Vector Contains : " + v);
// reverse() method to reverse vector element
// by passing vector object so as to reverse
Collections.reverse(v);
// Display vector element after reversing
System.out.println(
"After Reverse Order, Vector Contains : " + v);
}
}
OutputBefore 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
// Java Program to reverse order of elements of vector
// using listiterator
// Importing Vector and ListIterator classes
// of java.util package
import java.util.Vector;
import java.util.ListIterator;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Create(Declare) empty vector
Vector<String> v1 = new Vector<String>();
// Add elements to vector using add() method
v1.add("Geeks");
v1.add("for");
v1.add("Geeks");
v1.add("is");
v1.add("Best");
// Print message
System.out.print("Before: ");
// Printing all elements of Vector before reversing
System.out.println(v1);
// Declare list iterator
ListIterator<String> l_itr
= v1.listIterator(v1.size());
// Iistiterator to reverse the vector element using
// hashPrevious() method
// Print message
System.out.println("After: ");
while (l_itr.hasPrevious())
// Print vector elements after reversing
System.out.println(l_itr.previous());
}
}
OutputBefore: [Geeks, for, Geeks, is, Best]
After:
Best
is
Geeks
for
Geeks
Similar Reads
Copy Elements of Vector to Java ArrayList
Since Vector class and ArrayList class both are part of Java Collections, ie Collection framework, so both of these classes can use methods available to the Collection framework. Copy() method is one of the methods of Collection Interface which is used to copy one list to another list, here list can
3 min read
Java Program to Copy Elements of ArrayList to Vector
Vector implements List Interface, like ArrayList it also maintains insertion order but it is rarely used in the non-thread environment as it is synchronized, and due to which it gives a poor performance in adding, searching, deleting, and updating of its elements. To copy elements from one collectio
4 min read
Copy Elements of One Java Vector to Another Vector in Java
Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also. Syntax : Vector<Integer>
3 min read
How to Iterate the Vector Elements in the Reverse Order in Java?
The Vector class is found in java.util package and it implements List interface. The Vector class is included in the java collection framework from Java version 1.2. Unlike arrays, vectors can grow and shrink their size, and thus they are also called the Dynamic Array. Vectors are synchronized, ie t
3 min read
Java Program to Shuffle Vector Elements
Vectors basically fall in legacy classes but now it is fully compatible with collections. Java has many built-in functions to perform different operations on collections or other data types and one of them is shuffle. To shuffle Vector elements Collections.shuffle() method is used. It shuffle method
3 min read
Iterate Over Vector Elements in Java
Vector is like the dynamic array which can grow or shrink its size. Unlike array, we can store n-number of elements in it as there is no size limit. We can iterate over vector by the following ways: Simple for-loopEnhanced for-loopIteratorsEnumeration interface Method 1: Simple for-loop The idea is
4 min read
Java Program to Search an Element in Vector
A vector in Java is a dynamic array that can be resized as needed. It is synchronized, which means it is safe in multi-threaded programs. To find an element in a Vector we have to loop through its elements to find a match. In this article, we will learn how to search for a component in a Vector usin
3 min read
Java Program to Left Rotate the Elements of an Array
In Java, left rotation of an array involves shifting its elements to the left by a given number of positions, with the first elements moving around to the end. There are different ways to left rotate the elements of an array in Java.Example: We can use a temporary array to rotate the array left by "
5 min read
Iterate TreeMap in Reverse Order in Java
The TreeMap in Java is used to implement the Map interface and NavigableMap along with the AbstractMap Class. The TreeMap is sorted according to the natural ordering of its keys. There are three simple ways to iterate TreeMap in reverse order in Java: Using the reverseOrder() methodUsing the descend
3 min read
Extract Unique Elements from a Vector While Preserving the Order in Java
Vector in Java can be understood as a dynamic size array, that can be shrink and grow as per the requirement. Vector can be assumed similar to ArrayList, but Vector contains some of the extra methods as compared to it. Usually, a vector is not used much because of its poor performance of insertion,
4 min read