It would be simpler if you were to just declare it as a fixed-size List (but with mutable elements) - does it have to be an ArrayList?

List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");

Or if you have only one element:

List<String> places2 = Collections.singletonList("Buenos Aires");

This would mean that places2 is immutable (trying to change it in any way will cause an UnsupportedOperationException exception to be thrown).

To make a variable-size list that is a concrete ArrayList you can create an ArrayList from the fixed-size list:

ArrayList<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));

And import the correct package:

import java.util.Arrays;
Answer from Tom on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › initialize-an-arraylist-in-java
Initialize an ArrayList in Java - GeeksforGeeks
July 11, 2025 - Example 1: This example demonstrates how to create an ArrayList using the add() method. ... // Java code to illustrate initialization // of ArrayList using add() method import java.util.*; public class Geeks { public static void main(String ...
Top answer
1 of 16
2789

It would be simpler if you were to just declare it as a fixed-size List (but with mutable elements) - does it have to be an ArrayList?

List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");

Or if you have only one element:

List<String> places2 = Collections.singletonList("Buenos Aires");

This would mean that places2 is immutable (trying to change it in any way will cause an UnsupportedOperationException exception to be thrown).

To make a variable-size list that is a concrete ArrayList you can create an ArrayList from the fixed-size list:

ArrayList<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));

And import the correct package:

import java.util.Arrays;
2 of 16
2240

Actually, probably the "best" way to initialize the ArrayList is the method you wrote, as it does not need to create a new List in any way:

ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");

The catch is that there is quite a bit of typing required to refer to that list instance.

There are alternatives, such as making an anonymous inner class with an instance initializer (also known as an "double brace initialization"):

ArrayList<String> list = new ArrayList<String>() {{
    add("A");
    add("B");
    add("C");
}};

However, I'm not too fond of that method because what you end up with is a subclass of ArrayList which has an instance initializer, and that class is created just to create one object -- that just seems like a little bit overkill to me.

What would have been nice was if the Collection Literals proposal for Project Coin was accepted (it was slated to be introduced in Java 7, but it's not likely to be part of Java 8 either.):

List<String> list = ["A", "B", "C"];

Unfortunately it won't help you here, as it will initialize an immutable List rather than an ArrayList, and furthermore, it's not available yet, if it ever will be.

Discussions

java - ArrayList initialization equivalent to array initialization - Stack Overflow
What this is actually doing is ... from ArrayList (the outer set of braces do this) and then declare a static initialiser (the inner set of braces). This is actually an inner class of the containing class, and so it'll have an implicit this pointer. Not a problem unless you want to serialise it, or you're expecting the outer class to be garbage collected. I understand that Java 7 will provide ... More on stackoverflow.com
🌐 stackoverflow.com
Initializing an empty ArrayList (Java)
Assuming both are not static variables the only difference should be the order of initialization. When you have something declared out of the constructor it is initialized first. It will run the constructor after all of the fields at the top have been initialized. Here is a stack overflow post about this topic which goes into more detail: https://stackoverflow.com/questions/4916735/default-constructor-vs-inline-field-initialization More on reddit.com
🌐 r/learnprogramming
3
9
March 31, 2018
Best Practice for Initializing ArrayLists with values?
This is one thing I have come to dislike about Java. Both idioms are excessively verbose compared to other languages. list = ['a', 'b', 'c'] Python var list = ['a', 'b', 'c']; Javascript List thirdList = Arrays.asList('a','b','c'); This gives you a random-access list that can be mutated (set()), but not expanded or shrunk. This is the most efficient. A single array is created (via varargs) and is used to back the resulting list. Your firstList example is the workaround for a list with full mutability, but this creates a second (Arrays$ArrayList.toArray()) and third array (new ArrayList()) and copies the contents. The copy is extremely fast though, because it uses System.arraycopy(), which is implemented in C (no bounds check to read/write each array element). Your secondList example creates just one array (new ArrayList()), but has to iterate (and bounds check the array) to add each element. Actually, the JIT may be able to remove the bounds check in this case. The best solution IMO is to write a utility method, or use 3rd party library like Guava: import static com.google.common.collect.Lists.newList; import static com.google.common.collect.Lists.newArrayList; List immutableList = newList('a', 'b', 'c'); List mutableList = newArrayList('a', 'b', 'c'); This is syntactic sugar around your secondList example, but performance will not be an issue in the vast majority of cases. If I were reading your code and saw anything more complex, I would take time to wonder "Why? Why is this optimization needed? What am I missing?". Developer time is the most valuable resource in most cases. Bonus: I often use this one-liner initialize an immutable static field: import static com.google.common.collect.Lists.newList; public static final List LIST = newList('a', 'b', 'c'); More on reddit.com
🌐 r/javahelp
10
1
August 22, 2016
How do I declare an ArrayList in a default constructor?
Take a look at https://www.w3schools.com/java/java_constructors.asp for some examples on how to initialise fields inside a constructor. Think about how you could copy what is being done in that link and change it to initialise size and books the way you need to. More on reddit.com
🌐 r/learnprogramming
4
1
November 2, 2021
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-initialize-an-arraylist
How to Initialize an ArrayList in Java
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); } } If you aware of the number of elements that you are going to add to ArrayList, you can initialize it with an initial capacity.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-initialize-an-arraylist-in-java-with-values
How to Initialize an ArrayList in Java – Declaration with Values
April 21, 2023 - Alternatively, you can create an ArrayList with values/elements at the point of declaration by using the add method in an initializer block: import java.util.ArrayList; public class ArrayListTut { public static void main(String[] args) { ...
🌐
Career Karma
careerkarma.com › blog › java › how to initialize arraylist in java
How to Initialize ArrayList in Java | Career Karma
December 1, 2023 - Type is the type of data our array list will store. arrayName is the name of the array list we are creating. new ArrayList<>() tells our program to create an instance of ArrayList and assign it to the arrayName variable.
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › initialize an arraylist in java
Initialize an ArrayList in Java
August 4, 2023 - Stream<String> stream = Stream.of("alex", "brian", "charles"); ArrayList<String> stringList = stream //perform stream operations .collect(Collectors.toCollection(ArrayList::new)); That’s all about initializing an ArrayList in Java.
🌐
iO Flood
ioflood.com › blog › java-initialize-arraylist
Initialize an ArrayList in Java: Easy and Advanced Methods
February 27, 2024 - In Java, the simplest way to initialize an ArrayList involves using the ‘new’ keyword and the ‘ArrayList’ constructor. This method is perfect for beginners and is often used in a wide range of Java programs.
Find elsewhere
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java arraylist: how to declare, create, initialize arraylist
Java ArrayList: How to Declare, Create, Initialize ArrayList
March 5, 2026 - In this case, we usually call it as ‘ArrayList of objects’. So if you want to store integer type of elements, then you have to use the Integer object of the wrapper class and not primitive type int. You can declare, create, initialize, and print an ArrayList in Java using the following examples.
🌐
TutorialsPoint
tutorialspoint.com › initialize-an-arraylist-in-java
Initialize an ArrayList in Java
March 18, 2025 - Arrays.asList() is used to initialize the ArrayList with predefined elements. The ArrayList is then sorted using Collections.sort(). ArrayList<integer> myList = new ArrayList<integer>(Arrays.asList(50,29,35,11,78,64,89,67));</integer></integer> ...
🌐
Quora
quora.com › In-Java-whats-the-best-way-to-create-and-initialize-an-ArrayList-Whats-the-best-way-to-do-it-in-one-line
In Java, what's the best way to create and initialize an ArrayList? What's the best way to do it in one line? - Quora
Answer (1 of 10): The obvious methods have been listed by other members, like [code ]Arrays.asList(oneString, twoString, threeString).[/code] I looked at the mature Java library called “Guava” and saw this method: newArrayList(E... elements); That seems to be the most elegant approach, ...
🌐
Blogger
javahungry.blogspot.com › 2017 › 11 › how-to-initialize-arraylist-in-java-example.html
2 ways on How to Initialize an Arraylist in java with Example | Java Hungry
Elements are:[Boston, Chicago, Dallas] Method 2: Normal Initialization method Syntax : ArrayList<T> obj = new ArrayList<T>(); obj.add("Object o1"); obj.add("Object o2"); obj.add("Object o3"); ... ... Example: import java.util.*; public class Initialization2 { public static void main(String ...
🌐
HelloKoding
hellokoding.com › create-and-initialize-an-arraylist-in-java
Initialize an ArrayList in Java
March 28, 2020 - You can use add or addAll method to initialize an ArrayList after the creation time · @Test public void initWithAddAndAddAll() { List<Integer> arrayList1 = new ArrayList<>(Set.of(1, 2, 3)); List<Integer> arrayList2 = new ArrayList<>(); ...
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
How to initialize an ArrayList in Java - Java Code Geeks
July 5, 2022 - Note: To execute this program, use Java 9 or above versions. In this way, we use some collection to initialize an ArrayList. The following coding example illustrates the initialization of an ArrayList using a collection.
🌐
javaspring
javaspring.net › blog › initiliaze-a-arraylist-in-java
Initializing an ArrayList in Java: A Comprehensive Guide — javaspring.net
The simplest way to initialize an ArrayList is by using the default constructor. This creates an empty ArrayList with an initial capacity of 10. import java.util.ArrayList; import java.util.List; public class DefaultConstructorExample { public ...
🌐
Baeldung
baeldung.com › home › java › java list › java list initialization in one line
Java List Initialization in One Line | Baeldung
April 4, 2025 - Here’s a simple example: int intNum = 42; long longNum = intNum; assertEquals(42L, longNum); So, we may want to initialize a List<Long> in this way: List<Long> listOfLong = new ArrayList<Long>(Arrays.asList(1, 2, 3)); In the example, 1, 2, ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-of-arraylist-in-java
ArrayList of ArrayList in Java - GeeksforGeeks
July 11, 2025 - // Java code to demonstrate the ... int n = 3; // Here aList is an ArrayList of ArrayLists ArrayList<ArrayList<Integer> > aList = new ArrayList<ArrayList<Integer> >(n); // Create n lists one by one and append to the // master ...
🌐
Verve AI
vervecopilot.com › interview-questions › can-java-how-to-initialize-arraylist-be-the-secret-weapon-for-acing-your-next-interview
Can `Java How To Initialize Arraylist` Be The Secret Weapon For Acing Your… · For Acing Your Next Interview · Interview Q&A | Verve AI
When you `java how to initialize arraylist` without specifying an initial capacity (e.g., `new ArrayList<>()`), Java allocates a default capacity (often 10) [^3]. As you add more elements than the current capacity, the `ArrayList` has to resize ...
🌐
SourceBae
sourcebae.com › home › how to initialization of an arraylist in one line?
How to Initialization of an ArrayList in one line? - SourceBae
August 21, 2025 - What it is: Double brace initialization is a way of initializing an ArrayList using an anonymous inner class and an instance initializer block. Example code snippet: ArrayList<String> names = new ArrayList<String>() {{ add("Alice"); add("Bob"); ...
🌐
BootcampToProd
bootcamptoprod.com › java-list-initialization
Java List Initialization: Easy Methods with Examples - BootcampToProd
December 22, 2024 - Handling Null Values: Double-brace initialization allows you to add null values to the list, just like you would with the ArrayList constructor or other list initialization methods. However, since null is allowed, be cautious when accessing or modifying these values to avoid NullPointerException. ... Here’s an example of how to use double-brace initialization to initialize and populate a list, including adding null values: import java.util.ArrayList; import java.util.List; public class DoubleBraceExample { public static void main(String[] args) { // Initialize and populate the list using double-brace initialization List<String> fruits = new ArrayList<>() {{ add("Apple"); add("Banana"); add(null); // Adding null to the list add("Cherry"); }}; // Print the list System.out.println("Fruits: " + fruits); } } DoubleBraceExample.java