Input : {3, 8, 5, 7, 4}
Output : {4, 7, 5, 8, 3}
Input : {10, 54, 23, 89, 97, 2}
Output : {2, 97, 89, 23, 54, 10}
Read Also : How to reverse a String in java with Example
The input Array can be a collection of :
1. int primitive data type Array (Method 1)
2. Integer Array (Method2)
3. String Array (Method 3)
Method 1: Code to Reverse int Array in Java
1. Swap the values of left indices with the right indices till the mid element.Please find the reverse an array in java image below for method 1:
Reverse Array in Java Example |
import java.util.*; public class ReverseArray { public static void main (String[] args) throws java.lang.Exception {
// Given input array
int[] inputArray = {3,7,9,6,4};
// Print array before reverse
System.out.println("Array without reverse" +
Arrays.toString(inputArray));
// Calling method to swap elements
reverseArray(inputArray); } public static void reverseArray(int[] inputArray) { for (int left = 0, right = inputArray.length - 1;
left < right; left++, right--) { // swap the values at the left and right indices int temp = inputArray[left]; inputArray[left] = inputArray[right]; inputArray[right] = temp; }
// Printing the Array after reverse
System.out.print("Reverse Array :"); for(int val : inputArray) System.out.print(" "+val); } }
Output :
Array without reverse[3, 7, 9, 6, 4]
Reverse Array : 4 6 9 7 3
Method 2: Code to Reverse Integer Array in Java
1. Convert the Integer Array to the list using Arrays.asList() method.2. Reverse the list using Collections.reverse() method
3. Convert the list back to the array using list.toArray() method.
import java.util.*; public class ReverseArray { public static void main (String[] args) throws java.lang.Exception { // Given input array Integer[] inputArray = {3,7,9,6,4}; // Print array before reverse System.out.println("Array without reverse" + Arrays.toString(inputArray)); // Calling method to swap elements reverseArray(inputArray); } public static void reverseArray(Integer[] arr) {
// Converting Array to List
List<Integer> list = Arrays.asList(arr);
// Reversing the list using Collections.reverse() method
Collections.reverse(list);
// Printing the reverse Array
System.out.print("Reverse Array :"); for(Integer val : list) System.out.print(" "+val); } }
Output :
Array without reverse[3, 7, 9, 6, 4]
Reverse Array : 4 6 9 7 3
Method 3: Code to Reverse String Array in Java
1. Convert the String Array to the list using Arrays.asList() method.2. Reverse the list using Collections.reverse() method
3. Convert the list back to the array using list.toArray() method.
import java.util.*; public class ReverseArray { public static void main (String[] args) throws java.lang.Exception { // Given input array String[] inputArray = {"India","USA","Germany","Australia"}; // Print array before reverse System.out.println("Array without reverse : " + Arrays.toString(inputArray)); // Calling method to swap elements reverseArray(inputArray); } public static void reverseArray(String[] arr) { // Converting Array to List List<String> list = Arrays.asList(arr); // Reversing the list using Collections.reverse() method Collections.reverse(list); // Converting list back to Array String[] reversedArray = list.toArray(arr); // Printing the reverse Array System.out.print("Reverse Array : " + Arrays.toString(reversedArray)); } }
Output :
Array without reverse : [India, USA, Germany, Australia]
Reverse Array : [Australia, Germany, USA, India]
Please mention in the comments in case if you have any questions regarding reverse array in java.