Home Tutorial Java Core Java Reverse integer array

 
 

Java Reverse integer array
Posted on: December 7, 2012 at 12:00 AM
Advertisement
In this tutorial, you will learn how to reverse integer array.

Java Reverse integer array

In this tutorial, you will learn how to reverse integer array. Here, we have created a method to reverse an array which has been passed as a parameter and returns a new array with all the elements in reverse order. In the main method, we have allowed the user to enter numbers and invoked the method.

Example:

import java.util.*;
class ReverseArray{
public static int [] reverse (int a[]){
for ( int i = 0 ; i < a.length/2 ; i++ ){
int temp = a[i] ;
a[i] = a[a.length-i-1] ;
a[a.length-i-1] = temp ;

}
return a;
}

public static void main(String[] args) 
{
int a[]=new int[5];
Scanner input=new Scanner(System.in);
System.out.println("Enter five array elements: ");
for(int i=0;i<a.length;i++){
a[i]=input.nextInt();
}
int array[]=reverse(a);
System.out.println("Array In Reverse Order: ");
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}
}

Output:

Enter five array elements:
3
5
6
2
4
Array In Reverse Order:
4
2
6
5
3
Advertisement

Related Tags for Java Reverse integer array:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 7, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com