How do I reverse an int array in Java?
Categories:
Mastering Array Reversal in Java: Techniques and Best Practices

Learn various efficient methods to reverse an integer array in Java, from basic loops to advanced API usage, and understand their performance implications.
Reversing an array is a common programming task that involves rearranging its elements in the opposite order. For an integer array in Java, there are several approaches you can take, each with its own advantages and use cases. This article will explore the most common and efficient methods, providing clear code examples and explanations to help you choose the best technique for your specific needs.
Method 1: Two-Pointer Iteration (In-Place Reversal)
The two-pointer approach is a classic and highly efficient method for reversing an array in-place, meaning it modifies the original array without requiring additional memory proportional to the array's size. It works by initializing two pointers: one at the beginning of the array (left) and one at the end (right). The elements at these pointers are then swapped, and the pointers move towards the center until they meet or cross. This method is generally preferred for its optimal space complexity.
public class ArrayReversal {
public static void reverseArrayTwoPointer(int[] arr) {
int left = 0;
int right = arr.length - 1;
while (left < right) {
// Swap elements at left and right pointers
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
// Move pointers towards the center
left++;
right--;
}
}
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
System.out.println("Original array: " + java.util.Arrays.toString(numbers));
reverseArrayTwoPointer(numbers);
System.out.println("Reversed array: " + java.util.Arrays.toString(numbers));
int[] evenNumbers = {10, 20, 30, 40};
System.out.println("Original array: " + java.util.Arrays.toString(evenNumbers));
reverseArrayTwoPointer(evenNumbers);
System.out.println("Reversed array: " + java.util.Arrays.toString(evenNumbers));
}
}
Java code demonstrating in-place array reversal using the two-pointer method.
flowchart TD A[Start] --> B{Initialize left=0, right=arr.length-1} B --> C{Is left < right?} C -- Yes --> D[Swap arr[left] and arr[right]] D --> E[Increment left, Decrement right] E --> C C -- No --> F[End]
Flowchart of the two-pointer array reversal algorithm.
Method 2: Using Collections.reverse()
for List
While Java's primitive arrays (int[]
) don't directly support Collections.reverse()
, you can easily convert an int[]
to an Integer[]
(wrapper array) or a List<Integer>
, reverse it, and then convert it back if necessary. This method leverages the utility provided by the Java Collections Framework, offering a concise and readable solution, especially if you're already working with List
objects.
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class CollectionsReverse {
public static int[] reverseArrayUsingCollections(int[] arr) {
// Convert int[] to List<Integer>
List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());
// Reverse the list
Collections.reverse(list);
// Convert List<Integer> back to int[]
return list.stream().mapToInt(Integer::intValue).toArray();
}
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
System.out.println("Original array: " + java.util.Arrays.toString(numbers));
int[] reversedNumbers = reverseArrayUsingCollections(numbers);
System.out.println("Reversed array: " + java.util.Arrays.toString(reversedNumbers));
int[] evenNumbers = {10, 20, 30, 40};
System.out.println("Original array: " + java.util.Arrays.toString(evenNumbers));
int[] reversedEvenNumbers = reverseArrayUsingCollections(evenNumbers);
System.out.println("Reversed array: " + java.util.Arrays.toString(reversedEvenNumbers));
}
}
Reversing an int array by converting it to a List and using Collections.reverse()
.
Collections.reverse()
is convenient, converting between primitive arrays and List<Integer>
involves autoboxing/unboxing and stream operations, which can incur performance overhead and increased memory usage compared to the in-place two-pointer method, especially for very large arrays.Method 3: Creating a New Reversed Array
Another straightforward approach is to create a new array of the same size and populate it with elements from the original array in reverse order. This method does not modify the original array and is easy to understand. It's suitable when you need to preserve the original array or when the performance overhead of creating a new array is acceptable.
import java.util.Arrays;
public class NewArrayReversal {
public static int[] reverseIntoNewArray(int[] originalArr) {
int[] reversedArr = new int[originalArr.length];
for (int i = 0; i < originalArr.length; i++) {
reversedArr[i] = originalArr[originalArr.length - 1 - i];
}
return reversedArr;
}
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
System.out.println("Original array: " + Arrays.toString(numbers));
int[] reversedNumbers = reverseIntoNewArray(numbers);
System.out.println("Reversed array: " + Arrays.toString(reversedNumbers));
System.out.println("Original array (unchanged): " + Arrays.toString(numbers));
}
}
Java code to reverse an array by creating and populating a new array.