Introduction
The add()
method in Java's ArrayList
is your primary tool for adding elements to the list. It comes in two forms: one that adds an element at the end of the list, and another that inserts an element at a specific index.
Syntax
public boolean add(E e)
public void add(int index, E element)
Parameters Table
Parameter | Description |
---|---|
e (for add(E e) ) |
The element to be added to the end of the list. |
index (for add(int index, E element) ) |
The index at which the specified element is to be inserted. Index must be between 0 and size() , inclusive. |
element (for add(int index, E element) ) |
The element to be added at the specified index. |
Return Value
The first version of add()
(add(E e)
) returns a boolean value: true
if the addition was successful, and false
if it failed. This is helpful in situations where you want to know definitively whether an element was added.
The second version of add()
(add(int index, E element)
) returns nothing (void). It modifies the ArrayList directly.
Examples
Adding to the End of an ArrayList
This example demonstrates how to add elements to the end of a Java ArrayList
using the first version of the add()
method. It shows adding both strings and integers.
import java.util.ArrayList;
public class AddToEndExample {
public static void main(String[] args) {
ArrayList<Object> myList = new ArrayList<>(); //Using Object to hold different types initially
// Adding strings
myList.add("Hello");
myList.add("World");
System.out.println(myList); // Prints the list after adding two strings
// Adding an integer
boolean success = myList.add(123);
System.out.println("Addition successful: " + success); //Prints whether addition was successful
System.out.println(myList); // Prints the list after adding an integer
}
}
[Hello, World]
Addition successful: true
[Hello, World, 123]
In this code, we create an ArrayList and add two strings “Hello” and “World”, then add the integer 123. The output shows that each addition was successful.
Inserting at a Specific Index
This example demonstrates how to insert elements at specific positions within an ArrayList
using the second version of the add()
method. It also illustrates what happens if you attempt to insert at an invalid index.
import java.util.ArrayList;
public class AddAtIndexExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
System.out.println("Initial list: " + names);
// Insert 'Charlie' at index 1
names.add(1, "Charlie");
System.out.println("List after inserting Charlie at index 1: " + names);
// Attempt to insert at an invalid index (beyond the size of the list)
try {
names.add(5, "Ram"); //This will throw IndexOutOfBoundsException if uncommented.
System.out.println("List after attempting to insert Ram at index 5: " + names);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: Invalid index! " + e.getMessage());
}
}
}
Initial list: [Alice, Bob]
List after inserting Charlie at index 1: [Alice, Charlie, Bob]
Error: Invalid index! Index: 5, Size: 3
Here, we create an ArrayList of strings. We add 'Charlie' at index 1, shifting 'Bob' to index 2. Attempting to insert ‘Ram’ at index 5 results in an `IndexOutOfBoundsException` because the valid range for insertion is from 0 to 3 (inclusive).
Adding Generic Types
This example shows how to add elements of a specific generic type to ArrayList
import java.util.ArrayList;
public class AddGenericTypes {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
System.out.println("Numbers list:"+ numbers);
}
}
Numbers list:[10, 20, 30]
In this example we create an ArrayList of Integer type and add three integers to it.