Please enable JavaScript to use CodeHS

Java Basics

ArrayLists in Java

Arrays are stuck at the size you give them. ArrayLists aren't — learn to add, remove, and resize a list while your program runs.

Beginner ~12 min All
The Big Idea

Arrays are fixed. ArrayLists aren't.

You already know that a Java array is locked in at the size you give it:

int[] scores = new int[4]; // exactly 4 slots, forever

That's fine when you know exactly how many things you need to store. But a lot of the time, you don't. Maybe students keep joining a class roster, or a shopping cart keeps getting new items added and removed. An array can't grow a fifth slot once it's created — you'd have to build a whole new, bigger array and copy everything over.

An ArrayList solves this. It's Java's built-in list that can grow and shrink while your program is running:

ArrayList<String> names = new ArrayList<String>(); // starts empty
names.add("Ada");
names.add("Alan"); // now it holds 2 items — no resizing math required

Under the hood, an ArrayList is still backed by an array — Java just handles the resizing for you automatically, so you never have to think about it.


How It Works

Declaring and using an ArrayList

To use ArrayList, first import it. Then declare one with the type of thing it will hold in angle brackets < >:

import java.util.ArrayList;

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

That <String> is called a generic type. It works a lot like the type before the brackets in an array declaration (String[] or int[]) — it tells Java, and anyone reading your code, what kind of value every item in the list will be. Here, every item added to names must be a String.

One Java quirk to remember: you can't write ArrayList<int>. Generics only work with object types, not primitives like int, double, or boolean. Use the "wrapper" versions instead: Integer, Double, Boolean. So a list of numbers looks like ArrayList<Integer> scores = new ArrayList<Integer>(); — you can still add and read plain int values like 90, Java converts automatically behind the scenes.

The core methods

Once you have an ArrayList, here's what you can do with it:

.add(item) — appends an item to the end of the list.

names.add("Grace"); // names is now ["Ada", "Alan", "Grace"]

.add(index, item) — inserts an item at a specific position, pushing everything after it back by one.

names.add(1, "Katherine"); // ["Ada", "Katherine", "Alan", "Grace"]

.get(index) — reads the item at a position, just like names[index] would for an array.

System.out.println(names.get(0)); // Ada

.set(index, item) — replaces the item already at a position, without changing the list's size.

names.set(0, "Ada Lovelace"); // ["Ada Lovelace", "Katherine", "Alan", "Grace"]

.remove(index) — deletes the item at a position, and everything after it shifts up to fill the gap.

names.remove(2); // removes "Alan" → ["Ada Lovelace", "Katherine", "Grace"]

.size() — tells you how many items are currently in the list.

System.out.println(names.size()); // 3

The gotcha everyone hits first: arrays use .length — no parentheses, because it's a field. ArrayLists use .size() — with parentheses, because it's a method. Writing names.length or scores.size (missing the parens) won't compile. It's a small difference, but it trips up almost everyone the first week they use ArrayList.


Looping

Looping over an ArrayList

Just like with arrays, you can loop over an ArrayList with a classic indexed for loop. Use .size() instead of .length as the stopping point, and .get(i) instead of [i] to read each item:

for (int i = 0; i < names.size(); i++) {
    System.out.println(names.get(i));
}

Java also gives you an enhanced for-each loop, which reads more simply when you just need each value and don't care about its index:

for (String name : names) {
    System.out.println(name);
}

Both loops print the same thing here. Reach for the indexed version when you need the position (to skip an index, compare neighbors, or call .set()); reach for the for-each version the rest of the time — it's shorter and there's no index math to get wrong.


Interactive

Build an ArrayList

Type a value and click add() to grow the list. Click a box to select it, type a new value, and click set() to change it — or click a box's × to remove it. Watch the equivalent Java code build itself as you go.

Click a box below to select it before using set().


        

Your Turn

Check yourself

Write the line of code that removes the item at index 2 from an ArrayList called scores.

Sample answer: scores.remove(2);


Code

A Java ArrayLists example

This program builds a list of scores, inserts, removes, and updates a few of them, then loops over what's left to print each score and compute the average.


Check Yourself

Quick quiz

1. Why would you use an ArrayList instead of an array in Java?

2. How do you find how many items are in an ArrayList called names?

3. Given this code, what does pets.get(0) return at the end?

ArrayList<String> pets = new ArrayList<String>();
pets.add("Cat");
pets.add("Dog");
pets.set(0, "Fish");
pets.remove(1);

Keep learning

You've gone from fixed-size arrays to ArrayLists that grow and shrink on demand. Ready for the next topic?