ArrayList

A plain array has a fixed size — once it's 5 elements long, it's always 5. Real programs rarely know the size in advance. ArrayList is a resizable list that grows and shrinks on demand, with friendly methods to add, find, and remove items.

Learn ArrayList in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Array vs ArrayList

An array is a fixed-size row of boxes. An ArrayList is a row of boxes that can add or remove boxes whenever you like.

You need to import it: import java.util.ArrayList; . The part in angle brackets — the generic type — declares what the list holds, e.g. ArrayList<String> for text or ArrayList<Integer> for whole numbers.

2️⃣ The Methods You'll Use Daily

Method

What it does

add(x)

Append x to the end

add(i, x)

Insert x at index i

get(i)

Read the element at index i

set(i, x)

Replace the element at index i

remove(i) / remove(x)

Delete by index or by value

size()

Number of elements

contains(x)

Is x in the list?

indexOf(x)

Position of x, or -1

isEmpty()

true if size is 0

clear()

Remove everything

3️⃣ Sorting With Collections

The java.util.Collections class provides static helpers that operate on any list:

Note the easy mix-up: Collections (helper methods) is a different class from Collection (the interface all collections implement).

🧩 Reorder Challenge

Reorder these lines to create a list of two colors and print its size.

Why: The list must be created before anything can be added to it, and both add calls must run before size() reports the count. Output: 2 .

🧠 Quick Recall

[y, x] . add(0, "y") inserts "y" at index 0, pushing "x" to index 1.

[10, 30] . remove(1) deletes the element at index 1 (the value 20), not the value 1.

3. What does indexOf return for a value that isn't present?

-1 . That's the universal "not found" sentinel; check contains first if you only need a yes/no.

Common Beginner Mistakes

📋 Quick Reference

Description

Example

new ArrayList<>()

Create an empty list

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

add(e)

Append to the end

l.add("x")

add(i, e)

Insert at index i

l.add(0, "y")

Read element at index i

l.get(0)

set(i, e)

Replace element at index i

l.set(0, "z")

remove(i)

Delete element at index i

l.remove(1)

remove(e)

Delete first matching value

l.remove("x")

l.size()

contains(e)

Is e in the list?

l.contains("x")

indexOf(e)

Position of e, or -1

l.indexOf("x")

true when size is 0

l.isEmpty()

Remove every element

l.clear()

for (T e : l)

Loop over every element

for (String s : l)

Collections.sort(l)

Sort ascending in place

List.of(...)

Fixed, immutable list literal

List.of("a", "b")

Frequently Asked Questions

🎉 Lesson Complete!

You can now use Java's workhorse collection: create a generic ArrayList , add/get/set/remove elements, query it with size / contains / indexOf , loop with for-each, and sort or reverse with Collections . You also know the remove(index) vs remove(value) trap.

Next up: HashMap — store key→value pairs for instant lookups, like a phone book or a word-count tally.

Practice quiz

What is the key advantage of an ArrayList over a plain array?

  • It is always faster
  • It can hold primitives directly
  • It grows and shrinks automatically
  • It needs no import

Answer: It grows and shrinks automatically. An ArrayList resizes automatically, unlike a fixed-size array.

Which is the correct way to declare a list of whole numbers?

  • ArrayList<Integer>
  • ArrayList<int>
  • ArrayList<number>

Answer: ArrayList<Integer>. Lists hold objects, so use the wrapper Integer, not the primitive int.

Given a list [10, 20, 30], what does remove(1) do?

  • Removes the value 1
  • Removes the first element
  • Throws an error
  • Removes the element at index 1 (the 20)

Answer: Removes the element at index 1 (the 20). remove(int) removes by index, so remove(1) deletes index 1 (the value 20), leaving [10, 30].

Continue this course