Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.3 kB
2
Indexable
Never
package JavaCollection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class NumberList {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the size of list");
        int n = scanner.nextInt();

        System.out.println("Enter the elements of list");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(scanner.nextInt());
        }
        System.out.println("List before updation:" + list);
        System.out.println("Insert : Enter the index and element");
        int index = scanner.nextInt();
        int insertElement = scanner.nextInt();
        if (index >= 0 && index <= list.size()) {
            list.add(index, insertElement);
        } else {
            System.out.println("Index is not present in list");
        }

        System.out.println("Delete : Enter the index");
        int deleteIndex = scanner.nextInt();
        if (index >= 0 && index <= list.size()) {
            list.remove(deleteIndex);
        } else {
            System.out.println("Element is not present in list");
        }

        System.out.println("List after updation:" + list);

    }
}
Leave a Comment