Untitled
unknown
plain_text
2 years ago
1.2 kB
11
Indexable
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Read user choice (I / S): ");
char choice = scanner.next().charAt(0);
if (choice == 'I' || choice == 'S') {
System.out.print("Read 'n' (number of items): ");
int n = scanner.nextInt();
ArrayList<Object> items = new ArrayList<>();
for (int i = 0; i < n; i++) {
System.out.print("Read item " + (i + 1) + ": ");
if (choice == 'I') {
int item = scanner.nextInt();
items.add(item);
} else {
String item = scanner.next();
items.add(item);
}
}
System.out.println("Items in reverse order: ");
for (int i = n - 1; i >= 0; i--) {
System.out.println(items.get(i));
}
} else {
System.out.println("Invalid Choice");
}
scanner.close();
}
}
Editor is loading...