ArrayList in Java with example

ArrayList in Java
ArrayList in Java

ArrayList is a part of the Collection framework and it is present in java. util package.

It provides dynamic arrays in Java.

The similarity between the array list and arrays is both are sequential.

For searching and sorting ArrayList is faster than LinkedList.

For insertion and deletion, LinkedList is faster than ArrayList

ArrayList required the number of shifting operation so it is slower.

The difference between Array and ArrayList is Array is static and ArrayList is dynamic.

The similarity is both are sequential.

ArrayList inherits the AbstractList class.

ArrayList allows duplicate and null values.

ArrayList is an ordered collection.

It is also called a dynamic array.


import java.util.ArrayList;
import java.util.List;

public class CreateArrayList {

    public static void main(String[] args) {
        // Creating an ArrayList of String
        List<String> animal = new ArrayList<>();

        // Adding new elements to the ArrayList


        // Adding an element at a particular index in an ArrayList
        animals.add(2, "E");


# Output
[A, B, C, D]
[A, B, E, C, D]

Leave a comment

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