ArrayList

Introduction to ArrayList

What is ArrayList?

ArrayList is a part of the Java Collections Framework. It provides a dynamic array for storing elements, which can grow and shrink in size automatically when elements are added or removed. It is found in the java.util package.

Why Use ArrayList?

  • Dynamic resizing
  • Easy to use compared to arrays
  • Allows random access to elements
  • Provides built-in methods for common operations like addition, removal, and searching

Importing ArrayList

To use ArrayList, you need to import it from the java.util package.

Java
import java.util.ArrayList;

Creating an ArrayList

Syntax

Java
ArrayList<Type> arrayList = new ArrayList<>();

Example: Creating an ArrayList

Java
import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        // Create an ArrayList of String type
        ArrayList<String> fruits = new ArrayList<>();

        // Add elements to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Print the ArrayList
        System.out.println(fruits);  // Output: [Apple, Banana, Cherry]
    }
}

Common Methods of ArrayList

  1. Adding Elements
  2. Accessing Elements
  3. Modifying Elements
  4. Removing Elements
  5. Iterating through Elements
  6. Size of the ArrayList
  7. Checking if ArrayList Contains Element
  8. Clearing the ArrayList

Adding Elements

Example: Adding Elements

Java
import java.util.ArrayList;

public class AddingElements {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        fruits.add(1, "Blueberry");  // Adding element at specific index

        System.out.println(fruits);  // Output: [Apple, Blueberry, Banana, Cherry]
    }
}

Accessing Elements

Example: Accessing Elements

Java
import java.util.ArrayList;

public class AccessingElements {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Accessing elements
        String firstFruit = fruits.get(0);  // Accessing first element
        String secondFruit = fruits.get(1);  // Accessing second element

        System.out.println("First Fruit: " + firstFruit);  // Output: First Fruit: Apple
        System.out.println("Second Fruit: " + secondFruit);  // Output: Second Fruit: Banana
    }
}

Modifying Elements

Example: Modifying Elements

Java
import java.util.ArrayList;

public class ModifyingElements {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Modifying elements
        fruits.set(1, "Blueberry");  // Changing the second element

        System.out.println(fruits);  // Output: [Apple, Blueberry, Cherry]
    }
}

Removing Elements

Example: Removing Elements

Java
import java.util.ArrayList;

public class RemovingElements {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Removing elements
        fruits.remove(1);  // Removing the second element
        fruits.remove("Cherry");  // Removing the element "Cherry"

        System.out.println(fruits);  // Output: [Apple]
    }
}

Iterating through Elements

Example: Iterating through Elements

Java
import java.util.ArrayList;

public class IteratingElements {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Iterating using for-each loop
        for (String fruit : fruits) {
            System.out.println(fruit);
        }

        // Iterating using for loop
        for (int i = 0; i < fruits.size(); i++) {
            System.out.println(fruits.get(i));
        }
    }
}

Size of the ArrayList

Example: Size of the ArrayList

Java
import java.util.ArrayList;

public class ArrayListSize {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Getting the size of the ArrayList
        int size = fruits.size();

        System.out.println("Size of the ArrayList: " + size);  // Output: Size of the ArrayList: 3
    }
}

Checking if ArrayList Contains Element

Example: Checking if ArrayList Contains Element

Java
import java.util.ArrayList;

public class ContainsElement {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Checking if ArrayList contains an element
        boolean containsApple = fruits.contains("Apple");
        boolean containsMango = fruits.contains("Mango");

        System.out.println("Contains Apple: " + containsApple);  // Output: Contains Apple: true
        System.out.println("Contains Mango: " + containsMango);  // Output: Contains Mango: false
    }
}

Clearing the ArrayList

Example: Clearing the ArrayList

Java
import java.util.ArrayList;

public class ClearingArrayList {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Clearing the ArrayList
        fruits.clear();

        System.out.println("ArrayList after clearing: " + fruits);  // Output: ArrayList after clearing: []
    }
}

Combining Methods in a Program

Example: Combined ArrayList Operations

This example combines various operations like adding, accessing, modifying, removing, and iterating elements.

Java
import java.util.ArrayList;

public class CombinedArrayListExample {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();

        // Adding elements
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        // Accessing elements
        System.out.println("First fruit: " + fruits.get(0));  // Output: First fruit: Apple

        // Modifying elements
        fruits.set(1, "Blueberry");
        System.out.println("Modified ArrayList: " + fruits);  // Output: Modified ArrayList: [Apple, Blueberry, Cherry]

        // Removing elements
        fruits.remove("Cherry");
        System.out.println("ArrayList after removal: " + fruits);  // Output: ArrayList after removal: [Apple, Blueberry]

        // Iterating elements
        System.out.println("Iterating using for-each loop:");
        for (String fruit : fruits) {
            System.out.println(fruit);
        }

        // Checking size
        System.out.println("Size of the ArrayList: " + fruits.size());  // Output: Size of the ArrayList: 2

        // Checking if ArrayList contains an element
        boolean containsApple = fruits.contains("Apple");
        System.out.println("Contains Apple: " + containsApple);  // Output: Contains Apple: true

        // Clearing the ArrayList
        fruits.clear();
        System.out.println("ArrayList after clearing: " + fruits);  // Output: ArrayList after clearing: []
    }
}

Conclusion

ArrayList in Java is a versatile and easy-to-use class that provides dynamic array functionality. It offers methods for adding, accessing, modifying, removing, and iterating through elements. Understanding and utilizing these methods effectively can help you manage collections of data efficiently in your Java programs. Practice these operations to become proficient in using ArrayList.

Scroll to Top