Home page

Thursday, June 22, 2023

ArrayList in JAVA

In Java, ArrayList is a class that implements the List interface and provides a resizable array-based implementation of a dynamic array. It allows you to store and manipulate elements in a flexible manner. Here's when and why you would use an ArrayList:

Dynamic size: Unlike regular arrays, ArrayList provides dynamic resizing, allowing you to add or remove elements at runtime without worrying about the fixed size of the array. It automatically adjusts its size as elements are added or removed.

Random access: ArrayList supports random access to elements, meaning you can retrieve elements by their index. This allows for efficient and fast retrieval of elements at a specific position.

Collection manipulation: ArrayList provides various methods for manipulating the collection, such as adding elements, removing elements, replacing elements, and checking for the presence of elements. It also supports iterating over the elements using enhanced for loops or iterators.

Compatibility with other APIs: ArrayList is widely used and is compatible with many Java APIs and frameworks. It is often used as a common data structure to exchange and manipulate data between different parts of an application.

Here's an example of how to use ArrayList in Java:

import java.util.ArrayList;

import java.util.List;

public class ArrayListExample {

    public static void main(String[] args) {

        // Create an ArrayList

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

        // Add elements to the ArrayList

        fruits.add("Apple");

        fruits.add("Banana");

        fruits.add("Orange");

        // Access elements by index

        String firstFruit = fruits.get(0);

        System.out.println("First fruit: " + firstFruit); // Output: Apple

        // Iterate over the ArrayList

        for (String fruit : fruits) {

            System.out.println(fruit);

        }

        // Output: Apple, Banana, Orange

        // Remove an element

        fruits.remove("Banana");

        // Check if an element exists

        boolean containsOrange = fruits.contains("Orange");

        System.out.println("Contains Orange? " + containsOrange); // Output: true

        // Get the size of the ArrayList

        int size = fruits.size();

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

    }

}

In this example, we create an ArrayList called fruits that stores String elements.

We add elements to the ArrayList using the add() method.

We retrieve an element by its index using the get() method.

We iterate over the ArrayList using a for-each loop and print the elements.

We remove an element using the remove() method.

We check if an element exists using the contains() method.

We get the size of the ArrayList using the size() method.

Output:
First fruit: Apple
Apple
Banana
Orange
Contains Orange? true
Size of ArrayList: 2

As shown in the example, ArrayList provides a flexible and convenient way to store and manipulate elements in Java. It offers dynamic resizing, random access to elements, and various methods for element manipulation, making it a commonly used data structure in Java programming.

No comments:

Post a Comment