ArrayList in Java with example

ArrayList in Java
ArrayList in Java

ArrayList is a part of 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

As 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 AbstractList class.

ArrayList allows duplicate and null values.

ArrayList is an ordered collection.

It is also called a dynamic array.

Program


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
        animals.add("A");
        animals.add("B");
        animals.add("C");
        animals.add("D");

        System.out.println(animal);

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

        System.out.println(animal);

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

basicittopic

A Computer Science Study for IT students and people of IT community

Leave a Reply