Print array in reverse order java: In the previous article, we have seen Java Program to Shuffle a Given Array of Integers
In this article we are going to see how we can print the elements of an array in reverse order by using various ways in Java.
Java Program to Print the Elements of an Array in Reverse Order
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to print an array in reverse order.
Method-1: Java Program to Print an Array in Reverse Order By Using For Loop(Static Input)
Approach:
- Use a for loop to iterate the array index from last index to first index.
- Print the array elements at those index.
Program:
public class array { public static void main(String args[]) { // Array with elements int arr[] = {10,15,1,30,50,7,1,0,0}; int row; System.out.print("The array elements in reverse order are : "); //Loop to print the elements for(row=arr.length-1;row>=0;row--) { System.out.print(arr[row]+" "); } } }
Output: The array elements in reverse order are: 0 0 1 7 50 30 1 15 10
Method-2: Java Program to Print an Array in Reverse Order By Using For Loop(Dynamic Input)
Approach:
- Ask the user for array length.
- Store it in a variable.
- Use a for loop to iterate the index and insert the elements.
- Use a for loop iterate the array from last index to first index and print all the elements.
Program:
import java.util.Scanner; public class array { public static void main(String args[]) { Scanner scan = new Scanner(System.in); // Array with elements int arr[] = null; System.out.println("Enter the length of the array : "); int length = scan.nextInt(); arr = new int[length]; int iter; // Entering the array elements System.out.println("Enter the array elements : "); for(iter=0;iter<arr.length;iter++) arr[iter]=scan.nextInt(); System.out.println("The array in reverse order is : "); //For Loop to print the elements for(iter=arr.length-1;iter>=0;iter--) { System.out.print(arr[iter]+","); } } }
Output: Enter the length of the array : 5 Enter the array elements : 1 2 3 4 5 The array in reverse order is : 5,4,3,2,1,
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Related Java Programs: