Array Java Programs

Print Array in Reverse Order

In this tutorial we will learn to write a Java program to create an array and return the elements in reverse order.

Our program will first input the size of the array and then the elements of the array. And return the reverse order of the array you input.

For example

Case 1: If the user enters 4 as the array (list) size and the array (list) elements as 1,2,3,4, the output should be 4,3,2,1.

Case 2: If the user enters 5 as the array (list) size and the array (list) elements as 9,8,7,6,5, the output should be 5,6,7,8,9.

Print Array in Reverse Order using For Loop in Java

import java.util.*;

public class ArrayExample {
	public static void main(String[] args) {
		int n;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter the number of elements you want in array: ");
		n = sc.nextInt();
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
			System.out.print("Please give value for index " + i + " : ");
			arr[i] = sc.nextInt();
		}
		System.out.println("Our original array: ");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
		System.out.println();
		System.out.println("Array after reverse: ");
		for (int i = arr.length - 1; i >= 0; i--) {
			System.out.print(arr[i] + " ");
		}
	}
}

Explanation

The input array size is 5, so the ‘for loop’ will executes the body 5 times taking input from the users as the elements of the array, which is {1, 2, 3, 4, 5}. The program returned the reverse of the input array i.e. {5, 4, 3, 2, 1}.

Output

Enter the number of elements you want in array: 5
Please give value for index 0 : 1
Please give value for index 1 : 2
Please give value for index 2 : 3
Please give value for index 3 : 4
Please give value for index 4 : 5
Our original array: 
1 2 3 4 5 
Array after reverse: 
5 4 3 2 1 

Print Array in Reverse Order using While Loop in Java

import java.util.*;

public class ArrayExample {
	public static void main(String[] args) {
		int startIndex, lastIndex;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter the size of array: ");
		int size = sc.nextInt();
		int arr[] = new int[size];
		int reverse[] = new int[size];
		for (int i = 0; i < size; i++) {
			System.out.print("Please give value for index " + i + " : ");
			arr[i] = sc.nextInt();
		}
		startIndex = 0;
		lastIndex = size - 1;
		while (lastIndex >= 0) {
			reverse[startIndex] = arr[lastIndex];
			startIndex++;
			lastIndex--;
		}
		System.out.println("Array After Reversing :");
		for (int i = 0; i < size; i++) {
			System.out.println(reverse[i]);
		}
	}
}

Explanation

The input array size is 5, so the ‘for loop’ will executes the body 5 times taking input from the users as the elements of the array, which is {1, 2, 3, 4, 5}. The program returned the reverse of the input array i.e. {5, 4, 3, 2, 1}.

Output

Enter the size of array: 5
Please give value for index 0 : 1
Please give value for index 1 : 2
Please give value for index 2 : 3
Please give value for index 3 : 4
Please give value for index 4 : 5
Array After Reversing :
5
4
3
2
1

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs