Explain ArrayList in Java with example

ArrayList in Java

An ArrayList in Java is a part of the Java Collections Framework and provides a dynamic array for storing elements. Unlike arrays in Java, which have a fixed size, an ArrayList can grow and shrink in size dynamically when elements are added or removed.

Key Features of ArrayList in Java

  • Resizable: Automatically resizes as elements are added or removed.
  • Indexed Access: Allows random access of elements using an index.
  • Non-synchronized: Not thread-safe, but can be synchronized externally.
  • Allows Duplicates: Can store duplicate elements.

Basic Operations

  • Adding elements: add()
  • Accessing elements: get()
  • Modifying elements: set()
  • Removing elements: remove()
  • Size of the list: size()

Example of Using ArrayList in Java

import java.util.ArrayList;

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

        // Adding elements to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        System.out.println("Initial ArrayList: " + fruits);

        // Accessing elements
        System.out.println("Element at index 1: " + fruits.get(1));

        // Modifying elements
        fruits.set(1, "Blueberry");
        System.out.println("ArrayList after modification: " + fruits);

        // Removing elements
        fruits.remove(2);
        System.out.println("ArrayList after removal: " + fruits);

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

Output:

Initial ArrayList: [Apple, Banana, Cherry]
Element at index 1: Banana
ArrayList after modification: [Apple, Blueberry, Cherry]
ArrayList after removal: [Apple, Blueberry]
Size of the ArrayList: 2

Explanation

Creating an ArrayList in Java:

ArrayList<String> fruits = new ArrayList<>();

This line creates an ArrayList of type String.

Adding elements:

fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");

Elements “Apple”, “Banana”, and “Cherry” are added to the ArrayList.

Accessing elements:

System.out.println("Element at index 1: " + fruits.get(1));

Retrieves the element at index 1, which is “Banana”.

Modifying elements:

fruits.set(1, "Blueberry");

Replaces the element at index 1 (“Banana”) with “Blueberry”.

Removing elements:

fruits.remove(2);

Removes the element at index 2 (“Cherry”).

Size of the ArrayList in Java:

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

Prints the size of the ArrayList, which is 2 after removal.

Summary

An ArrayList in Java is a flexible and powerful data structure for storing and manipulating a dynamically sized collection of elements. It provides various methods to perform different operations like adding, removing, and accessing elements efficiently.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *