A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/how-to-iterate-the-vector-elements-in-the-reverse-order-in-java/ below:

How to Iterate the Vector Elements in the Reverse Order in Java?

How to Iterate the Vector Elements in the Reverse Order in Java?

Last Updated : 23 Jul, 2025

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 they are thread-safe.

There are basically two methods to iterate vector elements in reverse order, though there is another method using Apache Commons for iterating the vector backward in the backward direction but to use it, we just have to download some jar files and packages and also, the majority of the system does not support apache-commons.

We will be using two methods:

  1. Using For Loop
  2. Using ListIterator

Method 1: Using For Loop

To Iterate vector elements in backward direction using for loop, we can traverse vector from backward direction by running a loop from the index (vector. size()-1) to index 0.

Java
// Java Program to traverse the vector elements in backward
// direction

import java.util.*;

class GFG {
    public static void main(String[] args)
    {
        // creating a vector of String type
        Vector<String> numbers = new Vector<String>();
      
        // adding elements to vector
        numbers.add("One");
        numbers.add("Two");
        numbers.add("Three");
        numbers.add("Four");

        // Iterating from index equal to numbers.size()-1to
        // 0 and decrement index by 1 using the for loop

        for (int index = numbers.size() - 1; index >= 0; index--) {
            System.out.println(numbers.get(index));
        }
    }
}

Output
Four
Three
Two
One

Method 2: Using ListIterator

The Vector class has ListIterator method, which is used to iterate over the vectors. The ListIterator method takes the starting index from where traversal has to begin and returns the ListIterator.

Java
// Java Program to traverse the vector elements in backward
// direction using ListIterator

import java.util.*;

class GFG {
    public static void main(String[] args)
    {
        // creating a vector of String type
        Vector<String> numbers = new Vector<String>();
      
        // adding elements to vector
        numbers.add("One");
        numbers.add("Two");
        numbers.add("Three");
        numbers.add("Four");

        // listIterator method will return the list of
        // String of type ListIterator.
        ListIterator<String> listIterator
            = numbers.listIterator(numbers.size());

        // Iterate the ListIterator using the hasPrevious()
        // method

        while (listIterator.hasPrevious()) {
          
            // if element exist at previous index,then print
            // that element
            System.out.println(listIterator.previous());
        }
    }
}

Output
Four
Three
Two
One


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4