Situatie
To add all the elements of a collection to another linked list, we use the addAll() method.
Solutie
Pasi de urmat
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
LinkedList<String> mammals = new LinkedList<>();
mammals.add(“Dog”);
mammals.add(“Cat”);
mammals.add(“Horse”);
System.out.println(“Mammals: ” + mammals);
LinkedList<String> animals = new LinkedList<>();
animals.add(“Crocodile”);
// Add all elements of mammals in animals
animals.addAll(mammals);
System.out.println(“Animals: ” + animals);
}
}
Output:
Mammals: [Dog, Cat, Horse]
Animals: [Crocodile, Dog, Cat, Horse]
Leave A Comment?