Home »
Java programming language
How to Reverse an ArrayList in Java
By Preeti Jain Last updated : February 05, 2024
Problem statement
Given an ArrayList, write a Java program to reverse the given ArrayList.
Reversing an ArrayList in Java
Below are the two approaches to reverse a given ArrayList -
- Using ListIterator
- Using Collections.reverse() Method
Reverse an ArrayList in Java using ListIterator
Below is an example to reverse an ArrayList using ListIterator.
Example to Reverse an ArrayList in Java using ListIterator
// Java program to reverse an ArrayList
// using ListIterator
import java.io.*;
import java.util.*;
// Creating a class
class ReverseArrayListClass {
// ListIterator
public ArrayList < Integer > reverseArrayList(ArrayList < Integer > arr_list) {
// Reversing Array List
for (int i = 0; i < arr_list.size() / 2; i++) {
Integer temp_element = arr_list.get(i);
arr_list.set(i, arr_list.get(arr_list.size() - i - 1));
arr_list.set(arr_list.size() - i - 1, temp_element);
}
return arr_list;
}
// Method to print ArrayList
public void printArrayList(ArrayList < Integer > arr_list) {
for (int i = 0; i < arr_list.size(); i++) {
System.out.print(arr_list.get(i) + " ");
}
}
}
// The Main Class
public class Main {
public static void main(String[] args) {
// Creating an objArrayListect of ReverseArrayListClass class
ReverseArrayListClass objArrayList = new ReverseArrayListClass();
// Creating an ArrayList object
ArrayList < Integer > numbers = new ArrayList < Integer > ();
// Adding Elements
numbers.add(new Integer(10));
numbers.add(new Integer(20));
numbers.add(new Integer(30));
numbers.add(new Integer(40));
numbers.add(new Integer(50));
// Printing original ArrayList
System.out.print("Original ArrayList: ");
objArrayList.printArrayList(numbers);
numbers = objArrayList.reverseArrayList(numbers);
// Printing reversed ArrayList
System.out.print("\nReversed ArrayList: ");
objArrayList.printArrayList(numbers);
}
}
The output of the above example is:
Original ArrayList: 10 20 30 40 50
Reversed ArrayList: 50 40 30 20 10
Reverse an ArrayList in Java using Collections.reverse() Method
The Collections.reverse() method is available in java.util package. It is used to reverse the elements of any Collection but here we are talking about ArrayList.
Syntax:
public static void reverse(Collection c);
Parameter(s):
- Collection c – represents the name of Collection for which reverse ArrayList is to be returned.
Return value:
The return type of this method is void, it returns nothing or does not return any value.
Example to Reverse an ArrayList in Java using Collections.reverse() Method
// Java program to demonstrate the example of
// reversing an ArrayList by using reverse()
// method of Collections class.
import java.util.*;
public class Main {
public static void main(String[] args) {
// ArrayList Declaration
ArrayList al = new ArrayList();
// By using add() method to add few elements in
//ArrayList
al.add(10);
al.add(20);
al.add(30);
al.add(40);
al.add(50);
// Display ArrayList
System.out.print("Display ArrayList : " + " ");
System.out.println(al);
// By using reverse() method of Collections class is
// to reverse an ArrayList
Collections.reverse(al);
// Display Reverse ArrayList
System.out.print("Display Reverse ArrayList : " + " ");
System.out.println(al);
}
}
The output of the above example is:
Display ArrayList : [10, 20, 30, 40, 50]
Display Reverse ArrayList : [50, 40, 30, 20, 10]