Learn Java
import com.learnjava.datastructurescollections.Arraylist;

ArrayList in Java

@Concept

ArrayList, from java.util, is a resizable array. Unlike a plain array, it grows automatically as elements are added, with no fixed size decided up front. Internally, it still stores elements in a real array behind the scenes, and it manages growing that backing array transparently whenever more space is needed.

Reading or writing by index (get, set) is fast regardless of the list's size, since it's just indexing into the backing array directly. But inserting or removing from anywhere except the very end requires shifting every subsequent element over by one position, which gets slower as the list grows.

@HowItWorks
1

Creating one

new ArrayList<Integer>() starts empty; its type parameter (Integer here) fixes what kind of element it can hold.

2

Adding elements

add(value) appends to the end; add(index, value) inserts at a specific position, shifting later elements over.

3

Reading and writing by index

get(index) and set(index, value) both run in constant time, just like indexing a plain array.

4

Removing elements

remove(index) or remove(Object) both shift every element after the removed one back by one position.

5

Automatic resizing

Once the backing array fills up, ArrayList transparently allocates a larger one and copies existing elements across, all without the caller doing anything.

@Example
import java.util.ArrayList;
 
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Ana");
names.add("Bo");
names.add(0, "Zed");
System.out.println(names);
System.out.println("Size: " + names.size());
}
}
Output
[Zed, Ana, Bo]
Size: 3
1Two names are appended to the end in order, making the list ["Ana", "Bo"].
2add(0, "Zed") inserts "Zed" at the very front, shifting "Ana" and "Bo" each one position later.
3Printing the list directly calls its built-in toString(), producing that bracketed, comma-separated form automatically.
4size() reports 3, matching the three elements now in the list.
@Complexity
time get/set: O(1); add at end: O(1) amortized; add/remove at an index: O(n)
space O(n)

Random access and appending are fast because they touch the backing array directly. Inserting or removing anywhere but the end costs a shift proportional to how many elements come after it.

@UseCases
Storing a collection of items whose final count isn't known ahead of time
Building a list of results incrementally while processing some other data
Choosing ArrayList over a plain array when elements need to be added or removed dynamically
Reading elements by index frequently, where ArrayList's constant-time get() matters

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.