Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Reverse Order of All Elements of Java Vector
Next article icon

Reverse Order of All Elements of Java Vector

Last Updated : 20 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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<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));         }     } } 

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<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);     } } 

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<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());     } } 

Output
Before: [Geeks, for, Geeks, is, Best] After:  Best is Geeks for Geeks


 


Next Article
Reverse Order of All Elements of Java Vector

A

aksrathod07
Improve
Article Tags :
  • Java
  • Java Programs
  • Java-Vector
Practice Tags :
  • Java

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
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences