In this section we will learn what the ArrayList add() and addAll() methods are and how to use them in Java.
What is Java ArrayList add() Method?
The Java List add() method is used to add a new element to an ArrayList object.
When using this method, each new value will be added to the end of target ArrayList object.
For example, if you have an ArrayList object with the values 1,2,3 and now using this method to add the value 4, then after running the method, the value 4 will be added to the list and you’ll get:
1,2,3,4
Java ArrayList add() Method Syntax:
boolean add(E e)
add() Method Parameters:
The method takes one argument and that is the element we want to append to the target list.
add() Method Return Value:
The return value of this method is of Boolean type.
If we use the method to append an element and the operation was successful, then the result of this method will be true. Otherwise you’ll get the value false, which means a failed operation.
add() Method Exceptions:
This method might throw any of the exceptions below:
UnsupportedOperationException – if the add operation is not supported by the target list object.
ClassCastException – This exception will be thrown if you try to add an element of type other than the data type of the target list object.
NullPointerException – If you attempt to add null value to a list object, you’ll get this exception.
IllegalArgumentException – If a property of the element that you’re adding to a list is preventing it from this operation, you’ll get this exception.
Example: using ArrayList add() method
import java.util.ArrayList; public class Main { public static void main(String[] args){ ArrayList<String> list = new ArrayList<>(); list.add(“One”); list.add(“Two”); list.add(“Three”); list.add(“Four”); list.add(“Five”); list.add(“Six”); } }
In this example, we’ve created a list object and then added 6 String values via the add() method.
What is Java ArrayList addAll() Method?
The Java List addAll() method is used to add the elements of a collection (an object that inherits from the Collection interface) into a list object.
Basically, this method is a way of copying the entire elements of one list into another.
Java ArrayList addAll () Method Syntax:
boolean addAll(Collection<? extends E> c)
addAll () Method Parameters:
The method takes one argument, and that is a reference to the list object (an object that inherits from the Collection interface) which we want to copy all its elements and bring it into the current list object.
addAll () Method Return Value:
The return value of this method is of Boolean type.
For a successful operation, the result of this method is true, otherwise false.
addAll () Method Exceptions:
All the exceptions that you see for the add() method could also be thrown for this method.
Example: using ArrayList addAll () method
import java.util.ArrayList; public class Main { public static void main(String[] args){ ArrayList<String> list = new ArrayList<>(); list.add(“One”); list.add(“Two”); list.add(“Three”); list.add(“Four”); list.add(“Five”); list.add(“Six”); ArrayList<String> list2 = new ArrayList<>(); list2.addAll(list); for(int i: list2){ System.out.println(i); } } }
Output:
One Two Three Four Five
In this example, we have 2 lists. The first one has already 6 elements in its body. Now by using the addAll() method, we could copy the elements of the first list and put it in the body of the second list object.