Exploring Arraylists. Remember that arraylists are good for adding and removing things, and that they are clock-cycle friendlier in this case than resizing arrays as we discussed in lecture. This lab allows you to get some hands-on using an arraylist so you will remember it for those times it would be a good fit for an application. a) Create an Arraylist of type String and prompt the user for three names and add these names to your ArrayList. b) Print a message with the number of elements in the ArrayList for the user using the size() method (to find the number to print.) c) Prompt the user for two more names and add them to the ArrayList and once again print a message with the number of elements in the ArrayList for the user. d) Use a loop to print all of the names in the ArrayList for the user. e. Ask the user for a name to remove, remove the value the user provides, and then use a loop to print all of the names in the ArrayList for the user. (25 points)

Respuesta :

Answer:

Code is given below:

Explanation:

import java.io.*;

import java.util.*;

import java.util.Scanner;

class Solution {

  public static void main(String args[]) {

      Scanner in = new Scanner(System.in);

     

     

      ArrayList<String> ans = new ArrayList();

     

      System.out.println("(a) ADD THREE NAMES: ");

     

      for(int i=0;i<3;i++)

      {

          ans.add(in.next());

         

      }

      System.out.println("(b) NUMBER OF ELEMENTS: " + ans.size());

     

      System.out.println("(c) ADD TWO MORE NAMES : ");

      for(int i=0;i<2;i++)

      {

          ans.add(in.next());

         

         

      }

      System.out.println("(d) PRINT ALL NAMES: ");

      for(int i=0;i<ans.size();i++)

      {

          System.out.println(ans.get(i));

      }

     

      System.out.println("(e) ENTER A NAME TO REMOVE: ");

      ans.remove(in.next());

      for(int i=0;i<ans.size();i++)

      {

          System.out.println(ans.get(i));

      }

     

     

  }

}