ArrayList add and addAll (Insert Elements)
This page was last reviewed on Nov 25, 2022.
Dot Net Perls
Add. This method appends an element to the end of an ArrayList. Add must be passed an element of the ArrayList's type, like String or Integer.
With 2 arguments, we can insert an element with add(). There is no insert method. We must specify a valid insertion index as the first argument to add().
Add example. This program uses add() with one argument, and then with two arguments. We first append strings with values "cat" and "dog" to the end of the ArrayList.
Then We invoke add() with two arguments. The first argument is 0. This means we want to insert "bird" at the first 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); } }
[cat, dog] [bird, cat, dog]
Add class instances. Here is an example of adding class instances to an ArrayList. With ArrayList we can specify built-in classes like String or Integer, but custom classes too can be used.
Detail Here we use an Animal class with two fields (weight and color) and provide an Animal constructor.
Detail We add three new Animals to the ArrayList. We create Animal instances pass them to add. Then we print the elements.
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); } } }
Animal/10/white Animal/5/orange Animal/8/grey
AddAll example. Let us begin with an ArrayList of String elements. We use the names of pet cats. We add three Strings to the first ArrayList.
Then We create a second ArrayList with just one element ("Meow"). With System.out.println we display these ArrayLists.
Finally 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.
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]
AddAll, index. Here we insert one ArrayList's elements into another. We use addAll as an "insert all" or "insert range" method. We use an ArrayList of Integers.
ArrayList int
Result The contents of the first ArrayList are placed in the second ArrayList. The previous elements are shifted.
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); } }
[1, 2] [10, 20] [10, 1, 2, 20]
Collections.addAll. This method allows us to directly add an array to an ArrayList. If you are trying to add an array, use Collections.addAll instead of ArrayList addAll.
A summary. With add() we have both an "append" and an "insert" method on ArrayList. With an index argument, we can insert anywhere in an ArrayList. No other insert method is present.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
© 2007-2024 Sam Allen.