Introduction to addAll() in Java
In Java ArrayList, there is a method addAll() that helps in appending every element available in argument collection to the list that is present in the end. By using the iterator of argument collection, the appended elements will be ordered. In addition to that, this method first makes sure that there is enough space in the list. If enough space is not available, it will be grown by adding spaces in the existing array. After this only, elements will be added to the end of the list. Even though it is possible to add any type of element in the array list, it is a best practice to add elements of a particular type available in the given instance.
Syntax with parameters:
Below is the syntax of addAll() method:
addAll(int index, Collection<? extends E> c)
Parameters:
1. index: Index where the first element has to be inserted in the collection mentioned.
2. c: Collection that contains the elements that has to be added in the list.
3. Return Value: True will be returned if the particular list has changed on calling this method.
4. Exception: There may occur two types of exceptions such as:
- NullPointerException occurs when the collection mentioned is null.
- IndexOutOfBoundsException which occurs when the index is out of range.
How does addAll() method work in Java?
addAll() method appends the elements in the end of the array list. If a new element comes, it will check whether there is space for that element. If no space, then arraylist will add spaces. Once the spaces are added, the element will be added to its end.
4.8 (8,018 ratings)
View Course
Examples of addAll() in Java
Given below are the examples of addAll() in Java:
Example #1: addAll(Collection c)
This method helps in adding elements of the collection mentioned to an arraylist.
Code:
import java.util.*;
public class AddAllExample
{
public static void main(String[] args)
{
ArrayList<String> A1 = new ArrayList<>(); //Array list 1
//add elements to arraylist 1
A1.add("Anna Sam");
A1.add("Izanorah Denan");
A1.add("Adam Sam");
A1.add("Annamu S");
A1.add("Naasy D");
A1.add("Thukidi D");
A1.add("Kuffi D");
A1.add("Samcha T");
ArrayList<String> A2 = new ArrayList<>(); //Array list 2
//add element to arraylist 2
A2.add("Anabeth Denan");
//Combine the arraylist 1 and arraylist 2
A1.addAll(A2);
//print the combined list
System.out.println(A1);
}
}
Output:
Explanation:
Create an arraylist A1 and add elements. Similarly, create an arraylist A2 and add elements to it as well. Then, add elements of A2 to A1 and print the arraylist A1.
Example #2: addAll (int fromIndex, Collection c)
Unlike the previous method, this method is an overloaded variant of the same. That is, an argument ‘fromIndex’ is additionally added here which inserts the beginning element from the collection mentioned. Normally, the starting index is ‘0’.
Code:
import java.util.*;
public class AddAllExample
{
public static void main(String[] args)
{
ArrayList<String> A1 = new ArrayList<>(); //Array list 1
//add elements to arraylist 1
A1.add("Izanorah Denan");
A1.add("Adam Sam");
A1.add("Annamu S");
A1.add("Naasy D");
A1.add("Thukidi D");
A1.add("Princy v");
A1.add("Kuffi D");
A1.add("Samcha T");
ArrayList<String> A2 = new ArrayList<>(); //Array list 2
//add element to arraylist 2
A2.add("Anabeth Denan");
//Combine the arraylist 1 and arraylist 2
A1.addAll(A2);
//print the combined list
System.out.println("Combined A1 and A2 list :"+ A1);
ArrayList<String> A3 = new ArrayList<>(); //Array list 3
//add element to arraylist 3
A3.add("Riyan Jaykar");
A3.add("Kukku Chikku");
//Combine the arraylist 1 and arraylist 3 starting from 2nd position
A1.addAll(2, A3);
//print the combined list
System.out.println("Combined A1 and A3 list :"+ A1);
}
}
Output:
Explanation:
Create an arraylist A1 and A2. Then, add elements of A2 to A1 and print the arraylist A1. Once this is done, create an arraylist A3 and add elements. After that add elements of A3 to A1 starting from index 2 and print the arraylist A1. This program will help in understanding how to insert elements from a particular index.
Let us see some more examples of addall() method.
Example #3
Code:
import java.util.*;
public class AddAllExample
{
public static void main(String[] args)
{
ArrayList<String> A1 = new ArrayList<>(); //Array list 1
ArrayList<String> A2 = new ArrayList<>(); //Array list 2
//add element to arraylist 2
A2.add("Anabeth Denan");
//Combine the arraylist 1 and arraylist 2
A1.addAll(A2);
//print the combined list
System.out.println("Combined A1 and A2 list :"+ A1);
}
}
Output:
Explanation:
Create an arraylist A1and A2. Then, add elements to A2 only. Print the elements after combining A1 and A2. It can be seen that even though arraylist A1 is empty, elements of Arraylist A2 can be added to A1.
Example #4
Code:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
ArrayList<String> A1 = new ArrayList<>(); //Array list 1
//add elements to arraylist 1
A1.add("Izanorah Denan");
A1.add("Adam Sam");
A1.add("Annamu S");
A1.add("Naasy D");
A1.add("Thukidi D");
A1.add("Princy v");
ArrayList<String> A2 = new ArrayList<>(); //Array list 2
//add element to arraylist 2
A2.add("Anabeth Denan");
//Combine the arraylist 1 and arraylist 2
A1.addAll(A2);
//print the combined list
System.out.println("Combined A1 and A2 list :"+ A1);
ArrayList<String> A3 = new ArrayList<>(); //Array list 3
//add element to arraylist 3
A3.add("Riyan Jaykar");
A3.add("Kukku Chikku");
//Combine the arraylist 2 and arraylist 3 starting from first position
A2.addAll(1, A3);
//print the combined list
System.out.println("Combined A2 and A3 list :"+ A2);
}
}
Output:
Explanation:
Create an arraylist A1 and A2. Then, add elements of A2 to A1 and print the arraylist A1. Once this is done, create an arraylist A3 and add elements. After that, add elements of A3 to A1 starting from index 1 and print the arraylist A1. Unlike the second program in this document, the arraylist A2 and A3 are combined here. Even though arraylist A2 is combined with A1, the original arraylist A2 has not changed and it can be seen in the given sample output.
Conclusion
addAll() is a method in the arraylist of Java that helps in appending every element available in argument collection to the list that is present in the end.
Recommended Articles
This is a guide to addAll() in Java. Here we discuss the introduction, examples, and how addAll() method works in Java? You may also have a look at the following articles to learn more –