Skip to content

ArrayLists – Super Flexible Arrays

Seanti edited this page Mar 24, 2025 · 1 revision

Now that we know about arrays, let’s talk about something even better: ArrayLists!

➡️ Think of an ArrayList as a magical box that can grow and shrink whenever you need it.
➡️ Unlike arrays, you don’t need to set a fixed size beforehand.


1. Why Use an ArrayList Instead of an Array?

Feature Arrays ArrayLists (✅ Better in Many Cases)
Fixed Size? ✅ Yes (Must define size) ❌ No (Can grow/shrink anytime)
Easy to Add/Remove? ❌ No (Hard to add/remove items) ✅ Yes (Built-in methods)
Stores Objects? ✅ Yes ✅ Yes

Example:

Imagine you have a box of gumballs.

  • With an array, you must decide beforehand how many gumballs will fit.
  • With an ArrayList, you can keep adding gumballs as needed—no need to worry about space!

2. Creating an ArrayList

To use an ArrayList, we need to import it first.

import java.util.ArrayList; // Import ArrayList

public class GumballList {
    public static void main(String[] args) {
        ArrayList<String> gumballs = new ArrayList<>(); // Create an ArrayList

        gumballs.add("Red");    // Add items
        gumballs.add("Blue");
        gumballs.add("Green");

        System.out.println(gumballs); // Print all gumballs
    }
}

Output:

[Red, Blue, Green]

🔹 .add("Red") → Adds "Red" to the list.
🔹 .add("Blue") → Adds "Blue" next.

➡️ Unlike arrays, we don’t need to define a size! The ArrayList automatically resizes as we add items.


3. Accessing Items in an ArrayList

Just like arrays, ArrayLists use index numbers (starting from 0).

System.out.println(gumballs.get(0)); // Get first item

Example:

public class GumballList {
    public static void main(String[] args) {
        ArrayList<String> gumballs = new ArrayList<>();
        gumballs.add("Red");
        gumballs.add("Blue");
        gumballs.add("Green");

        System.out.println("First gumball: " + gumballs.get(0));
        System.out.println("Second gumball: " + gumballs.get(1));
    }
}

Output:

First gumball: Red
Second gumball: Blue

🔹 get(0) → Gets the first item ("Red").
🔹 get(1) → Gets the second item ("Blue").


4. Changing an Item

We can replace an item using .set(index, newValue).

gumballs.set(1, "Purple"); // Change "Blue" to "Purple"

Example:

public class GumballList {
    public static void main(String[] args) {
        ArrayList<String> gumballs = new ArrayList<>();
        gumballs.add("Red");
        gumballs.add("Blue");
        gumballs.add("Green");

        gumballs.set(1, "Purple"); // Change "Blue" to "Purple"

        System.out.println(gumballs);
    }
}

Output:

[Red, Purple, Green]

🔹 "Blue" at index 1 is now "Purple".


5. Removing Items

We can remove items using .remove(index).

gumballs.remove(0); // Removes the first gumball

Example:

public class GumballList {
    public static void main(String[] args) {
        ArrayList<String> gumballs = new ArrayList<>();
        gumballs.add("Red");
        gumballs.add("Blue");
        gumballs.add("Green");

        gumballs.remove(0); // Removes "Red"

        System.out.println(gumballs);
    }
}

Output:

[Blue, Green]

🔹 The first item ("Red") is removed.


6. Checking the Size of an ArrayList

To find how many items are inside an ArrayList, use .size().

System.out.println(gumballs.size()); // Get total number of items

Example:

public class GumballList {
    public static void main(String[] args) {
        ArrayList<String> gumballs = new ArrayList<>();
        gumballs.add("Red");
        gumballs.add("Blue");
        gumballs.add("Green");

        System.out.println("Total gumballs: " + gumballs.size());
    }
}

Output:

Total gumballs: 3

🔹 .size() returns the number of items in the list.


7. Looping Through an ArrayList

Instead of printing each item manually, we can use a loop.

1️⃣ Using a For Loop

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

Example:

public class GumballList {
    public static void main(String[] args) {
        ArrayList<String> gumballs = new ArrayList<>();
        gumballs.add("Red");
        gumballs.add("Blue");
        gumballs.add("Green");

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

Output:

Red
Blue
Green

🔹 Loops through each item in the ArrayList and prints it.


2️⃣ Using an Enhanced For Loop (Easier Way)

for (String gumball : gumballs) {
    System.out.println(gumball);
}

Example:

public class GumballList {
    public static void main(String[] args) {
        ArrayList<String> gumballs = new ArrayList<>();
        gumballs.add("Red");
        gumballs.add("Blue");
        gumballs.add("Green");

        for (String gumball : gumballs) {
            System.out.println(gumball);
        }
    }
}

Output:

Red
Blue
Green

🔹 This is a simpler way to loop through all items.


8. Summary of ArrayLists

Feature What It Does Example
Create an ArrayList Makes a resizable list ArrayList<String> list = new ArrayList<>();
Add Items Adds items to the list .add("Item")
Get Items Gets a specific item .get(index)
Change Items Changes an item .set(index, newValue)
Remove Items Removes an item .remove(index)
Get Size Finds how many items are in the list .size()
Loop Through List Goes through all items for (String item : list) {}

9. What’s Next?

Now that we can store multiple values flexibly using ArrayLists, we’ll move on to HashMaps, which let us store key-value pairs like a dictionary! 🚀

Clone this wiki locally