Consider the following cods segment ArrayList cats - new ArrayList{}; cata.add (
ID: 3816516 • Letter: C
Question
Consider the following cods segment ArrayList cats - new ArrayList{}; cata.add ("Lions"); Cats.add ("Tigers"); ArrayList swimmers = new ArrayKust {}; swimpers.add ("Whales"); swimmers.add ("Dolphins"); ArrayList primates = new ArrayList(); primates.add("Gorillas"); primates.add("Chimpanzees"): ArrayList mammals = new ArrayList (); mammals add (cats); mammals.add(swimmers); mammaIs.add(primates); for (ArrayList mammal: mammals) {for (String animal: mammal) System.out.println(animal); System.out.println{};/* missing code */Which of the following implementation* of f* missing code */will display every element of the mamma 1 s array? Implementation I for (ArrayList mammal; mammals {for (String animal: mammal) system.out. printin (animal); System. out. println(); Implementation III System. out.println. (cats); System. out. printIn (swimmwrs); System.out.print lr. (primates); (A) I only (B) II only (C) III only (D) I and II only (E) I, II and IIIExplanation / Answer
Which of the following implementation of /* missing code */ will display every element of the mammals array?
Answer: (E) I, II, and III
Explanation:
Implementation I:
for(ArrayList<String> mammal : mammals)
{
for(String animal: mammal)
System.out.println(animal);
System.out.println();
}
In the above code the first loop, loops through the ArrayList mammals, as each element in mammals is again an ArrayList, the second loop, loops through each element(ArrayList) and prints the mammal.
The above code prints
Lions
Tigers
Whales
Dolphins
Gorillas
Chimpanzees
Implementation II:
System.out.println(mammals);
The above code prints the ArrayList mammals as a string. As each element in mammals is in turn an ArrayList, it prints all the lists as strings. The output of the above code will be (each elements in the square brackets is an ArrayList)
[[Lions, Tigers], [Whales, Dolphins], [Gorillas, Chimpanzees]]
Implementation III:
System.out.println(cats);
System.out.println(swimmers);
System.out.println(primates);
As the ArrayList mammals has the ArrayLists cats, swimmers and primates as its elements, printing each ArrayList will print all the mammals. The output of the above code will be
[Lions, Tigers]
[Whales, Dolphins]
[Gorillas, Chimpanzees]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.