In: Computer Science
Problem 1
Problem 2
Chris, Robert, Scarlett, Clark, Jeremy, Gwyneth, Mark
Problem 3
Current size of avengers: xxx
Problem 4
Now the size is: xxx
Problem 5
Problem 6
Java code:
import java.util.ArrayList; public class ListORama { public static void makeLists() { // Problem 1 ArrayList<String> avengers = new ArrayList<String>(); // Problem 2 avengers.add("Chris"); avengers.add("Robert"); avengers.add("Scarlett"); avengers.add("Clark"); avengers.add("Jeremy"); avengers.add("Gwyneth"); avengers.add("Mark"); System.out.println(avengers); for (String avenger : avengers) System.out.println(avenger); // Problem 3 System.out.println(); System.out.println("Current size of avengers: " + avengers.size()); for (int i = 0; i < avengers.size(); i++) System.out.println(avengers.get(i)); // Problem 4 avengers.remove("Clark"); avengers.remove("Gwyneth"); System.out.println("Now the size is: " + avengers.size()); System.out.println(avengers); // Problem 5 avengers.add("Chris H."); avengers.set(0, "Chris E."); avengers.add(2, "Loki"); System.out.println("Now the size is: " + avengers.size()); System.out.println(avengers); // Problem 6 for (int i = 0; i < avengers.size(); i++) { if (i == avengers.size() - 1) System.out.println(avengers.get(i)); else System.out.print(avengers.get(i) + ", "); } } }
Output:
Kindly rate the answer and for any help just drop a comment