Java program to add all elements from other collection to LinkedList

Configurare noua (How To)

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]

Tip solutie

Permanent

Voteaza

(8 din 18 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?