Java ArrayList

An ArrayList in Java is a resizable array that belongs to the java.util package. It is part of the Collection Framework and provides a way to store and manage dynamic data. Unlike arrays, the ArrayList can grow or shrink in size as elements are added or removed.

Characteristic of ArrayList

1. Dynamic sizing: Unlike arrays, an ArrayList can grow or shrink dynamically as you add or remove elements.

2. Allows duplicates: ArrayList allows storing duplicate elements.

3. Ordered: It maintains the insertion order of elements.

4. Indexed access: You can access elements by their index position.

How to import the ArrayList?

Since ArrayList is part of the java.util package, you need to import it first:


import java.util.ArrayList; // Import the ArrayList class

Create a Array List

You create an ArrayList by using the ArrayList class, specifying the type of elements it will hold (e.g., String, Integer).


// Creating an ArrayList to store Strings
ArrayList fruits = new ArrayList<>();

Here, fruits is an ArrayList that will store String objects. Note that we are using Generics (String), which means this list can only hold String elements.

Add Elements to the ArrayList

You can add elements to the ArrayList using the add() method.


fruits.add("Apple");   // Adds "Apple" to the list
fruits.add("Banana");  // Adds "Banana" to the list
fruits.add("Mango");  // Adds "Mango" to the list
fruits.add("Orange");  // Adds "Orange" to the list

Access Elements from the ArrayList

You can retrieve an element by its index using the get() method. The index is zero-based, meaning the first element is at index 0.


System.out.println(fruits.get(0)); // Output: Apple
System.out.println(fruits.get(1)); // Output: Banana
System.out.println(fruits.get(2)); // Output: Mango
System.out.println(fruits.get(3)); // Output: Orange

Get the Size of the ArrayList

You can get the size of the ArrayList (i.e., the number of elements it contains) using the size() method.


System.out.println("Size of the list: " + fruits.size());  // Output: 4

Modify Elements in the ArrayList

You can modify elements at a specific index using the set() method.


fruits.set(1, "Orange");  // Change "Banana" at index 1 to "Orange"
System.out.println(fruits.get(1)); // Output: Orange

Remove Elements from the ArrayList

You can remove elements using the remove() method. You can remove by index or by object.


fruits.remove(3);  // Removes the element at index 3 ("Orange")
System.out.println(fruits);  // Output: [Apple, Orange, Mango]

You can also remove an element by its value:


fruits.remove("Apple");  // Removes the element "Apple"
System.out.println(fruits);  // Output: [Orange, Mango]

Check if an Element Exists in the ArrayList

Use the contains() method to check if a specific element exists in the list.


System.out.println(fruits.contains("Orange"));  // Output: true
System.out.println(fruits.contains("Apple"));  // Output: false

Clear All Elements from the ArrayList

You can remove all elements from the ArrayList using the clear() method.


fruits.clear();  // Clears the entire list
System.out.println(fruits);  // Output: []

Complete Example:


//Main.java
import java.util.ArrayList;  // Importing ArrayList class

public class Main {
    public static void main(String[] args) {
        // Step 1: Create an ArrayList to store Strings
        ArrayList fruits = new ArrayList<>();
        
        // Step 2: Add elements to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Mango");
        fruits.add("Orange");

        // Step 3: Access elements from the ArrayList
        System.out.println("First fruit: " + fruits.get(0));  // Output: Apple
        System.out.println("Second fruit: " + fruits.get(1)); // Output: Banana
        System.out.println("Third fruit: " + fruits.get(2));  // Output: Mango
        System.out.println("Third fruit: " + fruits.get(3));  // Output: Orange

        // Step 4: Get the size of the ArrayList
        System.out.println("Size of the list: " + fruits.size());  // Output: 4

        // Step 5: Modify an element in the ArrayList
        fruits.set(1, "Orange");
        System.out.println("Modified second fruit: " + fruits.get(1));  // Output: Orange

        // Step 6: Remove an element from the ArrayList by index
        fruits.remove(3);  // Removes "Orange"
        System.out.println("After removing fruits: " + fruits);  // Output: [Apple,Orange, Mango]

        // Step 7: Remove an element by value
        fruits.remove("Apple");  // Removes "Apple"
        System.out.println("After removing Apple: " + fruits);  // Output: [Orange, Mango]

        // Step 8: Check if an element exists
        System.out.println("Contains Orange? " + fruits.contains("Orange"));  // Output: true
        System.out.println("Contains Apple? " + fruits.contains("Apple"));  // Output: false

        // Step 9: Clear all elements
        fruits.clear();
        System.out.println("After clearing the list: " + fruits);  // Output: []
    }
}