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:
add(E element)
: Appends the element to the end of theArrayList
.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
Parameter | Description |
---|---|
index | (Optional) The position at which to insert the element. Must be between 0 and size() . |
element | The element to be added to the list. |
Return Value
Method Version | Return |
---|---|
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>
orArrayList<Integer>
.
Summary
Aspect | Details |
---|---|
Method Purpose | Add elements to an ArrayList |
Overloaded Variants | add(E element) and add(int index, E element) |
Adds at the end or specific position | End if no index; specific index if given |
Return Value | boolean (append), void (insert) |
Exception | IndexOutOfBoundsException if invalid index |
Typical Use Cases | Dynamic, ordered data storage and manipulation |