allthingsare๐Ÿ…ฟ๏ธ.com Books โฌ…๏ธ Back allthingsare

โ€œThe light shines in the darkness, and the darkness has not overcome it.โ€
[John 1:5]



ArrayList


7.2.1 Why Use ArrayList

While arrays are perfect for storing fixed-size data, most real-world applications need collections that can grow and shrink dynamically.

This is where ArrayList shines.
An ArrayList works much like an array but can automatically resize as you add or remove elements.
Internally, it uses a standard array but manages resizing for you.

Key features of ArrayList:

  • Add elements anytime with .add()

  • Remove elements with .remove()

  • Insert elements at any position

  • Size changes dynamically

  • Stores only objects, so you canโ€™t put primitives directly (Java auto-boxes them, e.g. int becomes Integer)

ArrayList is great for:

  • To-do lists,

  • Shopping carts,

  • Dynamic search results,

  • Building and filtering collections on the fly.


Mini Lab

java
import java.util.ArrayList; public class ArrayListIntro { public static void main(String[] args) { ArrayList<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); names.add("Charlie"); System.out.println("Names: " + names); names.remove("Bob"); System.out.println("After removal: " + names); names.add(1, "David"); System.out.println("After insertion: " + names); } }

7.2.2 Looping Over an ArrayList

An ArrayList works naturally with both for loops and enhanced for-each loops.

Unlike arrays, you use .size() instead of .length.
You also use .get(index) to access items by index.

Looping over an ArrayList is common for:

  • Displaying items,

  • Searching,

  • Filtering or transforming data,

  • Calculating totals.


Mini Lab

java
import java.util.ArrayList; public class ArrayListLoop { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); numbers.add(30); System.out.println("Using for-each:"); for (int n : numbers) { System.out.println("Number: " + n); } System.out.println("Using index:"); for (int i = 0; i < numbers.size(); i++) { System.out.println("Index " + i + ": " + numbers.get(i)); } } }

7.2.3 Array vs. ArrayList โ€” When to Use Which

When should you choose a plain array vs. an ArrayList?

Use an array if:

  • You know the exact size up front.

  • You need maximum speed and low memory overhead.

  • You work with performance-critical code.

Use ArrayList if:

  • You donโ€™t know how many items youโ€™ll have.

  • You need to add, insert, or remove items freely.

  • You want built-in methods for collection operations.

In practice, ArrayList is the go-to choice for general-purpose lists in modern Java.
But arrays still shine for small, simple, or low-level operations.


Mini Lab

java
import java.util.ArrayList; public class ArrayListVsArray { public static void main(String[] args) { int[] numbersArray = {1, 2, 3}; ArrayList<Integer> numbersList = new ArrayList<>(); numbersList.add(1); numbersList.add(2); numbersList.add(3); System.out.println("Array length: " + numbersArray.length); System.out.println("ArrayList size: " + numbersList.size()); numbersList.add(4); System.out.println("ArrayList after adding 4: " + numbersList); // Arrays are fixed // numbersArray[3] = 4; // Would cause error } }