Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java ArrayList Explained: Usage, Key Methods, and Practical Examples

What is ArrayList in Java?

ArrayList is a dynamic array that automatically expands and contracts as elements are added or removed. It’s essential when you need a flexible collection that can grow or shrink during runtime.

Unlike a fixed‑size array (think of a rigid rope), an ArrayList behaves like a stretchable rubber band, allowing you to store more items without having to predefine a maximum size.

Java ArrayList Explained: Usage, Key Methods, and Practical Examples

When you add elements, ArrayList grows automatically; when elements are removed, it shrinks, ensuring efficient memory usage.

ArrayList Methods

Java ArrayList Example

Below is a concise demonstration of the most common operations on an ArrayList.

import java.util.ArrayList;

public class TestArrayList {
    public static void main(String[] args) {
        // Create a generic ArrayList
        ArrayList<String> arlTest = new ArrayList<>();

        // Initial size
        System.out.println("Size at creation: " + arlTest.size());

        // Add elements
        arlTest.add("D");
        arlTest.add("U");
        arlTest.add("K");
        arlTest.add("E");

        // Size after additions
        System.out.println("Size after adding: " + arlTest.size());
        System.out.println("Elements: " + arlTest);

        // Remove by value
        arlTest.remove("D");
        System.out.println("After removing " + "D": " + arlTest);

        // Remove by index
        arlTest.remove(2); // removes "K"
        System.out.println("After removing index 2: " + arlTest);

        // Final size
        System.out.println("Final size: " + arlTest.size());
        System.out.println("Final list: " + arlTest);

        // Contains check
        System.out.println("Contains " + "K" + ": " + arlTest.contains("K"));
    }
}

Output:

Size at creation: 0
Size after adding: 4
Elements: [D, U, K, E]
After removing D: [U, K, E]
After removing index 2: [U, K]
Final size: 2
Final list: [U, K]
Contains K: true

Note: While the example uses single characters, ArrayList can hold any object type—strings, integers, custom classes, and more.

Java ArrayList Explained: Usage, Key Methods, and Practical Examples

Java

  1. Java Methods: How to Define, Call, and Use Them Effectively
  2. Mastering Java ArrayList: Operations, Methods, and Best Practices
  3. Mastering Java Collection Algorithms: Sorting, Shuffling, and More
  4. Mastering C# Queues: Enqueue, Dequeue, and First‑In‑First‑Out Operations Explained
  5. Java ArrayList Explained: Usage, Key Methods, and Practical Examples
  6. Mastering Java's String compareTo() Method: Syntax, Use Cases, and Practical Examples
  7. Mastering the 'this' Keyword in Java: Purpose, Usage, and Practical Examples
  8. Converting a Char to a String in Java – Practical Examples and Best Practices
  9. Mastering Java Methods: Create, Invoke, and Abstraction
  10. Java 8 Default Methods Explained: Enhancing Interfaces & Backward Compatibility