Skip to content
QuizMaker logoQuizMaker
Activity
Java Programming: From Zero to Enterprise
3. Core APIs & I/O
1. Getting Started with Java & the JVM
2. Data Types & Variables
3. Control Flow: Ifs & Loops
4. String Manipulation in Depth
5. Methods (Functions) Architecture
6. Arrays & The Enhanced For Loop
7. User Input via Scanner
8. Mathematical Operations & The Math Class
9. Operators in Depth
10. Block Scope & Variable Lifecycles
11. Introduction to Object-Oriented Programming
12. Classes & Instances Deep Dive
13. Constructors
14. Encapsulation & The 'this' Keyword
15. Inheritance: Extending Functionality
16. Polymorphism & Method Overriding
17. Abstraction & Abstract Classes
18. Interfaces: The Ultimate Contract
19. Packages & Access Modifiers
20. Enums (Enumerations)
21. Exceptions: Handling Runtime Errors
22. The 'throw' and 'throws' keywords
23. Dates, Times, and Formatting
24. Enumerable Data Structures
25. LinkedLists: The Alternative
26. HashMaps: Key-Value Architecture
27. HashSets: The Art of Uniqueness
28. Iterator: Safe Collection Traversal
29. Wrapper Classes & Autoboxing
30. Basic File I/O
31. Generics: Type-Safe Templates
32. Lambda Expressions & Functional Interfaces
33. The Stream API: Functional Data Pipelines
34. Optional: Beating the NullPointerException
35. Multithreading & Concurrency Basics
36. JDBC: Connecting to SQL Databases
37. Annotations & Reflection
38. The JVM Garbage Collector
39. Introduction to Spring Boot
40. Unit Testing with JUnit
41. Java Collections for DSA
CONTENTS

24. Enumerable Data Structures

Ditching raw arrays for memory-safe List constructs.

Java Programming: From Zero to Enterprise
3. Core APIs & I/O
February 22, 2026
161
A

[!NOTE] In chapter 6, we learned about standard Arrays (e.g., String[] cars = new String[5];). Standard arrays are incredibly fast, but they have a fatal flaw: their size cannot be changed once created.

If you don't know how many users will sign up for your application today, you cannot use a strictly sized array. You need a data structure that dynamically grows or shrinks instantly as data is added or removed.

Introducing ArrayList

The ArrayList class is a resizable array, which is completely built-in to the java.util package.

Under the hood, an ArrayList is secretly just a standard [] array. When it gets full, Java silently creates a brand new array double the size in memory, copies all the old data over rapidly, and destroys the old array without bothering you.

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    // 1. Creation. We MUST use the primitive wrapper class (Integer, not int)!
    ArrayList<String> cars = new ArrayList<String>();
    
    // 2. Dynamic Addition. We didn't specify a size limit!
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    
    // 3. Retrieval. (We use .get() instead of standard [0] brackets)
    System.out.println(cars.get(0)); // Outputs "Volvo"
    
    // 4. Modification
    cars.set(0, "Opel");
    
    // 5. Deletion. All elements automatically shift to fill the gap safely!
    cars.remove(1); // Deletes BMW
    
    // 6. Checking dynamic size
    System.out.println(cars.size()); // Outputs 3
  }
}

Primitive Wrapper Classes

An annoying quirk of ArrayList is that it can only store fully-featured Objects on the Heap. It physically cannot store stack-based primitive variables like int, double, or boolean.

To fix this, Java provides Object wrappers for all primitives. They act identically, but cost slightly more memory.

  • Use Integer instead of int
  • Use Double instead of double
  • Use Boolean instead of boolean
  • Use Character instead of char
// ERROR: Syntax error on token "int", Dimensions expected!
// ArrayList<int> myNumber = new ArrayList<int>();

// CORRECT!
ArrayList<Integer> myNumbers = new ArrayList<Integer>();
myNumbers.add(5);
myNumbers.add(10);

Looping through ArrayLists

Just like arrays, you can use a classic loop or an enhanced for-each loop. Notice how we use .size() instead of .length!

for (String car : cars) {
    System.out.println(car);
}

Choosing Between Array and ArrayList

Arrays are fixed and simple. ArrayList is flexible and usually easier for application code. In most beginner projects, if the number of items can change, use ArrayList.

Common ArrayList Operations

ArrayList<String> names = new ArrayList<>();
names.add("Asha");
names.add("Ravi");
names.add("Meera");

names.set(1, "Rahul");
boolean hasAsha = names.contains("Asha");
int count = names.size();

Use the Interface Type

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

Declaring the variable as List keeps your code flexible. You can change the implementation later if needed without changing most callers.

Common Mistakes

  • Using list.length instead of list.size().
  • Using array brackets with ArrayList instead of get() and set().
  • Removing items in a for-each loop.
  • Choosing LinkedList because it sounds more advanced.

Mini Practice

Create a shopping list with ArrayList. Add five items, update one item, remove one item, and print the final count and contents.

Practice Lab: Shopping List Manager

Use ArrayList for a list whose size changes.

  1. Create an ArrayList<String> named shoppingList.
  2. Add five items.
  3. Update the second item with set.
  4. Remove one item by value or index.
  5. Print the final list and total count.

Goal: Practice dynamic list operations: add, get, set, remove, and size.

Revision Checkpoint

  • Array: Fixed size and index-based.
  • ArrayList: Dynamic size backed by an array internally.
  • add: Adds an item.
  • get/set: Read or update by index.
  • size(): Collection count, unlike array length.

Before the quiz: Decide whether fixed-size array or dynamic list is better for a changing shopping cart.

Share this article

Share on TwitterShare on LinkedInShare on FacebookShare on WhatsAppShare on Email

Test your knowledge

Take a quick quiz based on this chapter.

easyJava
Quiz: ArrayLists
12 questions5 min
Lesson 4 of 10 in 3. Core APIs & I/O
Previous in 3. Core APIs & I/O
23. Dates, Times, and Formatting
Next in 3. Core APIs & I/O
25. LinkedLists: The Alternative
Back to Java Programming: From Zero to Enterprise
Back to moduleCategories