# 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:

1. Using for-loop (Naive Approach)
2. Using Collection.reverse() method
3. 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 v1 = ``new` `Vector();` `        ``// 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));``        ``}``    ``}``}`
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

 `// 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 v = ``new` `Vector();` `        ``// 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);``    ``}``}`
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

 `// 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 v1 = ``new` `Vector();` `        ``// 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 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());``    ``}``}`
Output
```Before: [Geeks, for, Geeks, is, Best]
After:
Best
is
Geeks
for
Geeks```

My Personal Notes arrow_drop_up