In this tutorial, you will learn how to initialize an ArrayList in Java. There are several different ways to do this. Let’s discuss them with examples.
1. Basic (Normal) Initialization
One of the ways to initialize an ArrayList
is to create it first and then add elements later using add()
method.
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
// Creating an empty ArrayList with String type
ArrayList<String> names = new ArrayList<>();
// Adding elements to the ArrayList
names.add("Chaitanya");
names.add("Rahul");
names.add("Aditya");
System.out.println(names);
}
}
2. Initialization with Initial Capacity
If you aware of the number of elements that you are going to add to ArrayList
, you can initialize it with an initial capacity. This avoid resizing overhead.
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
// Initial capacity of 10
ArrayList<String> names = new ArrayList<>(10);
// Adding elements to the ArrayList
names.add("Chaitanya");
names.add("Rahul");
names.add("Aditya");
System.out.println(names);
}
}
3. Initialization using asList() method
You can initialize an ArrayList
with elements using Arrays.asList()
. In this methods, all elements can be specified inside asList()
method as shown below. However you can add more elements later using add()
method.
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> names =
new ArrayList<>(Arrays.asList("Chaitanya", "Rahul", "Aditya"));
System.out.println(names);
}
}
4. Initialization using anonymous inner class (Double Brace Initialization)
This method may have some overhead.
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>() {{
add("Chaitanya");
add("Rahul");
add("Aditya");
}};
System.out.println(names);
}
}
5. Using Java 9+ List.of() and new ArrayList
In Java 9 and later, you can use List.of()
to create an immutable list and then pass it to the ArrayList
constructor if you need a mutable list.
import java.util.ArrayList;
import java.util.List;
public class ArrayListExample {
public static void main(String[] args) {
List<String> immutableList =
List.of("Chaitanya", "Rahul", "Aditya");
ArrayList<String> names = new ArrayList<>(immutableList);
System.out.println(names);
}
}
6. Initializing using Collections.ncopies()
The Collections.ncopies()
method is used when you need to initialize the ArrayList with multiple same elements. For example, if you want all 50 elements of an ArrayList as 10 then you can call the Collections.ncopies()
method like this: = new ArrayList<Integer>(Collections.nCopies(50, 10))
Syntax:
ArrayList<T> obj = new ArrayList<T>(Collections.nCopies(count, element));
count is number of elements and element is the item value
Example:
In the following example, we have initialized an array with 10 elements and all the elements are 5.
import java.util.*;
public class ArrayListExample {
public static void main(String args[]) {
ArrayList<Integer> intlist = new ArrayList<>(Collections.nCopies(10, 5));
System.out.println("ArrayList items: "+intlist);
}
}
Output:
ArrayList items: [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
Recommended articles:
Nysa says
Hi i used the anonymous inner class way to initialize an arrayList but i get the error below:
The type java.util.function.Consumer cannot be resolved. It is indirectly referenced from required .class files
Can you hint me as to what i have missed ?
Thanks
Nysa
irfan says
may be u are missing import statement
Shivam says
Hello,
I have a doubt :
//************************************
import java.util.*;
public class Initialization2 {
public static void main(String args[]) {
final ArrayList cities = new ArrayList() {
{
add(“Delhi”);
add(“Agra”);
add(“Chennai”);
}
};
System.out.println(“Content of Array list cities:” + cities);
cities.add(“Goa”);
System.out.println(“Content of Array list cities:” + cities);
}
}
//************************************
In this code I have declared the ArrayList as “final” so it should not allow any modifications, like any integer variable marked as final cannot be changed once it is assigned.
But it allows the modification as I have added one more object “Goa” to cities ArrayList.
Please explain.
Thanks
vishwjeet says
@Shivam in the above example…u r making reference variable as final so,if u want to again assign reference variable cities= new ArrayList();// you will get warning
Shashibhushan says
In the example 1 to initialize , you missed showing the relationship between the arrays.aslist and arraylist object…
arr.addAll(Arrays.asList(“bangalore”,”hyderabad”,”chennai”));
Aditya says
In this method,
ArrayList cities = new ArrayList(){{
add(“Delhi”);
add(“Agra”);
add(“Chennai”);
}};
Why is there 2 openings after object creation I didn’t get that and what is the difference between ArrayList and Array.asList.
In the above example what I have given why not use “cities.add(“”) ” and whats the difference between add and obj.add ?