Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Java ArrayList add() Method

Posted on July 22, 2025July 22, 2025 By Admin No Comments on Java ArrayList add() Method

The add() method is one of the most commonly used methods in the Java ArrayList class. It enables you to add elements dynamically to an ArrayList, either at the end or at a specified position, making it a powerful and flexible way to manage collections of data.

What Is the add() Method in ArrayList?

  • The add() method inserts an element into the list.
  • It has two overloaded versions:
    1. add(E element): Appends the element to the end of the ArrayList.
    2. add(int index, E element): Inserts the element at the specified index, shifting subsequent elements to the right.

Where, E represents the data type of the elements stored in the ArrayList.

Syntax

// Add element at the end of the list
public boolean add(E element)

// Add element at specified index
public void add(int index, E element)

Parameters

ParameterDescription
index(Optional) The position at which to insert the element. Must be between 0 and size().
elementThe element to be added to the list.

Return Value

Method VersionReturn
add(E element)Returns true if the list was modified successfully.
add(int index, E element)Returns void (does not return a value).

Exceptions

  • Throws IndexOutOfBoundsException if the provided index is less than 0 or greater than the current size of the list.
  • Throws NullPointerException if the list does not support null elements and a null element is added (depending on implementation).
  • Other exceptions like UnsupportedOperationException can be thrown if the list implementation does not support adding elements.

How Does add() Work Internally?

  • Before adding, the underlying array backing the ArrayList ensures it has sufficient capacity.
  • If needed, the internal array resizes dynamically to accommodate new elements.
  • When adding at a specific index, existing elements from that position onwards are shifted one step to the right.

Examples of the add() Method

1. Add Elements at the End of the List

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    System.out.println(cars);
  }
}

Output:

[Volvo, BMW, Ford, Mazda]

2. Add Element at a Specific Position

Inserting an element at index 2 shifts elements starting from position 2 to the right.

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> cars = new ArrayList<>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");

    cars.add(2, "Toyota");  // Insert "Toyota" at index 2

    System.out.println(cars);
  }
}

Output:

[Volvo, BMW, Toyota, Ford, Mazda]

3. Add Element at the Beginning of ArrayList

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> fruits = new ArrayList<>();
    fruits.add("Apple");
    fruits.add("Banana");
    fruits.add("Cherry");

    fruits.add(0, "Mango"); // Insert at index 0 (beginning)

    System.out.println(fruits);
  }
}

Output:

[Mango, Apple, Banana, Cherry]

Important Notes

  • The add(E element) method always appends elements at the end, maintaining the order.
  • The add(int index, E element) inserts at the specified index and shifts elements to keep the list consistent.
  • If you try to add an element at an invalid index (less than 0 or greater than current size), an IndexOutOfBoundsException will be thrown.
  • The method returns true for successful append operations but returns void when inserting at a specific index.
  • Use generics to ensure type safety, for example ArrayList<String> or ArrayList<Integer>.

Summary

AspectDetails
Method PurposeAdd elements to an ArrayList
Overloaded Variantsadd(E element) and add(int index, E element)
Adds at the end or specific positionEnd if no index; specific index if given
Return Valueboolean (append), void (insert)
ExceptionIndexOutOfBoundsException if invalid index
Typical Use CasesDynamic, ordered data storage and manipulation
Java Tags:Java-ArrayList, Java-ArrayList-Method, Java-Collections

Post navigation

Previous Post: Java ArrayList Methods
Next Post: Java ArrayList addAll() Method

Leave a Reply Cancel reply

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

  • Java ArrayList
  • Java ArrayList Methods
  • Java ArrayList add() Method
  • Java ArrayList addAll() Method
  • Java ArrayList clear() Method
  • Java ArrayList clone() Method
  • Java ArrayList contains() Method
  • Java ArrayList ensureCapacity() Method
  • Java ArrayList forEach() Method
  • Java ArrayList get() Method
  • Java ArrayList indexOf() Method
  • Java ArrayList isEmpty() Method
  • Java ArrayList iterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList listIterator() Method
  • Java ArrayList remove() Method
  • Java ArrayList removeAll() Method
  • Java ArrayList removeIf() Method
  • Java ArrayList replaceAll() Method
  • Java ArrayList retainAll() Method
  • Java ArrayList set() Method
  • Java ArrayList size() Method
  • Java ArrayList sort() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList subList() Method
  • Java ArrayList toArray() Method
  • Java ArrayList trimToSize() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme