Table of contents
1.
Introduction
2.
What is ArrayList in Java?
3.
Constructors in ArrayList
4.
Methods in ArrayList
5.
Basic Operations in ArrayList
5.1.
Add/Update elements 
5.2.
Access elements 
5.3.
Remove elements 
6.
Frequently Asked Questions
6.1.
Why is ArrayList used?
6.2.
What is the difference between an array and ArrayList?
6.3.
What is the difference between iterator and listIterator?
7.
Conclusion
Last Updated: Sep 22, 2025
Easy

ArrayList in Java

Author Yashesvinee V
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The List interface in Java defines an ordered collection of elements. The ArrayList is a resizable-array implementation of the List interface that is part of Java.util package. It implements all list operations and permits the use of null and duplicate elements. The ArrayList class provides functions/methods to manipulate the size and contents of the list.

ArrayList in Java

What is ArrayList in Java?

 

ArrayList in Java is a part of the Java Collections Framework, which comes with built-in methods such as adding, removing as well as accessing the elements by their index. It has an advantage over traditional Java arrays as such as it grows automatically as elements are added.

Constructors in ArrayList

Constructor SyntaxDescription
ArrayList arr = new ArrayList();It constructs an empty ArrayList with an initial capacity of ten.
ArrayList arr = new ArrayList(Collection c);It constructs an ArrayList containing the elements of the specified collection in the order returned by the collection's iterator.
ArrayList arr = new ArrayList(int capacity);It constructs an empty ArrayList with the specified capacity.

 

Methods in ArrayList

MethodsDescription
add(int index, E element)Inserts the given element at the specified position in the list. If index is not specified, it appends the element to the end.
addAll(int index, Collection c)Inserts all elements of the specified collection into the list, starting at the given position. If index is not specified, it appends the elements.
clear()Removes all elements of the ArrayList.
clone()Returns a shallow copy of the ArrayList instance.
contains(Object o)Returns true if the ArrayList contains the specified element.
forEach(Consumer action)Performs the given action for each element until all have been processed or an exception is thrown.
get(int index)Returns the element at the specified position in the list.
indexOf(Object o)Returns the index of a given element in the list.
isEmpty()Returns true if the ArrayList is empty.
iterator()Returns an iterator over the list elements in proper sequence.
lastIndexOf(Object o)Returns the index of the last occurrence of a given element, or -1 if not present.
listIterator()Returns a list iterator over the list elements in a proper sequence.
remove(int index)Removes the element at the given index.
removeAll(Collection c)Removes all elements also present in the given collection.
removeIf(Predicate filter)Removes elements that satisfy the given predicate.
removeRange(int from, int to)Removes all elements with indexes from fromIndex (inclusive) to toIndex (exclusive).
replaceAll(UnaryOperator operator)Replaces each element of the list with the result of applying the operator.
set(int index, E element)Replaces the element at the specified position with the given element.
size()Returns the number of elements in the list.
sort(Comparator c)Sorts the list based on the order provided by the comparator.
spliterator()Returns a spliterator for traversing and partitioning elements.
subList(int from, int to)Returns a portion of the list from fromIndex (inclusive) to toIndex (exclusive).
toArray()Returns an array of all list elements in proper sequence.
trimToSize()Trims the capacity of the ArrayList to match the current size.

Basic Operations in ArrayList

Add/Update elements
 

import java.util.ArrayList;

public class Main {
  public static void main(String[] args){
    // create ArrayList
    ArrayList<String> languages = new ArrayList<>();

    // add() method without the index parameter
    languages.add("Arabic");
    languages.add("Chinese");
    languages.add("Hindi");

    System.out.println("List: " + languages);

    languages.set(1,"English");

    languages.replaceAll(e -> e.toUpperCase());

    System.out.println("List: " + languages);
  }
}
You can also try this code with Online Java Compiler
Run Code


Output:

List: [Arabic, Chinese, Hindi]
List: [ARABIC, ENGLISH, HINDI]

 

Access elements
 

import java.util.ArrayList;

public class Main {
  public static void main(String[] args){
    ArrayList<String> languages = new ArrayList<>();

    languages.add("Arabic");
    languages.add("Malayalam");
    languages.add("Hindi");

    System.out.println("1: " + languages.get(0));
    System.out.println("2: " + languages.get(1));
    System.out.println("3: " + languages.get(2));

    languages.forEach((e) -> {
      e = e.toUpperCase();
      System.out.print(e + "\n");
    });

    System.out.println("Indian Languages: " + languages.subList(1,3));
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

1: Arabic
2: Malayalam
3: Hindi
ARABIC
MALAYALAM
HINDI
Indian Languages: [Malayalam, Hindi]


You can compile java code just by clicking here.

Remove elements
 

import java.util.ArrayList;

public class Main {
  public static void main(String[] args){
    ArrayList<String> languages = new ArrayList<>();

    languages.add("Arabic");
    languages.add("Malayalam");
    languages.add("Hindi");
    System.out.println("Languages: " + languages);
    
    System.out.println("Removing " + languages.remove(0));
    
    System.out.println("Removing Malayalam...");
    languages.removeIf(e -> e.startsWith("Mal"));
    
    System.out.println("Languages: " + languages);
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Languages: [Arabic, Malayalam, Hindi]
Removing Arabic
Removing Malayalam...
Languages: [Hindi]


Try this code by yourself on Online Java Compiler.

Frequently Asked Questions

 

Why is ArrayList used?


ArrayList is used in Java to store dynamic data, allowing fast access, easy resizing, and flexible insertion or deletion of elements.

What is the difference between an array and ArrayList?


An array is a basic data structure provided by Java that has a fixed length. In contrast, ArrayList is a class of Java Collections framework that has a variable length. An Array can store values of primitive data types while ArrayList uses Wrapper classes.


What is the difference between iterator and listIterator?


Iterator can traverse the array elements in the forward direction and can perform only remove operations. ListIterator can traverse the array of elements in backward as well as forward directions. It can perform add, remove, and set operations while traversing the collection.

Conclusion

In this article, we have discussed that ArrayList in Java is a powerful, flexible, and dynamic array-like structure that simplifies element storage, access, and manipulation. It's built-in methods make it ideal for various list operations within the Java Collections Framework

Recommended Readings:

Recommended problems -

Live masterclass