GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java ArrayList add() Method with Examples - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java ArrayList add() Method with Examples

Last Updated : 14 Mar, 2026

The add() method in Java ArrayList is used to insert elements into the list dynamically. It allows adding elements either at the end of the list or at a specific index position.

  • Supports dynamic insertion of elements without needing to define the size in advance.
  • Provides two forms: add(E element) and add(int index, E element).
Java
import java.util.*;

public class GFG {
    public static void main(String[] args) {
      
        // Creating an empty ArrayList
        ArrayList<Integer> al = new ArrayList<>();

        // Use add() method to 
        // add elements in the list
        al.add(10);
        al.add(20);
        al.add(30);

        System.out.println("" + al);
    }
}

Output
[10, 20, 30]

Syntax of add(Object element)

public boolean add(Object element)

  • Parameters: element: The element to be appended to the list.
  • Return Type: boolean: It returns true, if the element was successfully added.

Example 2: add(int index, Object element)

This method inserts the specified element at a given position in the ArrayList. It shifts the current element at that position and subsequent elements to the right.

Syntax of add(int index, Object element)

void add(int index, Object element)

Parameters:

  • index: The position at which the specified element is to be inserted.
  • element: The element to be inserted.

Exception: Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 or index > size()).

Java
import java.util.*;

public class GFG {
    public static void main(String[] args) {
      
        // Creating an empty ArrayList
        ArrayList<Integer> al = new ArrayList<>();

        // Use add() method to 
        // add elements in the list
        al.add(10);
        al.add(20);
        al.add(30);
        al.add(40);

        System.out.println("" + al);

        // Adding new element 
        // at index 2
        int i = 2;
        al.add(i, 21);

        System.out.println("" + al);
    }
}

Output
[10, 20, 30, 40]
[10, 20, 21, 30, 40]

Note:

  • Valid Index Range: The valid range for add(index, element) is 0 ≤ index ≤ size(). If 0 ≤ index < size(), the element is inserted at that position and existing elements shift to the right; if index = size(), the element is added at the end of the list (same as add(element)).
  • Invalid Index: If the index > size() or index < 0, Java throws an IndexOutOfBoundsException because elements cannot be inserted outside the valid list boundary.
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.