Reversing an array is a classic problem often asked in Java interviews. In this tutorial, we'll reverse an array manually without using Collections.reverse()
or Arrays
utility classes.
🧮 Logic
We use two pointers: one starting from the beginning, and one from the end. Swap their values while moving toward the center.
✅ Java Program
public class ReverseArray {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
System.out.println("Original Array:");
printArray(arr);
reverseArray(arr);
System.out.println("Reversed Array:");
printArray(arr);
}
static void reverseArray(int[] arr) {
int start = 0, end = arr.length - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
static void printArray(int[] arr) {
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}
🧠Interview Tip
This question is often followed by variations like "reverse only even elements" or "reverse using recursion." Make sure to explain your swap logic clearly.
✅ Output
Original Array: 10 20 30 40 50 Reversed Array: 50 40 30 20 10
📌 Conclusion
We learned how to reverse an array in Java using simple loop logic and without relying on any built-in methods. This program builds your core understanding of arrays and data manipulation.