To make the code simpler and also putting code reusability into consideration, you will require a method that reverses a given array. This method can be invoked any time in the main method.
The method can return anything or be void. For my case i used the void method, it takes an array and performs the reverse reversing operation. In the method i used the two pointer. The pointers "start" and "end" are initially set to the array's first and last indices, respectively. The loop continues until finish is larger than or equal to start. The elements at the start and end indices are switched around at each iteration, and the pointers are moved in the direction of the array's centre.
In the main method, using scanner, declare the size of the array and pass it to the array. Using a for loop pass the numbers you desire to the array i.e from the keyboard and map them to the indices of the array. Use another for loop to access the array(not reversed). Invoke the reverse array method and pass the array in the method. Use a for loop or for each loop to access the reversed array. for my case i used for each loop.
import java.util.Arrays;
import java.util.Scanner;
public class ReverseArray {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
//size of the array
System.out.println("Enter the size of you array:");
int size = sn.nextInt();
//create the array of size(size)
int[] numbers = new int[size];
//create a for loop to ask user to input numbers at each index
System.out.println("Enter " + size + " numbers.");
for (int i = 0; i < size; i++) {
numbers[i] = sn.nextInt();
}
//display the original array
System.out.println("Original array:");
for (int elements : numbers)
System.out.print(elements + " ");
//call the reverse method to reverse the numbers/ array
reverseArray(numbers);
//display the reversed array
System.out.println();
System.out.println("Reversed array: ");
for (int nums : numbers) {
System.out.print(nums + " ");
}
sn.close();
}
//method to reverse the array
//it takes an array and performs the reversing operation
public static void reverseArray(int[] array) {
//starting point of the array...first index
int start = 0;
//endpoint or the last index
int end = array.length - 1;
while (start < end) {
//create a temporary variable
//this method is called the two pointers approach
int temp = array[start];
array[start] = array[end];
array[end] = temp;
//start increases while the end decreases hence
start++;
end--;
}
}
}