Introduction
Reversing an array is a common programming task that involves rearranging the elements of the array so that they appear in the opposite order. This task can be done efficiently in place, meaning that you can reverse the array without using an additional array. This guide will walk you through writing a Java program that reverses an array in place.
Problem Statement
Create a Java program that:
- Prompts the user to enter the size of an array and its elements.
- Reverses the array in place without using another array.
- Displays the reversed array.
Example:
- Input:
[1, 2, 3, 4, 5]
- Output:
"Reversed array: [5, 4, 3, 2, 1]"
Solution Steps
- Read the Array Size and Elements: Use the
Scanner
class to take the size and elements of the array as input from the user. - Reverse the Array In-Place: Use a loop to swap elements from the beginning and the end of the array until the middle of the array is reached.
- Display the Reversed Array: Print the reversed array.
Java Program
// Java Program to Reverse an Array Without Using Another Array // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class ReverseArrayInPlace { public static void main(String[] args) { // Step 1: Read the size and elements of the array from the user Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the array: "); int size = scanner.nextInt(); int[] array = new int[size]; System.out.println("Enter the elements of the array:"); for (int i = 0; i < size; i++) { array[i] = scanner.nextInt(); } // Step 2: Reverse the array in place reverseArray(array); // Step 3: Display the reversed array System.out.println("Reversed array:"); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } // Method to reverse the array in place public static void reverseArray(int[] array) { int n = array.length; for (int i = 0; i < n / 2; i++) { // Swap elements at positions i and (n - 1 - i) int temp = array[i]; array[i] = array[n - 1 - i]; array[n - 1 - i] = temp; } } }
Explanation
Step 1: Read the Array Size and Elements
- The
Scanner
class is used to read the size of the array and its elements. ThenextInt()
method captures the size and each element.
Step 2: Reverse the Array In-Place
- The
reverseArray()
method performs the in-place reversal of the array:- The array is traversed up to its midpoint using a loop (
i < n / 2
), wheren
is the length of the array. - During each iteration, the element at position
i
is swapped with the element at positionn - 1 - i
. - The swapping is done using a temporary variable
temp
.
- The array is traversed up to its midpoint using a loop (
Step 3: Display the Reversed Array
- The program prints the reversed array using a
for
loop.
Output Example
Example 1:
Enter the size of the array: 5 Enter the elements of the array: 1 2 3 4 5 Reversed array: 5 4 3 2 1
Example 2:
Enter the size of the array: 4 Enter the elements of the array: 10 20 30 40 Reversed array: 40 30 20 10
Example 3:
Enter the size of the array: 6 Enter the elements of the array: 2 4 6 8 10 12 Reversed array: 12 10 8 6 4 2
Conclusion
This Java program demonstrates how to reverse an array in place without using another array. The program efficiently swaps elements within the array to reverse the order, covering essential concepts such as array manipulation, loops, and in-place operations. This exercise is valuable for understanding how to handle arrays in Java programming.