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.

Feb 22, 20264 views0 likes0 fires
18px

[!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);
}

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

Continue Learning

25. LinkedLists: The Alternative

Intermediate
11 min

26. HashMaps: Key-Value Architecture

Intermediate
14 min

27. HashSets: The Art of Uniqueness

Intermediate
7 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 Java Programming: From Zero to EnterpriseAll Categories