Create a class called CustomerListerArrayList with a main method that declares a
ID: 3606718 • Letter: C
Question
Create a class called CustomerListerArrayList with a main method that declares and instantiates an ArrayList of String objects called customerName. Add each of the following Strings to the ArrayList. • Beth • Jerry • Rick • Summer • Morty 2. Write for each loop (enhanced for loop) to display all the String objects on a separate new 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). 4. Write another for each loop (enhanced for loop) to display all the String objects on a separate new line. (After printing the contents of the array, print a blank new line for formatting) 5. Write a traditional for loop to remove “Jerry” and display the revised ArrayList. 6. 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?
Explanation / Answer
CustomerListerArrayList.java
import java.util.ArrayList;
public class CustomerListerArrayList {
public static void main(String args[]) {
ArrayList<String> customerName = new ArrayList<String>();
customerName.add("Beth");
customerName.add("Jerry");
customerName.add("Rick");
customerName.add("Summer");
customerName.add("Morty");
for(String s: customerName) {
System.out.println(s);
}
System.out.println();
customerName.add(4, "Jessica");
customerName.add(3, "Jerry");
for(String s: customerName) {
System.out.println(s);
}
System.out.println();
for(int i=0;i<customerName.size();i++) {
if(customerName.get(i).equals("Jerry")) {
customerName.remove(i);
i--;
}
}
for(int i=0;i<customerName.size();i++) {
System.out.println(customerName.get(i));
}
}
}
Output:
Beth
Jerry
Rick
Summer
Morty
Beth
Jerry
Rick
Jerry
Summer
Jessica
Morty
Beth
Rick
Summer
Jessica
Morty
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.