How to correctly reverse an int array in Java?

What is the correct way to reverse an int array in Java?

I am trying to reverse an int array in Java, but my method doesn’t seem to work correctly. Here’s what I tried:

for(int i = 0; i < validData.length; i++) {  
    int temp = validData[i];  
    validData[i] = validData[validData.length - i - 1];  
    validData[validData.length - i - 1] = temp;  
}

What is wrong with this approach, and how can I properly reverse the array?

Fixing Your Swap Logic (Two-Pointer Approach)

You’re almost there, but there’s a small mistake in your loop condition! Right now, your loop runs through the entire array, which swaps elements twice, effectively keeping the array the same.

Fixed Version (Two-Pointer Swap):

public class ReverseArray {
    public static void main(String[] args) {
        int[] validData = {1, 2, 3, 4, 5};

        for (int i = 0; i < validData.length / 2; i++) {  
            int temp = validData[i];  
            validData[i] = validData[validData.length - i - 1];  
            validData[validData.length - i - 1] = temp;  
        }

        System.out.println(java.util.Arrays.toString(validData));  
        // Output: [5, 4, 3, 2, 1]
    }
}

:white_check_mark: What’s fixed?

  • The loop now runs only until the middle (validData.length / 2), preventing unnecessary swaps.
  • Uses a temp variable for efficient in-place swapping.

:hammer_and_wrench: Why use this approach?

  • In-place reversal—modifies the original array without extra space.
  • Time complexity: O(n), making it fast and efficient.

Using a New Array (If You Need a Separate Copy)

Great solution, But what if you don’t want to modify the original array? Here’s how you can create a new reversed array while keeping the original untouched.

Copying into a New Array:

import java.util.Arrays;

public class ReverseArray {
    public static void main(String[] args) {
        int[] validData = {1, 2, 3, 4, 5};
        int[] reversed = new int[validData.length];

        for (int i = 0; i < validData.length; i++) {
            reversed[i] = validData[validData.length - i - 1];
        }

        System.out.println(Arrays.toString(reversed));  
        // Output: [5, 4, 3, 2, 1]
    }
}

:white_check_mark: Why use this?

  • Preserves the original array while creating a reversed copy.
  • Useful when you need immutable data for debugging or functional programming.

:rotating_light: When to avoid?

  • Extra memory required (O(n) space complexity), so not ideal for large datasets.

Using Collections.reverse() (For Integer Array Conversion)

@richaaroy, I see your point! But what if you’re working with Integer[] instead of int[]? Java has a built-in way to handle this using Collections.reverse().

Code Using Collections.reverse():

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ReverseArray {
    public static void main(String[] args) {
        Integer[] validData = {1, 2, 3, 4, 5};

        List<Integer> list = Arrays.asList(validData);
        Collections.reverse(list);
        
        System.out.println(Arrays.toString(validData));  
        // Output: [5, 4, 3, 2, 1]
    }
}

:white_check_mark: Why use this?

  • Super readable—no need to manually swap elements.
  • Uses Java’s built-in Collections.reverse() for cleaner, shorter code.

:rotating_light: When to avoid?

  • Works only for Integer[], not for int[] (primitive arrays).
  • Modifies the original array (since Arrays.asList() returns a fixed-size list).