Let us begin with an ArrayList of String elements. We use the names of pet cats. We add three Strings to the first ArrayList.
import java.util.ArrayList;
public class Program {
public static void main(String[] args) {
// Create ArrayList and add 3 strings to it.
ArrayList<String> cats1 = new ArrayList<>();
cats1.add(
"Max");
cats1.add(
"Mittens");
cats1.add(
"Fluffy");
System.out.println(cats1);
// Create a second ArrayList.
ArrayList<String> cats2 = new ArrayList<>();
cats2.add(
"Meow");
System.out.println(cats2);
// Add first ArrayList to second.
cats2.addAll(cats1);
System.out.println(cats2);
}
}
[Max, Mittens, Fluffy]
[Meow]
[Meow, Max, Mittens, Fluffy]