Prerequisite: Recursion in Java
In the previous article, we have discussed about Java Program to Find LCM by Using Recursion
In this program we are going to see how to reverse an Array by using Recursion by Java programming language.
Java Program to Reverse an Array by Using Recursion
Lets assume there is an array say A[ ] which has 5 elements {77, 82, 100, 17, 95}
So the reverse of A[] = {95, 17, 100, 82, 77}
Now let’s see different ways to reverse an Array by using Recursion.
Method-1: Java Program to Reverse an Array By Using Static Input and Recursion
Approach:
- Declare and initiate an integer array ‘
A[]
’ - Call a user defined method
reverseArray()
and pass the array ‘A[]
’ with first index ‘0
’ and last index ‘A.length-1
’ of the array as parameter. - Inside the user defined method, check if the first index is less than the last index if true then swap the elements else call
reverseArray()
method recursively likereverseArray(a, i+1, j-1)
. - Now the value of the user defined method
reverseArray()
is stored in an integer array say ‘B[]
’. - Print each elements in the array
B[]
using for each loop.
Program:
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { //declare and initialize an integer array A[] int A[] = {0,1,2,3,4,5,6,7,8}; //calling the user defined method reverseArray() //and store the result value inside an integer array say ‘B[]’ int B[] = reverseArray(A,0,A.length-1); //display the array after reversing elements. System.out.println("The elements in reverse array are"); for(int i:B) System.out.print(i+" "); } static int[] reverseArray(int[] a,int i,int j) { //check if 1st index is less than last index if(i<j) { //swap elements a[i],a[j] int temp=a[i]; a[i]=a[j]; a[j]=temp; // reverse the array by calling the reverseArray() method recursively. reverseArray(a, i+1, j-1); } return a; } }
Output: The elements in reverse array are 8 7 6 5 4 3 2 1 0
Method-2: Java Program to Reverse an Array By Using User Input and Recursion
Approach:
- Create a scanner class.
- Declare an integer variable say ‘
n
’ and prompt the user to enter it’s value, which is the size of the array. - Declare an integer array say ‘
A[]
’ - Prompt the user to enter the elements inside the array.
- The elements inside the Array A[] are added in sequential index position using a for loop. Where the loop starts
from i=0 to i<n
and the loop is incremented by 1. - Call a user defined method
reverseArray()
and pass the array ‘A[]
’ with first index ‘0
’ and last index ‘A.length-1
’ of the array as parameter. - Inside the user defined method, check if the first index is less than the last index if true then swap the elements else call
reverseArray()
method recursively likereverseArray(a, i+1, j-1)
. - Now the value of the user defined method
reverseArray()
is stored in an integer array say ‘B[]
’. - Print each elements in the array
B[]
using for each loop.
Program:
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { //create a scanner class object Scanner s = new Scanner(System.in); System.out.println("Enter no. of elements you want in array:"); //declare the size of the array int n = s.nextInt(); // declare the array and initialize it with user input int A[] = new int[n]; System.out.println("Enter all the elements:"); //get all the elements sequentially for(int i = 0; i < n; i++) A[i] = s.nextInt(); //calling the user defined method reverseArray() //and store the result value inside an integer array say ‘B[]’ int B[] = reverseArray(A,0,A.length-1); //print reversed array elements by using for-each loop System.out.println("The elements in reverse array are"); for(int i:B) System.out.print(i+" "); } static int[] reverseArray(int[] a,int i,int j) { //check if 1st index is less than last index if(i<j) { //swap elements a[i],a[j] int temp=a[i]; a[i]=a[j]; a[j]=temp; // reverse the array by calling the reverseArray() method recursively. reverseArray(a, i+1, j-1); } return a; } }
Output: Enter no. of elements you want in array: 7 Enter all the elements: 12 36 25 45 69 78 17 The elements in reverse array are 17 78 69 45 25 36 12
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Related Java Programs: