0
0
JavaProgramBeginner · 2 min read

Java Program to Reverse Array with Output and Explanation

To reverse an array in Java, use a loop to swap elements from the start and end moving towards the center, like for (int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; }.
📋

Examples

Input[1]
Output[1]
Input[1, 2, 3, 4, 5]
Output[5, 4, 3, 2, 1]
Input[]
Output[]
🧠

How to Think About It

To reverse an array, think of swapping the first element with the last, the second with the second last, and so on until you reach the middle. This way, the array is reversed in place without needing extra space.
📐

Algorithm

1
Get the input array.
2
Set two pointers: one at the start (left) and one at the end (right) of the array.
3
While left is less than right, swap the elements at left and right.
4
Move left pointer one step forward and right pointer one step backward.
5
Repeat until left is no longer less than right.
6
Return or print the reversed array.
💻

Code

java
public class ReverseArray {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        for (int i = 0; i < arr.length / 2; i++) {
            int temp = arr[i];
            arr[i] = arr[arr.length - 1 - i];
            arr[arr.length - 1 - i] = temp;
        }
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}
Output
5 4 3 2 1
🔍

Dry Run

Let's trace the array [1, 2, 3, 4, 5] through the code.

1

Initial array

arr = [1, 2, 3, 4, 5]

2

First iteration (i=0)

Swap arr[0] and arr[4]: arr = [5, 2, 3, 4, 1]

3

Second iteration (i=1)

Swap arr[1] and arr[3]: arr = [5, 4, 3, 2, 1]

4

Loop ends

i=2 is not less than arr.length/2 (2), stop.

iarr after swap
0[5, 2, 3, 4, 1]
1[5, 4, 3, 2, 1]
💡

Why This Works

Step 1: Swapping elements

We swap the element at the current index with the element at the corresponding position from the end using a temporary variable temp.

Step 2: Loop until middle

The loop runs only until the middle of the array because after that, elements would be swapped back to original positions.

Step 3: In-place reversal

This method reverses the array in place without using extra memory, making it efficient.

🔄

Alternative Approaches

Using a new array
java
public class ReverseArray {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int[] reversed = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            reversed[i] = arr[arr.length - 1 - i];
        }
        for (int num : reversed) {
            System.out.print(num + " ");
        }
    }
}
This uses extra space for a new array but keeps the original array unchanged.
Using Collections.reverse with Integer array
java
import java.util.Arrays;
import java.util.Collections;
public class ReverseArray {
    public static void main(String[] args) {
        Integer[] arr = {1, 2, 3, 4, 5};
        Collections.reverse(Arrays.asList(arr));
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}
This uses built-in methods but requires Integer objects instead of primitive int.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs half the length of the array, so time complexity is O(n) where n is the array size.

Space Complexity

The reversal is done in place using a temporary variable, so space complexity is O(1).

Which Approach is Fastest?

In-place swapping is fastest and uses least memory compared to creating a new array or using Collections.reverse.

ApproachTimeSpaceBest For
In-place swapO(n)O(1)Memory efficient, fast
New arrayO(n)O(n)Preserving original array
Collections.reverseO(n)O(n)Using built-in methods with objects
💡
Swap elements from start and end moving inward to reverse the array efficiently.
⚠️
Trying to swap elements without a temporary variable causes data loss.