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.
In this example, we use a String
array to initialize an ArrayList
. We create an empty ArrayList
of Strings.
Collections.addAll
method. This method receives two arguments.Collections.addAll
is the ArrayList
we want to add elements to.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]
ArrayList
Here we initialize an ArrayList
with the contents of another existing ArrayList
. We combine ArrayLists
. We use a copying constructor.
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]
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
, ForHere 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
.
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
An ArrayList
can store Integer elements, but not ints. It must store a class
. Integer is a class
that encapsulates an int
.
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.