Featured Article

Initializing an ArrayList with Values in Java: A Step-by-Step Guide

Alan Jun 16, 2026

Initializing an ArrayList with Values in Java

How to declare and initialize a List with values in Java (ArrayList/LinkedList)  - Arrays.asList() Example
How to declare and initialize a List with values in Java (ArrayList/LinkedList) - Arrays.asList() Example

In Java, ArrayList is a resizable-array implementation of the List interface, part of the Java Collections Framework. It allows you to store ordered collections of objects, and it's often used when you need to store a collection of objects that allow duplicate elements. Initializing an ArrayList with values is a common task, and this article will guide you through the process.

an array of numbers that are not in the form of words, and have been used to
an array of numbers that are not in the form of words, and have been used to

Understanding ArrayList

Before we dive into initializing an ArrayList with values, let's briefly understand what an ArrayList is. An ArrayList in Java is a dynamic array, meaning it can grow and shrink as needed. It provides fast random access to its elements, but slow linear access. It's a good choice when you need to store a collection of objects and access them by their index.

Absolute Value Function Explained | Easy Algebra & V-Shaped Graph Guide
Absolute Value Function Explained | Easy Algebra & V-Shaped Graph Guide

Initializing an Empty ArrayList

To initialize an empty ArrayList, you simply declare it and create an instance. Here's how you do it:

How to declare and initialize a List with values in Java (ArrayList/LinkedList)  - Arrays.asList() Example
How to declare and initialize a List with values in Java (ArrayList/LinkedList) - Arrays.asList() Example

ArrayList<String> list = new ArrayList<String>();

In this example, we're creating an ArrayList that can hold strings. The diamond operator (<>) is used to infer the type arguments from the context.

Initializing an ArrayList with Initial Values

You can also initialize an ArrayList with initial values using the constructor that takes a collection. Here's how you can do it:

C# Array and List
C# Array and List

ArrayList<String> list = new ArrayList<String>(Arrays.asList("Apple", "Banana", "Cherry"));

In this example, we're using the Arrays.asList() method to create a list of strings, and then passing it to the ArrayList constructor. The resulting ArrayList will contain the strings "Apple", "Banana", and "Cherry".

Initializing an ArrayList with a Specific Size

If you know the initial size of the ArrayList, you can specify it in the constructor. This can improve performance if you know you'll need to store a large number of elements. Here's how you do it:

the functions of an array in programming and visual language are shown on top of each other
the functions of an array in programming and visual language are shown on top of each other

ArrayList<String> list = new ArrayList<String>(10);

In this example, we're creating an ArrayList that can hold 10 strings. If you add more than 10 elements, the ArrayList will automatically grow.

Adding Elements to an ArrayList

an image of a table with the words's and numbers on it, which are in
an image of a table with the words's and numbers on it, which are in
a table that has different types of numbers and words on it, as well as the names
a table that has different types of numbers and words on it, as well as the names
excel_funclub
excel_funclub
Think ๐Ÿ’ญ
Think ๐Ÿ’ญ
Difference between Array List and Linked List
Difference between Array List and Linked List
the list of names and numbers for each type of item in this chart, you can see
the list of names and numbers for each type of item in this chart, you can see
Personal Value Chart, Community Values List, Values Sheet, Values Chart, Personal Core Values Worksheet, Values Worksheets For Adults, Strengths List, Core Values Vs Core Beliefs, Core Values Assessment Worksheet
Personal Value Chart, Community Values List, Values Sheet, Values Chart, Personal Core Values Worksheet, Values Worksheets For Adults, Strengths List, Core Values Vs Core Beliefs, Core Values Assessment Worksheet
a purple screen with the words x on it and an image of a computer keyboard
a purple screen with the words x on it and an image of a computer keyboard
the 20 core life values poster
the 20 core life values poster
the list of value words in english
the list of value words in english
an image of the basic instructions for using excelf formulas to help students learn how to
an image of the basic instructions for using excelf formulas to help students learn how to
Anil Patel on LinkedIn: #freshers #sql #itjobsearch | 15 comments
Anil Patel on LinkedIn: #freshers #sql #itjobsearch | 15 comments
an interactive poster describing the different types of plants and how they are used to describe them
an interactive poster describing the different types of plants and how they are used to describe them
a list of words that are in the same language as each word, which has different meanings
a list of words that are in the same language as each word, which has different meanings
3.2K views ยท 673 reactions | Stop wasting time searching symbols in Excel! โณ Use these Alt Codes and work like a pro ๐Ÿ’ปโšก Save this post & level up your Excel game ๐Ÿš€ Follow @excel_2_data for daily smart shortcuts #Ex | Excel Satish
3.2K views ยท 673 reactions | Stop wasting time searching symbols in Excel! โณ Use these Alt Codes and work like a pro ๐Ÿ’ปโšก Save this post & level up your Excel game ๐Ÿš€ Follow @excel_2_data for daily smart shortcuts #Ex | Excel Satish
a table with some words on it and numbers in the top right hand corner, which are
a table with some words on it and numbers in the top right hand corner, which are
the basic excel formats for each type of document is shown in this diagram, which shows
the basic excel formats for each type of document is shown in this diagram, which shows
Array vs Linked List | Difference Between Arrays And Linked List | Data Structures | Simplilearn
Array vs Linked List | Difference Between Arrays And Linked List | Data Structures | Simplilearn
an image of the back side of a computer screen with text and numbers on it
an image of the back side of a computer screen with text and numbers on it

Once you've initialized your ArrayList, you can add elements to it using the add() method. Here's how you do it:

list.add("Date");
list.add("Elderberry");

In this example, we're adding the strings "Date" and "Elderberry" to the ArrayList.

Accessing Elements in an ArrayList

To access an element in an ArrayList, you use the get() method and pass in the index of the element you want to access. Here's how you do it:

String fruit = list.get(0);

In this example, we're getting the first element in the ArrayList and storing it in the string variable "fruit".

Removing Elements from an ArrayList

To remove an element from an ArrayList, you use the remove() method and pass in the index of the element you want to remove. Here's how you do it:

list.remove(1);

In this example, we're removing the second element in the ArrayList.

Conclusion

Initializing an ArrayList with values is a straightforward process in Java. Whether you're initializing an empty ArrayList, an ArrayList with initial values, or an ArrayList with a specific size, the process is simple and efficient. Once you've initialized your ArrayList, you can add, access, and remove elements as needed.