Java ArrayList
Use a resizable, ordered sequence.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind ArrayList.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
ArrayList stores elements in list order and grows as you add items. Access by index is efficient. Inserting or removing near the front can shift many elements, so consider actual access patterns instead of choosing by name alone.
Explore the details
Java ArrayList
An ArrayList is like a resizable array.
It is part of the java.util package and implements the List interface.
The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to
add or remove elements to/from an array, you have to create a new one). While elements can be added and removed from an ArrayList whenever you want.
Create an ArrayList
To use an ArrayList, you must first import it from java.util:
Now you can use methods like add(), get(), set(), and remove() to manage your list of elements.
Add Elements
To add elements to an
ArrayList, use the add() method:
You can also add an element at a specified position by referring to the index number:
An ArrayList keeps elements in the same order you add them, so the first item you add will be at index 0, the next at index 1, and so on.
Access an Element
To access an element in the ArrayList, use the get() method and refer to the index number:
Change an Element
To modify an element, use the set() method
and refer to the index number:
Remove an Element
To remove an element, use the remove() method
and refer to the index number:
To remove all the elements in the ArrayList, use the clear() method:
ArrayList Size
To find out how many elements an ArrayList have, use the size method:
Loop Through an ArrayList
Loop through the elements of an ArrayList with a for loop, and use the
size() method to specify how many times the loop should run:
You can also loop through an ArrayList with the for-each loop:
Other Types
Elements in an ArrayList are actually objects. In the examples above, we created elements
(objects) of type "String". Remember that a String in Java is an object (not a primitive type). To use other types, such as int, you must specify an equivalent wrapper class: Integer. For other primitive types,
use: Boolean for boolean, Character for char, Double for double,
etc:
Sort an ArrayList
Another useful class in the java.util package is the Collections class, which include the sort() method for sorting lists
alphabetically or numerically:
The var Keyword
From Java 10, you can use the var keyword to declare an ArrayList variable without writing the type twice.
The compiler figures out the type from the value you assign.
This makes code shorter, but many developers still use the full type for clarity.
Since var is valid Java, you may see it in other code, so it's good to know that it exists:
The List Interface
Note: Sometimes you will see both List and ArrayList in Java code, like this:
This means the variable (cars) is declared as a List (the interface), but it stores an ArrayList object (the actual list). Since ArrayList implements the List interface, this is possible.
ArrayList Quick-Reference Methods
| Method | Description | Example |
|---|---|---|
add(e) | Append element to end | list.add("Java") |
add(i, e) | Insert at index | list.add(0, "First") |
get(i) | Retrieve element at index | list.get(2) |
set(i, e) | Replace element at index | list.set(1, "New") |
remove(i) | Remove element at index | list.remove(0) |
remove(obj) | Remove first occurrence of object | list.remove("Java") |
size() | Number of elements | list.size() |
contains(e) | Check if element exists | list.contains("Python") |
indexOf(e) | Index of first occurrence (−1 if absent) | list.indexOf("Java") |
clear() | Remove all elements | list.clear() |
isEmpty() | True if no elements | list.isEmpty() |
Sorting and Filtering with Streams
import java.util.ArrayList;
import java.util.Collections;
import java.util.stream.Collectors;
ArrayList<String> names = new ArrayList<>();
names.add("Charlie"); names.add("Alice"); names.add("Bob"); names.add("Anna");
// Sort alphabetically
Collections.sort(names);
System.out.println(names); // [Alice, Anna, Bob, Charlie]
// Sort in reverse
Collections.sort(names, Collections.reverseOrder());
System.out.println(names); // [Charlie, Bob, Anna, Alice]
// Stream: filter names starting with 'A'
var filtered = names.stream()
.filter(n -> n.startsWith("A"))
.collect(Collectors.toList());
System.out.println(filtered); // [Anna, Alice]
// Stream: uppercase all
names.stream()
.map(String::toUpperCase)
.forEach(System.out::println);It works the same way, but some developers prefer this style because it gives them more flexibility to change the type later.
A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws Exception {
ArrayList<String> topics = new ArrayList<>();
topics.add("Java");
topics.add("SQL");
topics.set(1, "Collections");
System.out.println(topics);
System.out.println(topics.size());
}
}Predict first. Then reveal the expected output / வெளியீடு
[Java, Collections] 2
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Insert Basics at index 0, then print the list.
Reveal the answer and reasoning / விடை
Use topics.add(0, "Basics");. The order becomes [Basics, Java, Collections].
One mistake to avoid: For ArrayList<Integer>, remove(1) removes by index. remove(Integer.valueOf(1)) removes a matching Integer value.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.