Private School Applic Flu CSC 201 Computer Science I ArrayList Lab Create a clas
ID: 3731068 • Letter: P
Question
Private School Applic Flu CSC 201 Computer Science I ArrayList Lab Create a class called CustomerListerArrayList with a ma in method that declares and instantiates an ArrayList of String objects called customerName. Add each of the following Strings to the ArrayList. 1 .Beth Jerry Rick . Summer Morty 2 Write for each loop to print all the String objects to the console. Each name MUST be on a separate line. (After printing the contents of the array, print a blank new line for formatting) 3 Add "Jessica" so that it is the 4th name in the ArrayList. Now add Jerry" into the third position in the list (there will be two identical strings"Jerry" in the list) Write another for each loop to print all the String objects to the console. Each name MUST be on a separate line. (After printing the contents of the array, print a blank new line for formatting) Write a traditional for loop to remove "Jerry" and display the revised ArrayList. In the comments text box in the Blackboard assignment link answer the following question: could we have removed "Jerry" using a for each loop? Why or why not? In the comments text box in the Blackboard assignment link answer the following questions was "Jerry" removed completely from the list? How many test cases does your program pass? Upload your file CustomerListerArrayList.java to the assignment link in Blackboard. 4 5 6 7Explanation / Answer
There are comments in the programs kindly adjust them according to your assignment, Create a file named: CustomerListerArrayList.java & Paste the below code.
----------------------------------------------------------CustomerListerArrayList.java----------------------------------------------
import java.util.ArrayList;
public class CustomerListerArrayList {
public static void main(String[] args) {
//1. Declaring & Instantiating a Arraylist of String named customerName
ArrayList<String> customerName=new ArrayList<String>();
//1. Adding elements to arraylist
customerName.add("Beth");
customerName.add("Jerry");
customerName.add("Rick");
customerName.add("Summer");
customerName.add("Morty");
//2. Printing each arraylist element in new line using foreach loop
for (String string : customerName) {
System.out.println(string);
}
System.out.println();//Blank line for formatting
//3. Adding Jessica & Jerry
customerName.add(3,"Jessica");
customerName.add(4,"Jerry");
//4. Printing each arraylist element in new line using foreach loop
//after adding Jessica & Duplicate Jerry
for (String string : customerName) {
System.out.println(string);
}
System.out.println();//Blank line for formatting
//5. Removing Jerry
for (int i = 0; i < customerName.size(); i++) {
customerName.remove("Jerry");
}
//5. Printing each arraylist element in new line using foreach loop
for (String string : customerName) {
System.out.println(string);
}
//5.
//No we couldn't remove Jerry using Foreach loop since it throws java.util.ConcurrentModificationException
//It is not recommended to use ArrayList.remove() when iterating over elements. This may lead to ConcurrentModificationException.
//When iterating over elements, Iterator.remove() method is recommended.
//6. Yes Jerry was completly removed since arraylist size was adjusted after removing Jerry.
}
}
---------------------------------------------------------End--------------------------------------------------------------
Output
Beth
Jerry
Rick
Summer
Morty
Beth
Jerry
Rick
Jessica
Jerry
Summer
Morty
Beth
Rick
Jessica
Summer
Morty
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.