Home
Map
ArrayList InitializeInitialize ArrayLists with String arrays and for-loops. Use Collections.addAll.
Java
This page was last reviewed on Nov 25, 2022.
Initialize ArrayList. With ArrayLists we have an expandable, fast collection. But often we must initialize them with values. The syntax for ArrayList initialization is confusing.
With special methods, like Collections.addAll, we can add many elements at once. And ArrayList provides a constructor that copies another ArrayList collection. This is useful.
Initialize with array. In this example, we use a String array to initialize an ArrayList. We create an empty ArrayList of Strings.
Then We invoke the Collections.addAll method. This method receives two arguments.
Collections.addAll
Argument 1 The first argument to Collections.addAll is the ArrayList we want to add elements to.
Argument 2 This is the String array reference. These elements are added to the end of the ArrayList.
import java.util.ArrayList; import java.util.Collections; public class Program { public static void main(String[] args) { // Initialize ArrayList with contents of string array. String[] array = {"bird", "fish"}; ArrayList<String> animals = new ArrayList<>(); Collections.addAll(animals, array); // The ArrayList now contains all strings. System.out.println(animals); } }
[bird, fish]
Another ArrayList. Here we initialize an ArrayList with the contents of another existing ArrayList. We combine ArrayLists. We use a copying constructor.
Detail We use the ArrayList's copy constructor to add all the elements from one ArrayList to the new one.
import java.util.ArrayList; public class Program { public static void main(String[] args) { // Create an ArrayList with two elements. ArrayList<Integer> list1 = new ArrayList<>(); list1.add(10); list1.add(20); // Initialize an ArrayList from another ArrayList of the same type. ArrayList<Integer> list2 = new ArrayList<>(list1); list2.add(30); // The collections are separate. System.out.println(list1); System.out.println(list2); } }
[10, 20] [10, 20, 30]
One statement. Sometimes it is convenient to add many values in one statement. We addAll to add 4 strings to an ArrayList. We initialize the ArrayList.
import java.util.ArrayList; import java.util.Collections; public class Program { public static void main(String[] args) { // Initialize many values in one statement. ArrayList<String> colors = new ArrayList<>(); Collections.addAll(colors, "blue", "red", "green", "yellow"); // Print colors. System.out.println(colors); } }
[blue, red, green, yellow]
Int, For. Here we use a for-loop to initialize an Integer ArrayList. We add ints to the ArrayList, and these are cast to Integers to be stored in the ArrayList.
Cast
Detail We use a capacity on our ArrayList. This reduces resizes when many elements are added—space is reserved.
import java.util.ArrayList; public class Program { public static void main(String[] args) { // Initialize with capacity. ArrayList<Integer> values = new ArrayList<>(10); // Initialize an ArrayList of Integers in a loop. for (int i = 0; i < 10; i++) { values.add(i); } // Print ArrayList and its size. System.out.println(values); System.out.println(values.size()); } }
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 10
Ints, Integers. An ArrayList can store Integer elements, but not ints. It must store a class. Integer is a class that encapsulates an int.
ArrayList int
A summary. The first step to using an ArrayList is initializing it. We often must add initial data. We do this with Collections.addAll, and a copy constructor.
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.
Home
Changes
© 2007-2024 Sam Allen.