How to use Java ArrayList
ArrayList is one of the most used data structures in Java. It allows you to dynamically modify and store a collection of objects. In this tutorial, we introduce you to the syntax of Java ArrayList and explain when to use it.
What is the difference between ArrayList and Array in Java?
Java ArrayList is dynamic, which means that it grows and shrinks in size when elements are added or removed. It is worth noting that the ArrayList class is part of the Java Collections Framework and is not available natively. Unlike arrays, it must be imported from the java.util library.
ArrayLists are a suitable choice when the length of a Java List may potentially vary. Examples include storing objects, searching or sorting data, and creating lists or queues.
In contrast, it’s not possible to change the size of an array. This means that you should ideally know the number of objects that the array will hold in advance. Arrays are suitable for managing a predefined set of primitive data types such as int, float, char or Boolean.
One drawback of ArrayLists is they can take longer to access. While arrays have a fixed continuous block of memory, objects in ArrayLists are not stored contiguously. It is important to take the advantages and disadvantages of the data structures into account so you are able to select the one that works best for your use case.
What is the syntax for Java ArrayList?
Before creating ArrayList, the corresponding class must be imported from the java.util library.
The general syntax is:
“Type” stands for the respective data type in Java ArrayList.
The next step is to create lists of strings
and integers
.
ArrayLists use the corresponding wrapper class of primitive data types so that they are treated like objects. This means you have to specify integer
instead of int
.
Examples of Java ArrayList methods
Operations such as adding or removing elements are not performed on ArrayLists with Java operators, but via predefined methods. We’ll show you the most common ArrayList methods below.
Adding elements
After creating the ArrayList “colors” (String
type), we’ll add various elements using the .add()
method.
This results in:
Removing elements
To remove objects from Java ArrayList, we use the .remove()
method and specify the element’s index.
The output shows the modified ArrayList and the element that has been removed:
As with most programming languages, counting in Java starts from 0. This means that the removed element at index 1 is red
.
Accessing elements in Java ArrayList
Using the function .get()
, we can access an element at a specific position.
For the output we get:
Changing elements
With .set()
, we can change an element by assigning a new element at a specific index.
We now see yellow
instead of green
at index 2:
Determining the length of Java ArrayList
The number of elements in ArrayList can easily be calculated using the .size()
method.
This is the result:
Sorting and iterating through ArrayList
To sort Java ArrayList, the collections class must be imported. For the iteration, we use a Java for-each loop. For each iteration of the loop, the respective element is output to the console.
The elements of ArrayList are displayed from smallest to largest: