Java program to reverse array using Collections
Program
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;
public class ReverseArrayUsingCollections {
public static void main(String[] args) {
System.out.println("Enter the required size of the array: ");
Scanner reader = new Scanner(System.in);
int size = reader.nextInt();
Integer inputArray[] = new Integer[size];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < size; i++) {
inputArray[i] = reader.nextInt();
}
System.out.println("Array in reverse order: ");
Collections.reverse(Arrays.asList(inputArray));
System.out.println(Arrays.asList(inputArray));
}
}
Output
javac .\ReverseArrayUsingCollections.java
java ReverseArrayUsingCollections
Enter the required size of the array:
5
Enter the elements of the array:
100
200
300
400
500
Array in reverse order:
[500, 400, 300, 200, 100]