Chapter 43 of 57

ArrayList in Java

We already learned about arrays, which allow us to store multiple values of the same type. But arrays have one important limitation: their size is fixed.

For example:

int[] numbers = new int[3];

This array can hold exactly 3 elements. If later we need to store a fourth value, we cannot simply increase the size of the same array.

This is where ArrayList becomes useful.

An ArrayList is a collection that allows us to store multiple values and change its size dynamically.

Creating an ArrayList

ArrayList is part of the java.util package, so we first need to import it:

import java.util.ArrayList;

Then we can create an ArrayList:

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

Here, String tells Java that this ArrayList will store Strings.

We can add values using the add() method:

names.add("John");
names.add("Jason");
names.add("Alex");

Now the list contains:

John
Jason
Alex

Accessing Elements

Just like arrays, an ArrayList uses indexes, and the first element is at index 0.

We can use the get() method to access an element:

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

Output:

John

To get the second element:

System.out.println(names.get(1));

Output:

Jason

So the indexes look like this:

Index:   0       1        2
Value: John    Jason    Alex

Adding Elements

One of the biggest advantages of ArrayList is that we can keep adding elements.

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

names.add("John");
names.add("Jason");
names.add("Alex");
names.add("Michael");

We didn't need to specify the size beforehand.

The ArrayList automatically manages its internal storage as it grows.

Changing an Element

We can change an existing element using the set() method.

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

names.add("John");
names.add("Jason");
names.add("Alex");

names.set(1, "Michael");

Now the list becomes:

John
Michael
Alex

The value at index 1 was changed from "Jason" to "Michael".

Removing an Element

We can remove an element using the remove() method.

For example:

names.remove(1);

This removes the element at index 1.

We can also remove an element by its value:

names.remove("Alex");

This removes the matching value from the list.

Finding the Size

For arrays, we use:

array.length

For an ArrayList, we use the size() method:

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

For example:

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

names.add("John");
names.add("Jason");
names.add("Alex");

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

Output:

3

Checking Whether an Element Exists

We can use contains() to check whether a particular value exists in the list.

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

names.add("John");
names.add("Jason");

System.out.println(names.contains("John"));
System.out.println(names.contains("Alex"));

Output:

true
false

This can be useful when we need to check whether a particular item is already present.

Looping Through an ArrayList

We can use a normal for loop:

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

names.add("John");
names.add("Jason");
names.add("Alex");

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

Output:

John
Jason
Alex

Notice that we use:

names.size()

instead of names.length.

We can also use an enhanced for loop:

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

This is often cleaner when we simply want to process every element.

ArrayList with Numbers

There is one important thing to understand when storing numbers in an ArrayList.

You cannot use primitive types such as int directly:

ArrayList<int> numbers; // Error

Instead, Java provides wrapper classes.

For int, we use Integer:

ArrayList<Integer> numbers = new ArrayList<>();

Now we can add numbers:

numbers.add(10);
numbers.add(20);
numbers.add(30);

Java automatically converts the primitive int values into Integer objects when needed. This process is called autoboxing.

We can access them normally:

System.out.println(numbers.get(0));

Output:

10

Similarly, we can use wrapper classes such as Double, Character, and Boolean with ArrayList.

Array vs ArrayList

The main difference is that an array has a fixed size, while an ArrayList can grow or shrink dynamically.

For example:

int[] numbers = new int[3];

The size is fixed at 3.

With:

ArrayList<Integer> numbers = new ArrayList<>();

we can keep adding or removing elements.

An array can store primitive values directly, such as int and double, while an ArrayList stores objects and uses wrapper classes such as Integer and Double for primitive values.

Arrays are useful when you know the size and need simple, fixed-size storage. ArrayList is often more convenient when the number of elements can change.

A Complete Example

Let's create a simple program that stores student names:

import java.util.ArrayList;

class Main {
    public static void main(String[] args) {

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

        students.add("John");
        students.add("Jason");
        students.add("Alex");

        students.set(1, "Michael");

        for (String student : students) {
            System.out.println(student);
        }
    }
}

Output:

John
Michael
Alex

Here, we created an ArrayList, added students, changed one student's name, and then used a loop to display the list.

The main thing to remember is:

ArrayList is a resizable collection that allows you to easily add, remove, access, and modify elements.

When you need a collection whose size can change during the program, ArrayList is often a better choice than a regular array.