Java ArrayList addAll() Function
What is an ArrayList?
Arraylist is a part of collection framework in java. It contains several inbuilt functions like addAll function which can be found in the java.util.ArrayList Package. It implements the List interface of the collections framework.
Here, in the page we will discuss about the ArrayList’s addAll() Method in java.
ArrayList addAll Function:
ArrayList addAll() function is a pre-defined function of java.ArrayList class. addAll() can add up all the elements of an ArrayList at the end of the other ArrayList or we can also specify any index where we want to append the elements at a particular position.
The two addAll methods that are used in Java are given below:
- arraylist.addAll(Collection c)
- arraylist.addAll(int index, Collection c)
1. arraylist.addAll(Collection C)
We can add each and every element of the chosen ArrayList as an addition to the last of the list. We are not specifying any index here, therefore by default, the elements will get added to the end of the chosen ArrayList.
Syntax:
ArrayList_Name1.addAll(ArrayList_Name2)
Return Type:
true, if the required changes occurs.
Exceptions:
UnsupportedOperationException : if this list does not support the addAll operation NullPointerException : if arraylist doesn't contain any value i.e. if It is null.
Example:
// Importing all the required Classes import java.util.*; public class Main{ public static void main(String[] args){ // Initializing an arraylist ArrayList arr1 = new ArrayList<>(); // Adding elements to the arraylist arr1.add(1); arr1.add(2); arr1.add(3); System.out.println("ArrayList1 : " + arr1); // Initializing the second arraylist ArrayList arr2 = new ArrayList<>(); arr2.add(4); arr2.add(5); System.out.println("ArrayList2 : " + arr2); // add elements using addAll function arr1.addAll(arr2); System.out.println("After Adding ArrayList2 in ArrayList1, We get : " + arr1); } }
Output: ArrayList1 : [1, 2, 3] ArrayList2 : [4, 5] After Adding ArrayList2 in ArrayList1, We get : [1, 2, 3, 4, 5]
2. arraylist.addAll(index i, Collection C)
We can also add each and every element of the chosen ArrayList to a specific position. The new elements will appear in this list in the order that they are returned by the specified collection’s iterator.
Syntax:
ArrayList_Name1.addAll(index i, ArrayList_Name2)
Return Type:
true, if the required changes occurs.
Exceptions:
UnsupportedOperationException : if this list does not support the addAll operation NullPointerException : if arraylist doesn't contain any value i.e. if It is null.
// Importing all the required packages import java.util.*; public class Main{ public static void main(String[] args){ // Initializing an arraylist ArrayList arr1 = new ArrayList<>(); // Adding elements to the arraylist arr1.add(1); arr1.add(4); arr1.add(5); System.out.println("ArrayList1 : " + arr1); // Initializing the second arraylist ArrayList arr2 = new ArrayList<>(); arr2.add(2); arr2.add(3); System.out.println("ArrayList2 : " + arr2); // Adding elements at the specified index arr1.addAll(1,arr2); System.out.println("After Adding ArrayList2 in ArrayList1, We get : " + arr1); } }
Output: ArrayList1 : [1, 4, 5] ArrayList2 : [2, 3] After Adding ArrayList2 in ArrayList1, We get : [1, 2, 3, 4, 5]
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment