Reversing an array in java can be done in three simple methods.
Examples:
Input : 1, 2, 3, 4, 5 Output :5, 4, 3, 2, 1 Input : 10, 20, 30, 40 Output : 40, 30, 20, 10
The first method is as follows:
(i) Take input the size of array and the elements of array.
(ii) Consider a function reverse which takes the parameters-the array(say arr) and the size of the array(say n).
(iii) Inside the function, a new array (with the array size of the first array, arr) is initialized. The array arr[] is iterated from the first element and each element of array arr[] is placed in the new array from the back, i.e, the new array is iterated from its last element.
(iv) In this way, all the elements of the array arr[] are placed reversely in the new array.
(v) Further, we can iterate through the new array from the beginning and print the elements of the array.
/* Basic Java program that reverses an array*/ public class reverseArray { /* function that reverses array and stores it in another array*/ static void reverse(int a[], int n) { int[] b = new int[n]; int j = n; for (int i = 0; i < n; i++) { b[j - 1] = a[i]; j = j - 1; } /*printing the reversed array*/ System.out.println("Reversed array is: \n"); for (int k = 0; k < n; k++) { System.out.println(b[k]); } } public static void main(String[] args) { int [] arr = {10, 20, 30, 40, 50}; reverse(arr, arr.length); } } |
Reversed array is: 50 40 30 20 10
The second method uses the similar code for the inputting and printing of the array. However, we don’t create a new array like the above method. Instead, we reverse the original array itself. In this method we swap the elements of the array. The first element is swapped with the last element. The second element id swapped with the last but one element and so on.
For instance, consider array [1, 2, 3, …., n-2, n-1, n]. We swap 1 with n, 2 with n-1, 3 with n-2 and further.
/* Program that reverses array in less number of swaps*/ public class arrayReverse { /*function swaps the array's first element with last element, second element with last second element and so on*/ static void reverse(int a[], int n) { int i, k, t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } /*printing the reversed array*/ System.out.println("Reversed array is: \n"); for (k = 0; k < n; k++) { System.out.println(a[k]); } } public static void main(String[] args) { int [] arr = {10, 20, 30, 40, 50}; reverse(arr, arr.length); } } |
Reversed array is: 50 40 30 20 10
The third method is to use the function java.util.Collections.reverse(List list) method. This method reverses the elements in the specified list. Hence, we convert the array into a list first by using java.util.Arrays.asList(array) and then reverse the list.
// Reversing an array using Java collections. import java.util.*; public class reversingArray { /*function reverses the elements of the array*/ static void reverse(Integer a[]) { Collections.reverse(Arrays.asList(a)); System.out.println(Arrays.asList(a)); } public static void main(String[] args) { Integer [] arr = {10, 20, 30, 40, 50}; reverse(arr); } } |
[50, 40, 30, 20, 10]
Recommended Posts:
- Java lang.Long.reverse() method in Java with Examples
- Perl | Reverse an array
- Reverse a string in Java
- Reverse an ArrayList in Java
- Reverse a LinkedList in Java
- StringBuilder reverse() in Java with Examples
- Reverse words in a given String in Java
- Collections.reverse() in Java with Examples
- Integer reverse() Method In Java
- How to create a TreeMap in reverse order in Java
- Reverse elements of a Parallel Stream in Java
- StringBuffer reverse() Method in Java with Examples
- Set to Array in Java
- Anonymous array in Java
- Array of ArrayList in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



