Knowee
Questions
Features
Study Tools

Write a JAVA program to create an Array List for objects of type String, and then several strings are added to it. Some of the elements are removed and the list is displayed again.Input-Output form:1.Addition2.Deletion3.Display4.Count number of elementsInput Choice: 1Enter element: AInput Choice: 1Enter element: BInput Choice: 1Enter element: CInput Choice: 2Enter element: CInput Choice: 3OutputàABInput Choice: 4Outputà2

Question

Write a JAVA program to create an Array List for objects of type String, and then several strings are added to it. Some of the elements are removed and the list is displayed again.Input-Output form:1.Addition2.Deletion3.Display4.Count number of elementsInput Choice: 1Enter element: AInput Choice: 1Enter element: BInput Choice: 1Enter element: CInput Choice: 2Enter element: CInput Choice: 3OutputàABInput Choice: 4Outputà2

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Here is a simple Java program that implements the functionality you described:

import java.util.ArrayList;
import java.util.Scanner;

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

        do {
            System.out.println("1.Addition\n2.Deletion\n3.Display\n4.Count number of elements\nInput Choice: ");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.println("Enter element: ");
                    element = scanner.next();
                    list.add(element);
                    break;
                case 2:
                    System.out.println("Enter element: ");
                    element = scanner.next();
                    list.remove(element);
                    break;
                case 3:
                    System.out.println("Output: " + list);
                    break;
                case 4:
                    System.out.println("Output: " + list.size());
                    break;
                default:
                    System.out.println("Invalid choice. Please enter a valid option.");
            }
        } while (choice != 0);
    }
}

This program creates an ArrayList of Strings and uses a Scanner to get user input. It then uses a switch statement to perform different actions based on the user's choice. The user can add elements to the list, remove elements from the list, display the list, or count the number of elements in the list. The program will continue to prompt the user for input until they enter 0.

This problem has been solved

Solution 2

Here is a simple Java program that implements the functionality you described:

import java.util.ArrayList;
import java.util.Scanner;

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

        do {
            System.out.println("1.Addition\n2.Deletion\n3.Display\n4.Count number of elements\nInput Choice: ");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.println("Enter element: ");
                    element = scanner.next();
                    list.add(element);
                    break;
                case 2:
                    System.out.println("Enter element: ");
                    element = scanner.next();
                    list.remove(element);
                    break;
                case 3:
                    System.out.println("Output: " + list);
                    break;
                case 4:
                    System.out.println("Output: " + list.size());
                    break;
                default:
                    System.out.println("Invalid choice");
            }
        } while (choice >= 1 && choice <= 4);
    }
}

This program creates an ArrayList of Strings and uses a Scanner to get user input. It then uses a switch statement to perform different actions based on the user's choice. The user can add elements to the list, remove elements from the list, display the list, or count the number of elements in the list. The program will continue to prompt the user for input until they enter a number outside of the range 1-4.

This problem has been solved

Solution 3

Here is a simple Java program that implements the functionality you described:

import java.util.ArrayList;
import java.util.Scanner;

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

        do {
            System.out.println("1.Addition\n2.Deletion\n3.Display\n4.Count number of elements\n");
            System.out.print("Input Choice: ");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.print("Enter element: ");
                    element = scanner.next();
                    list.add(element);
                    break;
                case 2:
                    System.out.print("Enter element: ");
                    element = scanner.next();
                    list.remove(element);
                    break;
                case 3:
                    System.out.println("Output: " + list);
                    break;
                case 4:
                    System.out.println("Output: " + list.size());
                    break;
                default:
                    System.out.println("Invalid choice");
            }
        } while (choice != 0);
    }
}

This program creates an ArrayList of Strings and uses a Scanner to get user input. It then uses a switch statement to perform different actions based on the user's choice. The user can add elements to the list, remove elements from the list, display the list, or display the number of elements in the list. The program will continue to prompt the user for input until they enter 0.

This problem has been solved

Solution 4

Here is a simple Java program that implements the functionality you described:

import java.util.ArrayList;
import java.util.Scanner;

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

        do {
            System.out.println("1.Addition\n2.Deletion\n3.Display\n4.Count number of elements\nInput Choice: ");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.println("Enter element: ");
                    element = scanner.next();
                    list.add(element);
                    break;
                case 2:
                    System.out.println("Enter element: ");
                    element = scanner.next();
                    list.remove(element);
                    break;
                case 3:
                    System.out.println("Output: " + list);
                    break;
                case 4:
                    System.out.println("Output: " + list.size());
                    break;
                default:
                    System.out.println("Invalid choice");
            }
        } while (choice != 0);
    }
}

This program creates an ArrayList of Strings and uses a Scanner to get user input. The user can choose to add an element to the list, remove an element from the list, display the list, or count the number of elements in the list. The program will continue to prompt the user for input until they enter 0.

This problem has been solved

Solution 5

Here is a simple Java program that implements the functionality you described:

import java.util.ArrayList;
import java.util.Scanner;

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

        do {
            System.out.println("1.Addition\n2.Deletion\n3.Display\n4.Count number of elements\n");
            System.out.print("Input Choice: ");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.print("Enter element: ");
                    element = scanner.next();
                    list.add(element);
                    break;
                case 2:
                    System.out.print("Enter element: ");
                    element = scanner.next();
                    list.remove(element);
                    break;
                case 3:
                    System.out.println("Output: " + list);
                    break;
                case 4:
                    System.out.println("Output: " + list.size());
                    break;
                default:
                    System.out.println("Invalid choice");
            }
        } while (choice >= 1 && choice <= 4);
    }
}

This program creates an ArrayList of Strings and uses a Scanner to get user input. It then uses a switch statement to perform different actions based on the user's choice. The user can add elements to the list, remove elements from the list, display the list, or count the number of elements in the list. The program will continue to prompt the user for input until they enter a choice outside of the range 1-4.

This problem has been solved

Solution 6

Here is a simple Java program that follows your instructions:

import java.util.ArrayList;
import java.util.Scanner;

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

        do {
            System.out.println("1.Addition\n2.Deletion\n3.Display\n4.Count number of elements\nInput Choice: ");
            choice = scanner.nextInt();
            scanner.nextLine(); // consume newline

            switch (choice) {
                case 1:
                    System.out.println("Enter element: ");
                    element = scanner.nextLine();
                    list.add(element);
                    break;
                case 2:
                    System.out.println("Enter element: ");
                    element = scanner.nextLine();
                    list.remove(element);
                    break;
                case 3:
                    System.out.println("Output: " + list);
                    break;
                case 4:
                    System.out.println("Output: " + list.size());
                    break;
                default:
                    System.out.println("Invalid choice. Please enter a number between 1 and 4.");
            }
        } while (choice >= 1 && choice <= 4);
    }
}

This program creates an ArrayList of Strings and uses a Scanner to get user input. It then uses a switch statement to perform different actions based on the user's choice. The user can add elements to the list, remove elements from the list, display the list, or count the number of elements in the list. The program will continue to prompt the user for input until they enter a number outside the range 1-4.

This problem has been solved

Similar Questions

Write a JAVA program to create an Array List for objects of type String, and then several strings are added to it. The list is then displayed. Some of the elements are removed and the list is displayed again.Input form:Initial size of an array:Size of an array after additions:Contents of an array:Size of an array after deletions:Contents of an array:Output form:Initial size of an array: Size of an array after additions: Contents of an array: Size of an array after deletions: Contents of an array:

import java.util.ArrayList;class Main { public static void main(String[] args){ ArrayList<String> animals = new ArrayList<>(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); animals.remove(2); System.out.println("ArrayList: " + animals); }}Select one:a.ArrayList: [Dog, Cat, Horse]b.ArrayList: [Horse]c.ArrayList: [Dog]d.ArrayList: [Dog, Cat]

What is the output of the following program:import java.util.ArrayList;class Main { public static void main(String[] args){ ArrayList<String> animals = new ArrayList<>(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); animals.remove(2); System.out.println("ArrayList: " + animals); }}Select one:a.ArrayList: [Dog, Cat, Horse]b.ArrayList: [Dog, Cat]c.ArrayList: [Dog]d.ArrayList: [Horse]

Write a Java program to remove the duplicate elements of a given array and return the new length of the array

public class Test {   public static void main(String[] args) {     for (int i = 0; i < args.length; i++) {       System.out.print(args[i] + " ");     }   } } What is the output, if you run the program using java Test 1 2 3?Group of answer choices311 2 31 2 Flag question: Question 15Question 151 ptsWhich correctly creates an array of five empty Strings? Group of answer choicesString[] a = new String [5];String[] a = {"", "", "", "", ""};String[5] a;String[ ] a = new String [5]; for (int i = 0; i < 5; a[i++] = null);

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.