Write a Java program to reverse an array using for loop, while loop, and functions. To reverse an array, we must create an entirely new one, for loop to iterate the items from last to first and add them to the new array.
Java Program to Reverse an Array using for loop
In this example, the for loop (for(i = Size – 1, j = 0; i >= 0; i–, j++)) iterates from bottom to top, assigning each value to another array. Next, we print that reverse array using another for loop.
package RemainingSimplePrograms;
import java.util.Scanner;
public class Example1 {
private static Scanner sc;
public static void main(String[] args) {
int Size, i, j;
sc = new Scanner(System.in);
System.out.print("Enter the size = ");
Size = sc.nextInt();
double[] a = new double[Size];
double[] b = new double[Size];
System.out.format("Enter %d elements = ", Size);
for(i = 0; i < Size; i++)
{
a[i] = sc.nextDouble();
}
for(i = Size - 1, j = 0; i >= 0; i--, j++)
{
b[j] = a[i];
}
System.out.println("\nThe Result");
for(i = 0; i < Size; i++)
{
System.out.print(b[i] + " ");
}
}
}

Instead of using another one in this Java example, we reversed and printed the Java array in a single Java for loop.
package RemainingSimplePrograms;
import java.util.Scanner;
public class ReverseAnArray3 {
private static Scanner sc;
public static void main(String[] args) {
int Size, i;
sc = new Scanner(System.in);
System.out.print("Enter the Array size = ");
Size = sc.nextInt();
double[] a = new double[Size];
System.out.format("Enter %d Array elements = ", Size);
for(i = 0; i < Size; i++)
{
a[i] = sc.nextDouble();
}
System.out.println("\nThe Result of the Reverse Array");
for(i = a.length - 1; i >= 0; i--)
{
System.out.print(a[i] + " ");
}
}
}

This program is an optimized version of reversing an array. Here, a for loop iterates up to half of the actual length. Next, we swap the first and last elements to reverse an existing array. No new array is created; we update the existing array with reversed elements.
import java.util.Arrays;
public class example {
public static void main(String[] args) {
int[] a = { 11, 33, 55, 77 };
int len = a.length;
for (int i = 0; i < len / 2; i++) {
int temp = a[i];
a[i] = a[len - 1 - i];
a[len - 1 - i] = temp;
}
System.out.println(Arrays.toString(a));
}
}
[77, 55, 33, 11]
Java program to reverse an array using a while loop
This example uses the while loop and temporary variable to swap the array items and reverse them. Refer to the while loop in Java.
package RemainingSimplePrograms;
import java.util.Scanner;
public class Example2 {
private static Scanner sc;
public static void main(String[] args) {
int Size, i, j;
double temp;
sc = new Scanner(System.in);
System.out.print("Enter the size = ");
Size = sc.nextInt();
double[] a = new double[Size];
System.out.format("Enter %d elements = ", Size);
for(i = 0; i < Size; i++)
{
a[i] = sc.nextDouble();
}
j = i - 1;
i = 0;
while(i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
System.out.println("\nThe Result");
for(i = 0; i < Size; i++)
{
System.out.print(a[i] + " ");
}
}
}
Enter the size = 6
Enter 6 elements = 11 98.4 29.6 12.9 14.4 12.1
The Result
12.1 14.4 12.9 29.6 98.4 11.0
Java Program to Reverse an Array using recursive functions
In this example, we created a revArr() function that accepts the start and end values. Within the Java If statement, the revArr(a, start + 1, end – 1) line calls the revArr() function recursively with an incremented start value and decreased end value.
package RemainingSimplePrograms;
import java.util.Scanner;
public class Example4 {
private static Scanner sc;
public static void main(String[] args) {
int Size, i;
sc = new Scanner(System.in);
System.out.print("Enter the size = ");
Size = sc.nextInt();
int[] a = new int[Size];
System.out.format("Enter %d elements = ", Size);
for(i = 0; i < Size; i++)
{
a[i] = sc.nextInt();
}
revArr(a, 0, Size - 1);
System.out.println("\nThe Result");
for(i = 0; i < Size; i++)
{
System.out.print(a[i] + " ");
}
}
public static void revArr(int[] a, int start, int end) {
int temp;
if(start < end)
{
temp = a[start];
a[start] = a[end];
a[end] = temp;
revArr(a, start + 1, end - 1);
}
}
}

Reverse an array using Collections.reverse()
In this program, we use Collections.reverse() method to reverse an array. As it only works on object types, we defined our array as an Integer object (not int).
import java.util.Arrays;
import java.util.Collections;
public class example {
public static void main(String[] args) {
Integer a[] = { 11, 33, 55, 77 };
Collections.reverse(Arrays.asList(a));
System.out.println(Arrays.toString(a));
}
}
[77, 55, 33, 11]
Program to reverse an array using a stack
In this program, we declare a Stack object and use the for loop and push() method to load array elements into the stack. Next, a for loop iterates over the stack, and pop() returns the last element. In each iteration, the last value is loaded into the existing array at the first position.
import java.util.Arrays;
import java.util.Stack;
public class example {
public static void main(String[] args) {
int[] a = { 11, 33, 55, 77, 99 };
Stack<Integer> s = new Stack<>();
for(int n: a) {
s.push(n);
}
for(int i =0; i < a.length; i++) {
a[i] = s.pop();
}
System.out.println(Arrays.toString(a));
}
}
[99, 77, 55, 33, 11]
Program to reverse an array using the Arrays.setAll() method
In this program, we use the built-in Arrays.setAll() method to assign the first index position to the last, and so on, to reverse an array’s elements.
import java.util.Arrays;
public class example {
public static void main(String[] args) {
int[] a = { 11, 33, 55, 77, 99 };
int len = a.length;
int[] b = new int[len];
Arrays.setAll(b, i -> a[len - 1 - i]);
System.out.println(Arrays.toString(b));
}
}
[99, 77, 55, 33, 11]
Also Visit