Adding Elements to an ArrayList in Java
This guide walks through the different ways you can insert elements into a Java ArrayList. The add() and addAll() methods are both overloaded, giving you four distinct approaches for adding single items or entire collections, either at the end of the list or at a chosen position.
- add(E e) — append a single element
- add(int index, E e) — insert a single element at a position
- addAll(Collection c) — append a whole collection
- addAll(int index, Collection c) — insert a whole collection at a position
Java ArrayList add(E e)
The add() method appends the supplied element to the end of the ArrayList. It returns a boolean: true when the element is added successfully, otherwise false.
Signature: boolean add(E e); — here E is the element type held by the list, and e is the element you want to add. The return type is boolean.
import java.util.ArrayList;
public class ArraylistExample {
public static void main(String[] args) {
ArrayList<String> listOfNames = new ArrayList<String>();
listOfNames.add(\"JAVA\");
listOfNames.add(\"GOAL\");
listOfNames.add(\"Website\");
// Adding Same element again
boolean isSuccessFullyAdded = listOfNames.add(\"GOAL\");
System.out.println(\"Element added again = \" + isSuccessFullyAdded);
for (String name : listOfNames) {
System.out.println(\"Names from list = \" + name);
}
}
}Output:
Element added again = true
Names from list = JAVA
Names from list = GOAL
Names from list = Website
Names from list = GOALThis output shows why duplicates can appear in an ArrayList. Every element occupies its own index, so the list never checks whether a value already exists before adding it.
add(int index, E e)
Use this overload when you need to place an element at a specific position in the ArrayList. For example, if the list already holds four elements and you want a new one at position 2, this method handles it. Any element currently at that position is shifted along.
Signature: boolean add(int index, E e); — index is the position where the element should be inserted, E is the element type, and e is the element to add. The return type is void. An IndexOutOfBoundsException is thrown if the index is invalid.
import java.util.ArrayList;
public class ArraylistExample {
public static void main(String[] args) {
ArrayList<String> listOfNames = new ArrayList<String>();
listOfNames.add(\"JAVA\");
listOfNames.add(\"GOAL\");
listOfNames.add(\"Website\");
// Adding element at specified position
listOfNames.add(1, \"Learning\");
for (String name : listOfNames) {
System.out.println(\"Names from list = \" + name);
}
}
}Output:
Names from list = JAVA
Names from list = Learning
Names from list = GOAL
Names from list = WebsiteArrayList addAll(Collection c)
The addAll() method appends every element of the given collection to the end of the ArrayList. It returns a boolean — true once the collection has been added. If the supplied collection is null, a NullPointerException is thrown.
Signature: boolean addAll(Collection c); — c is the collection whose elements should be added. A NullPointerException is thrown when c is null. The return type is boolean.
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> listOfNames = new ArrayList<String>();
listOfNames.add(\"JAVA\");
listOfNames.add(\"GOAL\");
listOfNames.add(\"Website\");
ArrayList<String> listOfNames2 = new ArrayList<String>();
listOfNames2.add(\"Learning\");
listOfNames2.add(\"Concepts\");
listOfNames.addAll(listOfNames2);
for (String name : listOfNames) {
System.out.println(\"Names from list = \" + name);
}
}
}Output:
Names from list = JAVA
Names from list = GOAL
Names from list = Website
Names from list = Learning
Names from list = ConceptsArrayList addAll(int index, Collection c)
This overload inserts all elements of the specified collection starting at a chosen position in the ArrayList. Elements already sitting at that position and beyond are shifted to make room. The return type is boolean.
Signature: boolean addAll(int index, Collection c); — index is the position where insertion begins, and c is the collection of elements to add. A NullPointerException is thrown if c is null. The return type is boolean.
package package1;
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> listOfNames = new ArrayList<String>();
listOfNames.add(\"JAVA\");
listOfNames.add(\"GOAL\");
listOfNames.add(\"Website\");
ArrayList<String> listOfNames2 = new ArrayList<String>();
listOfNames2.add(\"Learning\");
listOfNames2.add(\"Concepts\");
listOfNames.addAll(1, listOfNames2);
for (String name : listOfNames) {
System.out.println(\"Names from list = \" + name);
}
}
}Output:
Names from list = JAVA
Names from list = Learning
Names from list = Concepts
Names from list = GOAL
Names from list = WebsiteJavaGoal
Back to topLeave a Comment