BM Balmiki Mandal 06 May 2023 0 views 0 comments Core Java

Reverse an ArrayList in Java - Step-by-Step Guide

Reverse an ArrayList in Java

In this article, we are going to discuss about reversing an ArrayList in Java. Reversing an ArrayList means to reverse the order of elements of the ArrayList. There are several ways to reverse an ArrayList in Java. In this article, we will go through some of the common approaches.

Using Collections.reverse() Method

The collections framework provides a method named reverse() on List interface which can be used to reverse an ArrayList. This method reverses the elements in the underlying list. The order of elements remains unaffected. This is the simplest and most efficient way to reverse an ArrayList.

import java.util.ArrayList; 
import java.util.Collections; 

public class ArraylistReverse { 
    public static void main(String[] args) 
    { 
        ArrayList colors = new ArrayList<>(); 

        colors.add("green"); 
        colors.add("red"); 
        colors.add("blue"); 
        colors.add("yellow"); 
        colors.add("white"); 

        System.out.println("Original List: " + colors); 

        Collections.reverse(colors); 

        System.out.println("Reverse List: " + colors); 
    } 
}

Output:

Original List: [green, red, blue, yellow, white] 
Reverse List: [white, yellow, blue, red, green] 

Using for loop

We can reverse an ArrayList using a simple for loop. We can swap the first element with the last element, second element with the last second element and so on up to the middle of the list. This approach is efficient but requires us to write more code as compared to using Collections.reverse() method.

import java.util.ArrayList; 

public class ArraylistReverse {

    public static void main(String[] args) 
    { 
        ArrayList colors = new ArrayList<>(); 

        colors.add("green"); 
        colors.add("red"); 
        colors.add("blue"); 
        colors.add("yellow"); 
        colors.add("white"); 

        System.out.println("Original List: " + colors); 

        int size = colors.size(); 

        for (int i = 0; i < size / 2; i++) { 
            String tmp = colors.get(i); 
            colors.set(i, colors.get(size - i - 1)); 
            colors.set(size - i - 1, tmp); 
        } 

        System.out.println("Reverse List: " + colors); 
    } 
}

Output:

Original List: [green, red, blue, yellow, white] 
Reverse List: [white, yellow, blue, red, green] 

Using Recursion

This is another efficient approach to reverse an ArrayList. We can reverse the order of elements of the ArrayList by calling a recursive function which swaps the first element with the last element and then calls the recursive funtion on the sublist excluding first and last elements. This approach is more efficient but requires more understanding.

import java.util.ArrayList; 

public class ArraylistReverse {

    public static void main(String[] args) 
    { 
        ArrayList colors = new ArrayList<>(); 

        colors.add("green"); 
        colors.add("red"); 
        colors.add("blue"); 
        colors.add("yellow"); 
        colors.add("white"); 

        System.out.println("Original List: " + colors); 

        reverse(colors, 0 , colors.size()-1); 

        System.out.println("Reverse List: " + colors); 
    } 

    public static void reverse(ArrayList list, int start, int end) 
    { 
        if (start >= end) 
            return; 

        String temp = list.get(start); 
        list.set(start, list.get(end)); 
        list.set(end, temp); 

        reverse(list, start + 1, end - 1); 
    } 
}

Output:

Original List: [green, red, blue, yellow, white] 
Reverse List: [white, yellow, blue, red, green] 

These are some of the approaches to reverse an ArrayList in Java. As we can see all these approaches have their own use cases, so it is important to understand the requirements before choosing an approach.

0 Comments

No comments yet, Be the first to comment.

Post a Comment

Please sign in to post a comment.

Related Posts

Unlock the Ins and Outs of JavaScript Engines and Runtimes

What is a JavaScript Engine? A JavaScript engine is a program or interpreter which executes JavaScript code....

BM Balmiki Mandal 05 Sep 2023
Implementing HMACSHA256 Algorithm in Java with Key

Introducing HMACSHA256 in Java with Key HMACSHA256 is an authentication algorithm designed to secure data, su...

BM Balmiki Mandal 30 Aug 2023
HMACSHA256 Java Example - Creating Secure Message Authentication

HMACSHA256 Java Example HMACSHA256 is an advanced encryption algorithm that allows developers to create encry...

BM Balmiki Mandal 30 Aug 2023