Java ArrayList addAll() Method
The addAll() method adds all elements of 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 Details:
- index (optional parameter) - denotes the index value where the collection elements are inserted
- c - the collection elements to be inserted
If no actual parameter is passed for index, the elements will be 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 IndexOutOfBoundsException.
Example
Insert elements using the ArrayList addAll() method:
Example
class Main {
public static void main(String[] args){
// Create a dynamic array
ArrayList<Integer> 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<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
// Add all elements of primeNumbers to the dynamic array numbers
numbers.addAll(primeNumbers);
System.out.println("Numbers: " + numbers);
}
}
Running the above program produces the following output:
Prime Numbers: [3, 5] Numbers: [1, 2, 3, 5]
In the above example, we created two dynamic arrays, primeNumbers and numbers.
Please note this line:
numbers.addAll(primeNumbers);
Here, the optional parameter index is not passed in the addAll() method. Therefore, all elements in primeNumbers are appended to the end of numbers.
Insert elements at a specified position:
Example
class Main {
public static void main(String[] args){
ArrayList<String> languages1 = new ArrayList<>();
languages1.add("Java");
languages1.add("Python");
System.out.println("ArrayList 1: " + languages1);
// Create another array
ArrayList<String> languages2 = new ArrayList<>();
languages2.add("JavaScript");
languages2.add("C");
System.out.println("ArrayList 2: " + languages2);
// Add all elements of array languages1 to languages2 at index 1
languages2.addAll(1, languages1);
System.out.println("Updated ArrayList 2: " + languages2);
}
}
Running the above program produces the following output:
ArrayList 1:[Java,Python] ArrayList 2:[JavaScript,C] 更新 ArrayList 2:[JavaScript,Java,Python,C]
Please note this line:
languages2.addAll(1, languages1);
Here, the optional parameter index is passed in the addAll() method. Therefore, all elements of the array languages1 are added to the array languages at index 1.
Insert elements from a Set into a dynamic array:
Example
import java.util.HashSet;
class Main {
public static void main(String[] args){
// Create a HashSet of String type
HashSet<String> 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<String> list = new ArrayList<>();
// Add elements to the array
list.add("English");
System.out.println("Initialized ArrayList: " + list);
// Add all elements of hashset to the array
list.addAll(set);
System.out.println("Updated ArrayList: " + list);
}
}
Running the above program produces the following output:
Set: [Java, JavaScript, Python] 初始化 ArrayList: [English] 更新 ArrayList: [English, Java, JavaScript, Python]
In the above example, we created a hashset named set and a dynamic array named list. Note this line:
list.addAll(set);
We used the addAll() method to add all elements in the hashset to the dynamic array. This method does not take the optional parameter index. Therefore, all elements are added at the end of the dynamic array.
Other Extensions
Java ArrayList