C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: We invoke add() with two arguments. The first argument is 0. This means we want to insert "bird" at the first index.
Java program that uses add, add with index
import java.util.ArrayList;
public class Program {
public static void main(String[] args) {
// Create an ArrayList and add two strings.
ArrayList<String> list = new ArrayList<>();
list.add("cat");
list.add("dog");
System.out.println(list);
// Add a String at index 0.
// ... This shifts the following elements to make room.
list.add(0, "bird");
System.out.println(list);
}
}
Output
[cat, dog]
[bird, cat, dog]
Animal: Here we use an Animal class with two fields (weight and color) and provide an Animal constructor.
ConstructorsAdd: We add three new Animals to the ArrayList. We create Animal instances pass them to add. Then we print the elements.
Java program that adds class instances to ArrayList
import java.util.ArrayList;
class Animal {
public int weight;
public String color;
public Animal(int weight, String color) {
// Create new Animal instance.
this.weight = weight;
this.color = color;
}
}
public class Program {
public static void main(String[] args) {
// Create an ArrayList of custom objects.
ArrayList<Animal> list = new ArrayList<>();
// Add new objects to the ArrayList.
list.add(new Animal(10, "white"));
list.add(new Animal(5, "orange"));
list.add(new Animal(8, "grey"));
// Display elements in our list.
for (Animal animal : list) {
System.out.println(animal.getClass().getName() + "/" +
animal.weight + "/" +
animal.color);
}
}
}
Output
Animal/10/white
Animal/5/orange
Animal/8/grey
Then: We create a second ArrayList with just one element ("Meow"). With System.out.println we display these ArrayLists.
ConsoleFinally: We invoke addAll to add the second ArrayList at the end of the first ArrayList.
Info: With addAll, we can add an ArrayList at the end of another ArrayList. The elements are copied.
Java program that uses ArrayList addAll
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);
}
}
Output
[Max, Mittens, Fluffy]
[Meow]
[Meow, Max, Mittens, Fluffy]
Result: The contents of the first ArrayList are placed in the second ArrayList. The previous elements are shifted.
Java program that inserts elements with addAll
import java.util.ArrayList;
public class Program {
public static void main(String[] args) {
// New ArrayList.
ArrayList<Integer> ints = new ArrayList<>();
ints.add(1);
ints.add(2);
System.out.println(ints);
// A second ArrayList.
ArrayList<Integer> ints2 = new ArrayList<>();
ints2.add(10);
ints2.add(20);
System.out.println(ints2);
// Insert first ArrayList into second ArrayList.
ints2.addAll(1, ints);
System.out.println(ints2);
}
}
Output
[1, 2]
[10, 20]
[10, 1, 2, 20]