Java Arraylist Addall
[ Java ArrayList](#)
The addAll() method adds all elements from the given collection to the arraylist.
The syntax of the addAll() method is:
arraylist.addAll(int index, Collection c)
**Note:** arraylist is an object of the ArrayList class.
**Parameter Description:**
* index (optional parameter) - represents the index value where the collection elements are inserted
* c - the collection elements to be inserted
If index is not passed as an argument, elements are appended to the end of the array.
### Return Value
Returns true if elements are inserted successfully.
If the given collection is null, a NullPointerException is thrown.
Note: If index is out of range, the method throws an IndexOutOfBoundsException.
### Example
Insert elements using the ArrayList addAll() method:
## Example
import java.util.ArrayList;
class Main {
public static void main(String[] args){
// Create a dynamic array
ArrayList primeNumbers =new ArrayList();
// Add elements to the dynamic array
primeNumbers.add(3);
primeNumbers.add(5);
System.out.println("Prime Numbers: "+ primeNumbers);
// Create another dynamic array
ArrayList numbers =new ArrayList();
numbers.add(1);
numbers.add(2);
// Add all elements from primeNumbers to the numbers dynamic array
numbers.addAll(primeNumbers);
System.out.println("Numbers: "+ numbers);
}
}
The output of the above program is:
Prime Numbers: [3, 5]Numbers: [1, 2, 3, 5]
In the above example, we created two dynamic arrays, primeNumbers and numbers respectively.
Note this line:
numbers.addAll(primeNumbers);
The addAll() method here does not pass the optional parameter index. Therefore, all elements from primeNumbers are added to the end of numbers.
Insert elements at a specified position:
## Example
import java.util.ArrayList;
class Main {
public static void main(String[] args){
ArrayList languages1 =new ArrayList();
languages1.add("Java");
languages1.add("Python");
System.out.println("ArrayList 1: "+ languages1);
// Create another array
ArrayList languages2 =new ArrayList();
languages2.add("JavaScript");
languages2.add("C");
System.out.println("ArrayList 2: "+ languages2);
// Insert all elements from array languages1 into languages2 at index position 1
languages2.addAll(1, languages1);
System.out.println("Updated ArrayList 2: "+ languages2);
}
}
The output of the above program is:
ArrayList 1οΌ[JavaοΌPython]ArrayList 2οΌ[JavaScriptοΌC]Updated ArrayList 2οΌ[JavaScriptοΌJavaοΌPythonοΌC]
Note this line:
languages2.addAll(1, languages1);
The addAll() method here passes the optional parameter index. Therefore, all elements from the array languages1 are inserted at index position 1 of the array languages.
Insert elements from a Set collection into the dynamic array:
## Example
import java.util.ArrayList;
import java.util.HashSet;
class Main {
public static void main(String[] args){
// Create a String type hashset
HashSet set =new HashSet();
// Add elements to the hashset
set.add("Java");
set.add("Python");
set.add("JavaScript");
System.out.println("HashSet: "+ set);
// Create an array
ArrayList list =new ArrayList();
// Add elements to the array
list.add("English");
System.out.println("Initial ArrayList: "+ list);
// Add all elements from hashset to the array
list.addAll(set);
System.out.println("Updated ArrayList: "+ list);
}
}
The output of the above program is:
Set: [Java, JavaScript, Python]Initial ArrayList: Updated ArrayList: [English, Java, JavaScript, Python]
In the above example, we created a hashset class named set and a dynamic array named list, note this line:
list.addAll(set);
We used the addAll() method to add all elements from the hashset to the dynamic array. The method does not pass the optional parameter index. Therefore, all elements are added to the end of the dynamic array.
[ Java ArrayList](#)
YouTip