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
2787

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 - // Java code to illustrate initialization // of ArrayList using add() method import java.util.*; public class Geeks { public static void main(String args[]) { // create a ArrayList String type ArrayList<String> al = new ArrayList<String>(); ...
Discussions

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 you define default values of an ArrayList during a struct's initialization?
Using try makes the function return the error if it occured. You're explicitly stating that init cannot return an error. Try change the return type in the signature to !TestStruct. Then create it like so var MyStruct = try TestStruct.init(allocator); More on reddit.com
🌐 r/Zig
8
6
July 2, 2024
List vs Array vs ArrayList
These are three different things: Array - a very basic data structure. It has a fixed, predetermined size that must be known at the time of creating (instantiating) the array. Think of it as a sorting box with several slots. Here, you have .length - without parentheses as it is a property, not a method List - an interface (not a class) that defines certain behavior. It cannot be instantiated on its own. ArrayList - a concrete class that implements the List interface. When you see a declaration like List data = new ArrayList<>(); there are several things going on: Listprogramming against the interface - here, the interface List is used as data type - this is good practice as it allows you to at any time change the internal representation of the List - e.g. from ArrayList to LinkedList if you figure out that the other better suits your needs. this is a data type identifier. It limits the type of elements that can be stored in the list. Here, only String objects are allowed. If you were to omit this, the list would store Object (the ancestor of all Java classes) and descendant instances (i.e. Object and any subclass of it). data - just the variable name new - the keyword to create a new Object Instance ArrayList<>() - the constructor call. Here, the constructor without parameters of the ArrayList class is called. The diamond operator <> is used to infer (take) the data type for the ArrayList from the variable type (here String). So, the whole is: we create a new variable of type List that can only accept String elements and ist internally represented as ArrayList. More on reddit.com
🌐 r/javahelp
15
7
September 12, 2023
Why no default values in ArrayList ??
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
9
2
October 30, 2023
🌐
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.
🌐
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) { ...
🌐
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 ...
🌐
CodeJava
codejava.net › java-core › collections › initialize-list-with-values
Java Initialize ArrayList with Values in One Line
List<User> listUsers = ... 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....
🌐
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 - The Java ArrayList represents a resizable array of objects which allows us to add, remove, find, sort and replace elements. The ArrayList is part of the Collection framework and implements in the List interface. We can initialize an ArrayList in a number of ways depending on the requirement. In this tutorial, we will learn to initialize ArrayList based on some frequently seen usecases. // 1 - Empty ArrayList with initial capacity 10 ArrayList<String> list = new ArrayList<>(); //2 - Empty ArrayList with initial capacity 64 ArrayList<String> list = new ArrayList<>( 64 ); //3 - Initialize and pop
Find elsewhere
🌐
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.
🌐
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 - These methods include using the Arrays.asList() method, double brace initialization, Stream API, and the Collections.addAll() method. Each method has its own benefits and can be used depending on the specific requirements of the program.
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
// Without var ArrayList<String> cars = new ArrayList<String>(); // With var var cars = new ArrayList<String>(); ... This means the variable (cars) is declared as a List (the interface), but it stores an ArrayList object (the actual list).
🌐
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, ...
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java arraylist: how to declare, create, initialize arraylist
Java ArrayList: How to Declare, Create, Initialize ArrayList
April 7, 2020 - 1. Declare an ArrayList ArrayList<String> list; 2. Create an ArrayList ArrayList<String> list = new ArrayList<>(); 3. Initialize an ArrayList With Values ArrayList<String> list = new ArrayList<>(Arrays.asList("Apple", "Banana", "Mango")); 4.
🌐
Java67
java67.com › 2015 › 10 › how-to-declare-arraylist-with-values-in-java.html
How to declare ArrayList with values in Java? Examples | Java67
That's all about how to declare an ArrayList with values in Java. You can use this technique to declare an ArrayList of integers, String, or any other object. It's truly useful for testing and demo purpose, but I have also used this to create an ArrayList of an initial set of fixed values.
🌐
Baeldung
baeldung.com › home › java › java list › initialize an arraylist with zeroes or null in java
Initialize an ArrayList with Zeroes or Null in Java | Baeldung
March 7, 2025 - In this tutorial, we’ll explore different ways to initialize a Java ArrayList with all values null or zero.
🌐
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
Creating and initializing an ArrayList in Java depends on whether you need a mutable list, an immutable list, the number of elements, and whether you want type inference. Below are clear, idiomatic options (one-line variants included). ... Pros: single-line initialization, resulting list is modifiable (add/remove). Caveat: Arrays.asList returns a fixed-size list; wrapping with ...
🌐
Java2Blog
java2blog.com › home › core java › java collections › initialize arraylist with values in java
Initialize ArrayList with values in Java - Java2Blog
January 11, 2021 - In Java 9, Java added some factory methods to List interface to create immutable list in Java. It can be used to initialize ArrayList with values in a single line statement.
🌐
Blogger
javarevisited.blogspot.com › 2012 › 12 › how-to-initialize-list-with-array-in-java.html
How to declare and initialize a List with values in Java (ArrayList/LinkedList) - Arrays.asList() Example
Out of several ways, I think Arrays.asList() + Copy Constructor is best way to initialize ArrayList inline in Java. ... Join My Newsletter .. Its FREE · Everything Bundle (Java + Spring Boot + SQL Interview) for a discount ... How to sort an Array in Java? Ascending and Descen... How to loop through an Array in Java? Example Tuto... How to declare and initialize a List with values i...
🌐
Quora
quora.com › Is-it-really-true-that-you-have-to-initialize-an-ArrayList-with-a-value-in-Java-in-order-to-use-it
Is it really true that you have to initialize an ArrayList with a value in Java in order to use it? - Quora
... Initialization (programmi... ... No — you do not have to initialize an ArrayList with an initial element to use it. You only need to create an instance (i.e., initialize the variable to a new ArrayList object) before calling its methods.
🌐
HelloKoding
hellokoding.com › create-and-initialize-an-arraylist-in-java
Initialize an ArrayList in Java
March 28, 2020 - Provide either Set.of or List.of factory method, since Java 9+, to the ArrayList(Collection) constructor to create and init an ArrayList in one line at the creation time · @Test public void initWithListOfAndSetOf() { List<Integer> arrayList1 = new ArrayList<>(List.of(3, 1, 2)); assertThat(arrayList1).contains(3, 1, 2); List<Integer> arrayList2 = new ArrayList<>(Set.of(5, 4, 6)); assertThat(arrayList2).contains(5, 4, 6); }
🌐
freeCodeCamp
freecodecamp.org › news › how-to-initialize-a-java-list
How to Initialize a Java List – List of String Initialization in Java
April 14, 2023 - To initialize a List with values, we can use the constructor that takes a Collection as an argument. We can pass any collection object that implements the Collection interface to this constructor, such as another ArrayList or a LinkedList.