Java program to reverse an array – We will discuss the various methods to reverse an array in Java. The compiler has been added so that you can execute the programs by yourself, alongside few suitable examples and sample outputs. The reverse an array Java program has been written in Two different ways here.
- Without Using Another Array
- Using Class
Standard Array Reversal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.Scanner; class REV { public static void main(String[] args) { int n, res,i,j=0; Scanner s = new Scanner(System.in); System.out.print("Enter number of elements in the array:"); n = s.nextInt(); int array[] = new int[n]; int rev[] = new int[n]; System.out.println("Enter "+n+" elements "); for( i=0; i < n; i++) { array[i] = s.nextInt(); } System.out.println("Reverse of an array is :"); for( i=n;i>0 ; i--,j++) { rev[j] = array[i-1]; System.out.println(rev[j]); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Enter number of elements in the array:5 Enter 5 elements 1 2 3 4 5 Reverse of an array is : 5 4 3 2 1 |
Without Using Another Array
1) Insert the elements in to the array “array[]” using scanner class method s.nextInt().
2) To reverse the array we are interchanging the n-1 element with the i’th element by increasing i value and decreasing the n value until i<n.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.Scanner; class REV { public static void main(String[] args) { int n, temp,res,i,j=0; Scanner s = new Scanner(System.in); System.out.print("Enter number of elements in the array:"); n = s.nextInt(); int array[] = new int[n]; System.out.println("Enter "+n+" elements "); for( i=0; i < n; i++) { array[i] = s.nextInt(); } for( i=0;i<n ; i++,n--) { temp=array[i]; array[i]=array[n-1]; array[n-1]=temp; } System.out.println("Reverse of an array is :"); for( i=0; i < array.length; i++) { System.out.println(array[i]); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Enter number of elements in the array:6 Enter 6 elements 10 20 30 40 50 60 Reverse of an array is : 60 50 40 30 20 10 |
Using Class
1) The class “Reverse” will reverse the given array.
2) Reverse res=new Reverse(array,n), creating the Reverse class object res by passing the array, number of elements in the array. Then Reverse class constructor Reverse(int[] a,int n) will be executed.
- for i=length of the array to i>0 and j=0 to j<length of the array
- rev[j] = a[i-1]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.Scanner; class Reverse { int rev[],j=0; Reverse(int[] a,int n ) { rev=new int[n]; for(int i=n;i>0 ;i--,j++ ) { rev[j] = a[i-1]; } } } class REV { public static void main(String[] args) { int n,i,j=0; Scanner s = new Scanner(System.in); System.out.print("Enter number of elements in the array:"); n = s.nextInt(); int array[] = new int[n]; int rev[] = new int[n]; System.out.println("Enter "+n+" elements "); for( i=0; i < n; i++) { array[i] = s.nextInt(); } Reverse res=new Reverse(array,n); System.out.println("Reverse of an array is :"); for( i=0; i < n; i++) { System.out.println(res.rev[i]); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
Enter number of elements in the array:4 Enter 4 elements 5 9 15 30 Reverse of an array is : 30 15 9 5 |
More Java Programs Here: