Reverse an Array in Java

Reverse an Array in Java | Array Programs in Java – 16 | In the previous Java program, we developed programs to find the second largest number in the array and sort an array in Java. In this post, we will see how to reverse an array in Java. We will discuss different techniques like how to reverse an array in Java using for loop, without using another array in java, or using recursion.

Example:-

Original array:- { 10, 20, 30, 40, 50 }
Reverse of array:- { 50, 40, 30, 20, 10 }


Reverse an Array Using Another Array

Procedure to reverse an array using another array and loop,

a) Take an array, assume realArr
b) Find the length of the original array. See:- How to find the length of an array in Java
c) Declare another array having the same length, reverseArr
d) From realArr, select from the last and insert to reverseArr from the start
e) Repeat (d) until the end of the realArr

An example of this procedure,

int realArr[] = { 10, 20, 30 }; // original array
int reverseArr[3]; // new array

Then,
reverseArr[0] = realArr[2];
reverseArr[1] = realArr[1];
reverseArr[2] = realArr[0];

Finally, the reverse of the array:-
reverseArr[] = { 30, 20, 10 };

Now, let us Java program to reverse an array using while loop, and another array. In place of while loop, you can use another loop like for loop, or do-while loop.

import java.util.Arrays;
import java.util.Scanner;

public class ArrayTest {
  
  // method to reverse an array using another array
  public static int[] reverse(int[] realArr) {
    // declare variables
    int size = 0;
    int reverseArr[] = null;
    
    // find length of the given array
    size = realArr.length;
    
    // temporary array of the same size
    reverseArr = new int[size];
    
    // find reverse and store to temporary array
    // initialize iterator variables
    int i = 0;
    int j = size-1;
    
    while(i < size) {
      // assign element
      reverseArr[i] = realArr[j];
      
      // update iterator variables
      i++;
      j--;
    }
        
    // return result 
    return reverseArr;
  }

  public static void main(String[] args) {
    // declare variables
    Scanner scan = null;
    int size = 0;
    int numbers[] = null;
    int rev[] = null;
    
    // create Scanner class object to take input
    scan = new Scanner(System.in);
    
    // read array size
    System.out.print("Enter array size: ");
    size = scan.nextInt();
    
    // assign length to array
    numbers = new int[size];
    
    // read array elements 
    System.out.println("Enter array elements: ");
    for(int i=0; i<size; i++) {
      numbers[i] = scan.nextInt();
    }
    
    // find reverse and store to rev
    rev = reverse(numbers);
    
    // display reverse of the array
    System.out.println("Reverse = " + Arrays.toString(rev));
    
    // close Scanner 
    scan.close();
  }
}

Output:-

Enter array size: 5
Enter array elements:
10 20 30 40 50
Reverse = [50, 40, 30, 20, 10]

Reverse an Array in Java using For Loop

In the above program, we had used the while loop. Now, let us see the same method using the for loop.

While loop is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated. The for loop is also a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed.

// method to reverse an array using for loop
public static int[] reverse(int[] realArr) {
   // declare variables
   int size = 0;
   int reverseArr[] = null;
    
   // find length of the given array
   size = realArr.length;
    
   // temporary array of the same size
   reverseArr = new int[size];
    
   // find reverse and store to temporary array
   for(int i=0, j=size-1; i<size; i++, j--) {
      // assign element
      reverseArr[i] = realArr[j];
   }
        
   // return result 
   return reverseArr;
}

Instead of the previous method which was using a while loop, use this above method. The procedure of both methods is exactly the same, the only difference is the use of a loop. The for loop reduced the lines of code compared to the while loop.

Reverse an Array In Java Using While Loop

// find reverse and store to temporary array
// initialize iterator variables
int i = 0;
int j = size-1;
    
while(i < size) {
   // assign element
   reverseArr[i] = realArr[j];
      
   // update iterator variables
   i++; // increase i 
   j--; // decrease j
}

Using for loop,

// find reverse and store to temporary array
for(int i=0, j=size-1; i<size; i++, j--) {
   // assign element
   reverseArr[i] = realArr[j];
}

Reverse an Array Without Using Another Array In Java

It is also possible to don’t use another array. Using the original array only, we can reverse the array. In this way, we need to swap the corresponding elements from first and last.

Procedure to reverse an array using the same array,
a) Take an array, assume arr
b) Find the length of the array
c) Select the first and last element of the array and swap them
d) Repeat this process until length/2

If the length of the array is even then all elements of the array must swap with a relative element, but if the length of the array is odd then the element at the center position will remain the same.

Example

Example using an array of Even length,
arr[] = {10,20,30,40}; // original array
Now, swap arr[0] and arr[3] then
arr[] = {40,20,30,10};
Again, swap arr[1] and arr[2] then
arr[] = {40,30,20,10; // final result

Example using an array of Odd length,
arr[] = {10,20,30,40,50}; // original array
Now, swap arr[0] and arr[4] then
arr[] = {50,20,30,40,10};
Again, swap arr[1] and arr[2] then
arr[] = {50,40,30,20,10}; // final result
No need to do anything with the element at the center position.

Now, let us see the Java method to find reverse of an array without using another array.

Reverse an Array In Java Using While Loop

Java method to reverse an array using while loop and without using another array.

// method to reverse an array without another array
public static int[] reverse(int[] arr) {
    
   // find size
   int size = arr.length;
    
   // variables
   int i = 0;
   int j = size - 1;

   // while loop
   while (i <= size / 2) {
      
     // swap elements
     int temp = arr[i];
     arr[i] = arr[j];
     arr[j] = temp;

     // update i & j
     i++; // increase i
     j--; // decrease j
   }
    
   // return result
   return arr;
}

Using for loop

Java method to reverse an array using for loop and without using another array.

// method to reverse an array without another array
public static int[] reverse(int[] arr) {

   // find size
   int size = arr.length;

   // for loop
   for(int i=0, j=size-1; i<(size/2); i++, j--) {

      // swap
      int temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
   } 

   // return result 
   return arr;
}

Reverse an Array in Java Using Recursion

We can reverse an array using the recursion technique. A method that contains a call to itself is called the recursive method. A technique of defining the recursive method is called recursion. The recursive method allows us to divide the complex problem into identical single simple cases that can handle easily. This is also a well-known computer programming technique: divide and conquer.

// method to reverse an array
public static int[] reverse(int[] arr) {
   reverseArr(arr, 0, arr.length - 1);
   return arr;
}

// recursive method
public static void reverseArr(int[] x, int i, int j) {

   // swap
   if (i < j) {
     int tmp = x[i];
     x[i] = x[j];
     x[j] = tmp;

     // recursive call
     reverseArr(x, ++i, --j);
   }
}

The above method doesn’t require another temporary array to store the values. The reverse() is not a recursive method, it internally calls reverseArr() which is a recursive method, and internally calls itself.

The time complexity of all the methods given in this post is:- O(N).

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *