# Reverse an Array in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Topics Covered

Arrays in Java are frequently used data structures that provide a way of storing multiple values of the same type in contiguous memory locations. Developers use arrays to process a large collection of information. They may come across several situations where they have to reverse the array.

## Introduction to Reverse an Array in Java

While developing a software application, developers may face several situations in which they need to process the data stored in an array, starting with the last element. In such cases, we have to reverse the array to make use of it.

For this process, we need to have knowledge of array reversal techniques. Consider the example given below:

In the above example, the array A1 contains the elements [1, 2, 3, 4, 5]. On reversing this array, we get an array A2, i.e., [5, 4, 3, 2, 1]. Note how the element 3, present in the middle of the array, remains in place. This effect is observable only in arrays with odd lengths. Now, let's look at some of the methods using which we can reverse an array in Java:

• Method 1: Reverse Array in Place.
• Method 2: Using Temporary array.
• Method 3 : Using Collections.reverse() method.
• Method 4 : Using StringBuilder.append() method.
• Method 5: Reverse an Array using for Loop.

## Method 1: Reverse Array in Place

The basic approach to reverse an array is to iterate through the array and swap the elements of the array in such a way that it reverses the array, i.e., swap the first element with the last element, the second element with the second last element, and so on until we reach the middle of the array.

In this way, the array will be reversed in place, i.e., this array reversal technique will not consume any additional space.

Code:

Output:

In the above program, the input array array = [1,2,3,4,5] is reversed by iterating over the array from the start to the middle element, along with swapping the elements in place.

Time Complexity - The time complexity of this algorithm is O(n) as the algorithm needs to iterate over half of all the array elements and perform n/2 swaps.

Space Complexity - The space complexity of the algorithm is O(1) as no additional space is used in array reversal.

## Method 2: Using Temp Array

Another approach that can be followed when we have fewer restrictions on space usage is to make use of a temporary array to store the elements of the original array in reverse order.

In this method, the input array is accessed in reverse order, and each element is stored in the temporary array, thereby forming the reversed array of the input.

Code:

Output:

In the above program, we are accessing the elements of the array in reverse order, i.e., from the last element to the first element, and are storing these elements in the temporary array. The temp array represents the reverse of the original input array.

Time Complexity - The time complexity of this algorithm is O(n) as we have to traverse the whole array.

Space Complexity - The space complexity of the algorithm is O(n) as the temporary array is of the same length as that of the original array.

## Method 3 : Using Collections.reverse() Method

In this method, we use the java.util.Collections.reverse(List list) function that reverses the elements of the specified List. The reverse() method accepts a collection as an argument. Hence, we can use the wrapper classes such as Integer or Float to define the input array and the java.util.Arrays.asList(array) function to convert the input array into a List before providing it to the reverse() method.

Code:

Output:

In the above program, we are using the reverse() method provided by the Collection framework to reverse the input array.

Time Complexity- The time complexity of this algorithm is O(n) as the Collections.reverse() method takes a mutable list as an argument and reverses its order in linear time.

Space Complexity - The Collections.reverse() method reverses the mutable list in-place. Hence, the space complexity of the algorithm is O(1).

## Method 4 : Using StringBuilder.append() Method

This method is used when we are dealing with String arrays. In this method, we traverse the array in reverse order and append the elements in a StringBuilder object. This StringBuilder object is converted back to a string which can be further split to retrieve the reversed string array.

Code:

Output:

In the above program, we are accessing the elements of the array in reverse order, i.e., from the last element to the first element, and are appending these elements in the StringBuilder object, which is converted back to a string array using the split() function.

Time Complexity - The time complexity of this algorithm is O(n) as we have to traverse the whole array to append the elements in the StringBuilder object.

Space Complexity - The space complexity of the algorithm is O(n) as we are creating a new string array after splitting the StringBuilder object.

## Method 5: Reverse An Array Using For Loop

This method is used when we wish to view the elements of the input array in reverse order rather than actually reversing the array. In this method, we can simply use a for loop to access and display the elements of the array from the rear end.

Code:

Output:

In the above program, we are accessing the elements of the array in reverse order along with simultaneously printing them on the screen.

Time Complexity - The time complexity of this algorithm is O(n) as we have to traverse the whole array to print the elements on the screen.

Space Complexity - The space complexity of the algorithm is O(1) as no extra space is used.

## Conclusion

• Array reversal techniques are important in software development as several situations may arise where we have to process the data stored in an array, starting with the last element.
• An array can be reversed using various methods in Java, such as:
• Using in-place reversal in which the elements are swapped to place them in reverse order.
• Using a temporary array to traverse and store the elements in reverse order.
• Using the in-built reverse() method of the Collections framework.
• Using StringBuilder.append() method in case of string arrays.
• Using for loop to simply view the elements in reverse order.