There are multiple ways to reverse an array .One can reverse an array manually or by using built-in Java methods .
Approach 1 : Reverse an Array using a Loop
import java.util.Arrays;
public class ReverseArray {
public static void main(String args[] ) {
int arr[]=new int[] {3,5,7,9,13};
// reverse the array using loop
for(int i=(arr.length-1); i>=0;i–){
System.out.print(arr[i]+ ” “ );
}
}
}
Approach 2 : Using Collections.reverse() method
One can convert the array into a list by using Arrays.asList() and then use Collections.reverse() to reverse the array easily.
This method is for arrays of objects like Integer[].
import java.util.*;
public class ReverseArray{
//function to reverse the elements of the array
public static void reverse( Integer a[] ) {
Collections.reverse(Arrays.asList(a));
System.out.println(Arrays.asList(a));
}
public static void main(String args[] ) {
Integer [] arr= {3,5,7,9,13};
reverse(arr);
}
}
