Java program to reverse an array
In this post, you will learn how to reverse an array in the Java programming language. In the development process, sometimes we need to process the array in reverse order. So that, the first element is placed in the last position, the second element is placed in the second last position, and so on. There are various approaches to reverse an array in Java. Here, we have mentioned most of them.
Algorithm
Step1: START Step2: INITIALIZE Array arr Step3: PRINT "Original Array:" Step4: REPEAT STEP 5 for(i=0; i=0; i--) Step8: PRINT a[i] Step9: END
Java program to reverse an array using for loop
Here, we have used the for loop to reverse an array in Java programming. This is the simplest approach, as it starts printing the array in reverse order without doing any processing with it.
import java.util.*;
import java.util.stream.*;
public class Main
{
public static void main(String[] args) {
Integer[] arr = {88,32,12,43,64,66,23,73,29};
//print array starting from first element
System.out.println("Original Array:");
for(int i=0;i<arr.length;i++)
System.out.print(arr[i] + " ");
System.out.println();
//print array starting from last element
System.out.println("Array in reverse order:");
for(int i=arr.length-1;i>=0;i--)
System.out.print(arr[i] + " ");
}
}
Output of the above code:
Original Array:
88 32 12 43 64 66 23 73 29
Array in reverse order:
29 73 23 66 64 43 12 32 88
Java program to reverse an array using Collections.reverse() method
We can use the 'reverse()' method present in the collections framework to reverse an array. It reverses the order of elements in a list passed as an argument. Before using this, we first need to convert an array to a list. This class is present in the java.util package.
import java.util.*;
public class Main {
/* define function to reverse the elements of the array*/
static void reverse(Integer arr[])
{
Collections.reverse(Arrays.asList(arr));
System.out.println("Reversed Array:" + Arrays.asList(arr));
}
public static void main(String[] args)
{
Integer [] arr = {43,12,55,64,62,15,92};
System.out.println("Original Array:" + Arrays.asList(arr));
reverse(arr);
}
}
Output of the above code:
Original Array:[43, 12, 55, 64, 62, 15, 92]
Reversed Array:[92, 15, 62, 64, 55, 12, 43]
Java program to reverse an array using temp
Here, we have used a variable temp to reverse the given array elements.
import java.util.Arrays;
public class Main {
// function to reverse array
static void reverseArray(int arr[], int size)
{
int i, k, temp;
for (i = 0; i < size / 2; i++) {
temp = arr[i];
arr[i] = arr[size - i - 1];
arr[size - i - 1] = temp;
}
// print the reversed array
System.out.println("Reversed Array: \n" + Arrays.toString(arr));
}
public static void main(String[] args)
{
int [] arr = {77,23,11,53,63,34,65,90,89};
//print the original array
System.out.println("Original Array: \n" + Arrays.toString(arr));
//function call to reverse the array
reverseArray(arr, arr.length);
}
}
Output of the above code:
Original Array:
[77, 23, 11, 53, 63, 34, 65, 90, 89]
Reversed Array:
[89, 90, 65, 34, 63, 53, 11, 23, 77]
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java