Write a
java program to accept ‘n’ integers from the user & store them in an
ArrayList collection. Display the elements of ArrayList collection in reverse
order.
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class ReverseArrayList {
public static void
main(String[] args) {
Scanner scanner
= new Scanner(System.in);
ArrayList<Integer>
numberList = new ArrayList<Integer>();
System.out.print("Enter the number of integers (n): ");
int n =
scanner.nextInt();
System.out.println("Enter " + n + " integers:");
for (int i = 0;
i < n; i++) {
int num = scanner.nextInt();
numberList.add(num);
}
System.out.println("Elements in reverse order:");
for (int i =
numberList.size() - 1; i >= 0; i--) {
System.out.println(numberList.get(i));
}
scanner.close();
}
}
C:\Program Files\Java\jdk-11.0.17\bin>javac
ReverseArrayList.java
C:\Program Files\Java\jdk-11.0.17\bin>java ReverseArrayList
Enter the number of integers (n): 3
Enter 3 integers:
123
124
125
Elements in reverse order:
125
124
123