Java

How to Reverse an Array in Java

Array reversing is one of the most significant operations in Java, therefore Java provides multiple methods that can be adopted to reverse an array such as swapping the array elements, reverse() method of Collections class, append() method of Stringbuilder class, etc. Moreover, we can use the for-loop and traverse it in reverse order to print the array elements in reverse order.

This write-up will provide a complete understanding of how to reverse an array in java, and in this regard, it will cover the following ways of reversing an array:

Let’s get started!

How to Print an Array in Reverse Order

To print a reverse array, we can use the simple for loop and traverse it in reverse order (i.e., start it from the last index). It wouldn’t reverse the array’s order; instead, it will only print/display the array in reverse order.

Example

In the below snippet, we will create an integer type array of ten elements, and we will print it in reverse order:

public class RevArrayExample {

    public static void main(String[] args) {
        int arry[] = new int[]{15, 72, 33, 54, 75, 32, 33, 65, 67, 100};
        System.out.println("Reverse Order Array: ");
        for (inti = arry.length - 1; i>= 0; i--) {
            System.out.print(arry[i] + " ");
        }
    }
}

In the above snippet, we initialize the loop with “arry.length – 1”, which represents that loop will start from the last index of the array. The condition “i >= 0” represents that the loop will terminate when the value of “i” becomes less than zero while “i–” means in each iteration the value of “i” will decrement by 1:

The above snippet shows that the array is successfully printed into reverse order.

How to Reverse an Array Using Collections.reverse() Method

In Java, Collections class provides a rverse() method which can be used to reverse the java arrays.

Example

In this example, we will create a string type array of five elements, and we will print it in reverse order using Collections.reverse() method:

public class RevArrayExample {

    static void reverseArray(String ary[]) {
        Collections.reverse(Arrays.asList(ary));
        System.out.println("Reverse Array: ");
        System.out.println(Arrays.asList(ary));
    }
    public static void main(String[] args) {
        String[] arry = {"Java", "PHP", "C#", "C++", "JavaScript"};
        reverseArray(arry);
}
}

We created a reverseArray function to reverse the original array. Within the reverseArray function, we utilized the asList() method of the Arrays class to convert the array into a list. Then we passed it to the Collections.reverse() method, which will reverse the order of the original array:

The output authenticates the working of the Collections.reverse() method as it succeeded to reverse the original array.

How to Reverse an Array Using Swapping

In java, another frequently used way to reverse the array is by swapping its elements.

Example

The below snippet will let you understand how to reverse an array by swapping its elements in Java:

public class RevArrayExample {

    static void reverseArray(int ary[]) {
        int len = ary.length;
        int temp;
        for (inti = 0; i<len / 2; i++) {
            temp = ary[i];
            ary[i] = ary[len - i - 1];
            ary[len - i - 1] = temp;
        }
        System.out.println("Reversed Array: ");
        for (int j = 0; j <len; j++) {
            System.out.println(ary[j]);
        }
    }
    public static void main(String[] args) {
        int[] arry = {40, 34, 63, 14, 5};
        reverseArray(arry);
    }
}

In this example, we swapped the array’s first element with the last element; array’s second element with the second last element, and so on:

The output verified that the swapping process successfully reversed the array elements.

Conclusion

In Java, Collections.reverse() method, StringBuilder.append() method, swapping approach, etc. are used to print an array in reverse order. Moreover, to print an array in reverse order, we can use the traditional for loop and traverse it in reverse order. However, it wouldn’t reverse the array order; instead, it will only print the array in reverse order. In this write-up we discussed three different techniques to reverse an array in Java. For profound understanding, we utilized different data types such as String, integer etc, and presented the descriptive screenshots of the code snippets.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.