Java Lesson 33 – ArrayList | Dataplexa

ArrayList

An ArrayList is one of the most commonly used classes in Java. It is part of the Collections Framework and is used to store a dynamic list of elements.

Unlike arrays, an ArrayList can grow and shrink automatically as data is added or removed. This makes it very practical for real-world applications.


Why ArrayList is Important

In real applications, we rarely know the exact number of elements in advance. Using fixed-size arrays can become inefficient and difficult to manage.

ArrayList solves this problem by handling memory internally and resizing itself when needed.


Key Characteristics of ArrayList

  • Maintains insertion order
  • Allows duplicate elements
  • Provides fast access using index
  • Not synchronized by default

Because of these features, ArrayList is widely used in business applications, APIs, and backend systems.


Creating an ArrayList

An ArrayList is created using the ArrayList class. It usually works with the List interface.


import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {

        List names = new ArrayList<>();

        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");

        System.out.println(names);
    }
}

This approach keeps the code flexible and easy to maintain.


Accessing Elements

ArrayList allows index-based access, similar to arrays. Indexes start from 0.


System.out.println(names.get(0)); // Alice
System.out.println(names.get(2)); // Charlie

This makes ArrayList very efficient when reading data frequently.


Updating and Removing Elements

Elements can be updated or removed easily using built-in methods.


names.set(1, "David");     // Update element
names.remove("Alice");    // Remove by value
names.remove(0);          // Remove by index

System.out.println(names);

ArrayList automatically adjusts its size after removal.


Real-World Example

Imagine a shopping cart in an e-commerce application. Products are added and removed dynamically.


List cart = new ArrayList<>();

cart.add("Laptop");
cart.add("Mouse");
cart.add("Keyboard");

cart.remove("Mouse");

System.out.println("Cart items: " + cart);

This is a perfect use case for ArrayList.


Performance Considerations

ArrayList provides fast read operations, but inserting or removing elements in the middle can be slower because elements need to be shifted.

If frequent insertions and deletions are required, other collections like LinkedList may be more suitable.


Common Mistakes to Avoid

  • Using ArrayList when uniqueness is required
  • Modifying ArrayList while iterating without care
  • Using raw types instead of generics

Key Takeaways

  • ArrayList is a resizable array implementation
  • It maintains order and allows duplicates
  • It is ideal for dynamic lists
  • It is widely used in real-world Java applications

In the next lesson, we will explore LinkedList and understand how it differs from ArrayList internally.