Java Program to Reverse an Array Without Using Another Array

Reversing elements in an array is a common introductory challenge for programmers. Usually, the task involves using a temporary array to store the reversed data. But what if you could reverse the array in-place without the need for additional storage? Not only is this efficient, but it also offers a great introduction to algorithmic thinking. In this post, we'll guide beginners through a Java program that reverses an array without using another array. 

Basic Concept

We'll swap elements symmetrically from the start and end of the array, gradually moving towards the center, until the entire array is reversed. 

Java Program to Reverse an Array Without Using Another Array

import java.util.Scanner;

public class ArrayReverser {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the size of the array:");
        int size = scanner.nextInt();

        int[] array = new int[size];
        System.out.println("Enter " + size + " elements:");
        for (int i = 0; i < size; i++) {
            array[i] = scanner.nextInt();
        }

        reverseArray(array);
        System.out.println("Reversed array:");
        for (int num : array) {
            System.out.print(num + " ");
        }
    }

    public static void reverseArray(int[] array) {
        int startIndex = 0;
        int endIndex = array.length - 1;

        while (startIndex < endIndex) {
            // Swap the values
            int temp = array[startIndex];
            array[startIndex] = array[endIndex];
            array[endIndex] = temp;

            // Move indices towards the center
            startIndex++;
            endIndex--;
        }
    }
}

Output:

Enter the size of the array:
5
Enter 5 elements:
1 2 3 4 5
Reversed array:
5 4 3 2 1 

Step by Step Explanation: 

Taking User Input: 

        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the size of the array:");
        int size = scanner.nextInt();

We begin by taking the size of the array and its elements from the user using the Scanner class. 

Swapping Logic: The core of this reversal logic is the symmetric swapping of elements. Consider the array [1, 2, 3, 4, 5]. The first swap will exchange 1 and 5, the second will swap 2 and 4, and the middle element 3 remains unchanged. 

Loop for Reversal: 

        while (startIndex < endIndex) {
            // Swap the values
            int temp = array[startIndex];
            array[startIndex] = array[endIndex];
            array[endIndex] = temp;

            // Move indices towards the center
            startIndex++;
            endIndex--;
        }

We start with startIndex at the beginning of the array and endIndex at the end. We then swap the elements at these indices. After that, we increment the startIndex and decrement the endIndex, effectively moving them towards the center of the array. 

Displaying the Result: After processing the entire array, we loop through it to print the reversed elements.

Conclusion

In this blog post, we learned how to write a Java program to reverse an Array without using another Array. Happy coding!.

Comments