-
Notifications
You must be signed in to change notification settings - Fork 0
ArrayLists – Super Flexible Arrays
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.
| 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 |
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!
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
}
}[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.
Just like arrays, ArrayLists use index numbers (starting from 0).
System.out.println(gumballs.get(0)); // Get first itempublic 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));
}
}First gumball: Red
Second gumball: Blue
🔹 get(0) → Gets the first item ("Red").
🔹 get(1) → Gets the second item ("Blue").
We can replace an item using .set(index, newValue).
gumballs.set(1, "Purple"); // Change "Blue" to "Purple"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);
}
}[Red, Purple, Green]
🔹 "Blue" at index 1 is now "Purple".
We can remove items using .remove(index).
gumballs.remove(0); // Removes the first gumballpublic 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);
}
}[Blue, Green]
🔹 The first item ("Red") is removed.
To find how many items are inside an ArrayList, use .size().
System.out.println(gumballs.size()); // Get total number of itemspublic 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());
}
}Total gumballs: 3
🔹 .size() returns the number of items in the list.
Instead of printing each item manually, we can use a loop.
for (int i = 0; i < gumballs.size(); i++) {
System.out.println(gumballs.get(i));
}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));
}
}
}Red
Blue
Green
🔹 Loops through each item in the ArrayList and prints it.
for (String gumball : gumballs) {
System.out.println(gumball);
}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);
}
}
}Red
Blue
Green
🔹 This is a simpler way to loop through all items.
| 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) {} |
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! 🚀