import java.util.*;

public class ArrayListDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> list = new ArrayList<>();
        int choice;

        do {
            choice = sc.nextInt();

            switch (choice) {
                case 1:
                    int numElements = sc.nextInt();
                    for (int i = 0; i < numElements; i++) {
                        list.add(sc.nextInt());
                    }
                    break;

                case 2:
                    if (list.isEmpty()) {
                        System.out.println("The list is empty. Please add elements first.");
                    } else {
                        int index = sc.nextInt();
                        if (index >= 0 && index < list.size()) {
                            int value = sc.nextInt();
                            list.set(index, value);
                        } else {
                            System.out.println("Invalid index!");
                        }
                    }
                    break;

                case 3:
                    // Remove an Element
                    if (list.isEmpty()) {
                        System.out.println("The list is empty. Please add elements first.");
                    } else {
                        int removeIndex = sc.nextInt();
                        if (removeIndex >= 0 && removeIndex < list.size()) {
                            list.remove(removeIndex);
                            System.out.println("Element removed.");
                        } else {
                            System.out.println("Invalid index!");
                        }
                    }
                    break;

                case 4:
                    // Display elements
                    if (list.isEmpty()) {
                        System.out.println("The list is empty. Please add elements first.");
                    } else {
                        System.out.println("Current ArrayList: " + list);
                    }
                    break;

                case 5:
                    // Exit
                    System.out.println("Exiting...");
                    break;

                default:
                    System.out.println("Invalid choice! Please enter a valid option.");
                    break;
            }

        } while (choice != 5);

        sc.close();
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: