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
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.

🌐
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 ...
Discussions

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
How to pass an array of unknown size to a class?
You want to make it an instance variable. More on reddit.com
🌐 r/javahelp
5
1
June 14, 2020
Any way to initialize an ArrayList with random data without using a for loop?
This seems to be what you're looking for val arr = Array(size) { Math.random().toFloat * 100f } More on reddit.com
🌐 r/Kotlin
7
5
March 28, 2020
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
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 ...
🌐
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 - Java ArrayList can be initialized in one line using List.of(), and new ArrayList constructors. Learn to add elements one by one and in bulk.
🌐
iO Flood
ioflood.com › blog › java-initialize-arraylist
Initialize an ArrayList in Java: Easy and Advanced Methods
February 27, 2024 - The simplest way to initialize an ArrayList is with the syntax: ArrayList<String> list = new ArrayList<String>(); which creates an empty ArrayList named ‘list’ that can hold String objects.
🌐
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 - You'll see the different in-built methods that can be used to add, access, modify, and remove elements in an ArrayList. Let's get started! The terms "declaration" and "initialization" are commonly associated with data structures. Declaration has to do with creating a data structure, while initialization involves assigning values to the data structure. ... import java.util.ArrayList; public class ArrayListTut { public static void main(String[] args) { ArrayList<String> people = new ArrayList<>(); } }
🌐
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));
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 - This method is used to initialize the ArrayList with the same values. We provide the count of elements to be initialized and the initial value to the method. ... The below example demonstrates Array initialization using Collections.nCopies method. import java.util.*; public class Main { public static void main(String args[]) { //create ArrayList with 10 elements //initialized to value 10 using Collections.nCopies ArrayList&amp;lt;Integer&amp;gt; intList = new ArrayList&amp;lt;Integer&amp;gt;(Collections.nCopies(10,10)); //print the ArrayList System.out.println(&amp;quot;Content of ArrayList:&amp;quot;+intList); } }
🌐
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.
🌐
CodeJava
codejava.net › java-core › collections › initialize-list-with-values
Java Initialize ArrayList with Values in One Line
July 18, 2024 - public class User { String name; ... an ArrayList object that wraps the list returned by Arrays.asList() method. Since Java 9, you can use the List.of() method to initialize a List with some initial values in a single line....
🌐
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 - There are several methods to initialize an ArrayList in Java in one line. These methods include using the Arrays.asList() method, double brace initialization, Stream API, and the Collections.addAll() method.
🌐
Squash
squash.io › how-to-initialize-an-arraylist-in-one-line-in-java
How to Initialize an ArrayList in One Line in Java - Squash Labs
November 3, 2023 - If you are using Java 9 or later, you can also use the List.of() method to initialize an ArrayList in one line. This method is a convenient way to create an immutable List with a fixed number of elements.
🌐
HelloKoding
hellokoding.com › create-and-initialize-an-arraylist-in-java
Initialize an ArrayList in Java
March 28, 2020 - Provide an existing Collection ... assertThat(arrayList2).contains(3, 1, 2, null); } You can use add or addAll method to initialize an ArrayList after the creation time...
🌐
TutorialsPoint
tutorialspoint.com › article › initialize-an-arraylist-in-java
Initialize an ArrayList in Java
March 18, 2025 - One of the most frequent methods of initializing an ArrayList is to use its default constructor and manually add the elements using the add() method.
🌐
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
import java.util.*; public class Initialization2 { public static void main(String args[]) { ArrayList<String> cars = new ArrayList<String>(); cars.add("Honda"); cars.add("Hyundai"); cars.add("Toyota"); System.out.println("Cars stored in array list are: "+ cars); } } Output
🌐
Just Academy
justacademy.co › blog-detail › how-to-initialize-arraylist-in-java
How To Initialize ArrayList In Java by Roshan Chaturvedi | JustAcademy
How To Initialize ArrayList In JavaIn Java, an ArrayList is a dynamic array that can grow and shrink in size at runtime. To initialize an ArrayList in Java, you can use the ArrayList class constructor and add elements to it using the add() method.
🌐
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, ...
🌐
Medium
medium.com › javarevisited › how-to-initialize-arraylist-in-java-8bb3434e3eb8
How to Initialize ArrayList in Java | by Suraj Mishra | Javarevisited | Medium
December 15, 2022 - We can also initialize ArrayList by using anonymous class. ... A humble place to learn Java and Programming better.
🌐
JanBask Training
janbasktraining.com › community › java › initialization-of-an-arraylist-in-one-line
Initialization of an ArrayList in one line | JanBask Training Community
November 3, 2025 - The most common are new ArrayList<>(Arrays.asList("A","B","C")) for a modifiable list, new ArrayList<>(List.of("A","B","C")) in Java 9+ for concise syntax, or Collections.addAll(new ArrayList<>(), "A","B","C") for clean bulk addition.
🌐
Level Up Lunch
leveluplunch.com › java › examples › initialize-list
Initialize list | Level Up Lunch
November 16, 2013 - @Test public void initialize_list_java_with_arrays () { List<String> cheeses = Arrays.asList("Munster", "Swiss", "Sharp cheddar"); assertEquals(3, cheeses.size()); } Guava Lists.newArrayList will initialize and fill the ArrayList with the provided elements.