Open In App

ArrayList in Java

Last Updated : 06 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, an ArrayList is a resizable array implementation that is part of the java.util package. Unlike regular arrays, you don’t need to specify its size in advance; it can grow or shrink dynamically as elements are added or removed.

ArrayList Java

Key Features

  • Resizable Array: ArrayList can automatically grow dynamically in size.
  • Indexed Access: ArrayList elements can be accessed using indices like arrays.
  • Supports Generics: It ensures type safety at compile-time.
  • Not Synchronized: ArrayList uses Collections.synchronizedList() for thread safety.
  • Allows Null and Duplicates: ArrayList allows both null values and duplicate elements.
  • Maintains Insertion Order: Elements are stored in the order they are added.

Example: Java Program to demonstrate ArrayList


Output
[1, 2, 3]

Syntax:

ArrayList<Integer> arr = new ArrayList<Integer>();

Note: You can also create a generic ArrayList

Constructors of ArrayList in Java

In order to Create an ArrayList, we need to create an object of the ArrayList class. The ArrayList class consists of various constructors which allow the possible creation of the array list. The following are the constructors available in this class:

1. ArrayList(): This constructor is used to build an empty array list.

ArrayList<Integer> arr = new ArrayList<>();

2. ArrayList(Collection c): This constructor is used to build an array list initialized with the elements from the collection c.

ArrayList<String> arr = new ArrayList<>(collection);

3. ArrayList(int capacity): This constructor is used to build an array list with the initial capacity being specified.

ArrayList<Double> arr = new ArrayList<>(20);

Operations of ArrayList

Now, Using the constructors we have got ArrayList for further operations like Insertion,Deletion and Updation of the elements in ArrayList.

Example: Java Program Example to Demonstrate Addition, Deletion and Updation of Element


Output
Original List : [Geeks, Geeks]
After Adding element at index 1 : [Geeks, For, Geeks]
Element removed from index 0 : [For, Geeks]
Element Geeks removed : [For]
List after updation of value : [GFG]

Let us understand how the three operations performed in above Program works.

Advantages of Java ArrayList

  • Dynamic size: ArrayList can dynamically grow and shrink in size, making it easy to add or remove elements as needed.
  • Easy to use: ArrayList is simple to use, making it a popular choice for many Java developers.
  • Fast access: ArrayList provides fast access to elements, as it is implemented as an array under the hood.
  • Ordered collection: ArrayList preserves the order of elements, allowing you to access elements in the order they were added.
  • Supports null values: ArrayList can store null values, making it useful in cases where the absence of a value needs to be represented.

Java ArrayList Methods

MethodDescription
add(int index, Object element)This method is used to insert a specific element at a specific position index in a list.
add(Object o)This method is used to append a specific element to the end of a list.
addAll(Collection C)This method is used to append all the elements from a specific collection to the end of the mentioned list, in such an order that the values are returned by the specified collection’s iterator.
addAll(int index, Collection C)Used to insert all of the elements starting at the specified position from a specific collection into the mentioned list.
clear()This method is used to remove all the elements from any list.
clone()This method is used to return a shallow copy of an ArrayList in Java.
contains(Object o)Returns true if this list contains the specified element.
ensureCapacity(int minCapacity)Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
forEach(Consumer<? super E> action)Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
get(int index)Returns the element at the specified position in this list.
indexOf(Object O)The index the first occurrence of a specific element is either returned or -1 in case the element is not in the list.
isEmpty()Returns true if this list contains no elements.
lastIndexOf(Object O)The index of the last occurrence of a specific element is either returned or -1 in case the element is not in the list.
listIterator()Returns a list iterator over the elements in this list (in proper sequence).
listIterator(int index)Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
remove(int index)Removes the element at the specified position in this list.
remove(Object o)Removes the first occurrence of the specified element from this list, if it is present.
removeAll(Collection c)Removes from this list all of its elements that are contained in the specified collection.
removeIf(Predicate filter)Removes all of the elements of this collection that satisfy the given predicate.
removeRange(int fromIndex, int toIndex)Removes from this list all of the elements whose index is between from Index, inclusive and to Index, exclusive.
retainAll(Collection<?> c)Retains only the elements in this list that are contained in the specified collection.
set(int index, E element)Replaces the element at the specified position in this list with the specified element.
size()Returns the number of elements in this list.
spliterator()Creates a late-binding and fail-fast Spliterator over the elements in this list.
subList(int fromIndex, int toIndex)Returns a view of the portion of this list between the specified fromIndex, inclusive and toIndex, exclusive.
toArray()This method is used to return an array containing all of the elements in the list in the correct order.
toArray(Object[] O)It is also used to return an array containing all of the elements in this list in the correct order same as the previous method.
trimToSize()This method is used to trim the capacity of the instance of the ArrayList to the list's current size.

Complexity of Java ArrayList

Operation

Time Complexity

Space Complexity

Inserting Element in ArrayList

O(1)

O(N)

Removing Element from ArrayList

O(N)

O(1)

Traversing Elements in ArrayList

O(N)

O(N)

Replacing Elements in ArrayList

O(1)

O(1)


ArrayList in Java
Visit Course explore course icon

Similar Reads