Java ArrayList addAll() Method
The addAll() method adds all elements from a 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) - indicates the index value at which the collection elements are inserted
- c - the collection elements to be inserted
If no actual argument is passed for index, the element will be appended to the very end of the array.
Return Value
If the element is inserted successfully, returns true.
If the given collection is null, a NullPointerException is thrown.
Note: If index is out of range, this method throws an IndexOutOfBoundsException exception.
Example
Inserting elements using the ArrayList addAll() method:
Example
import
java.util.ArrayList
;
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 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 example above, we created two dynamic arrays, primeNumbers and numbers.
Please note this line:
numbers.addAll(primeNumbers);
Here the optional parameter index is not passed to the addAll() method. Therefore, all elements of primeNumbers are added to the end of numbers.
Inserting an element at a specified position:
Example
import
java.util.ArrayList
;
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 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]
更新 ArrayList 2:[JavaScript,Java,Python,C]
Please note this line:
languages2.addAll(1, languages1);
Here the optional parameter index is passed to the addAll() method. Therefore, all elements of array languages1 are added to array languages at index position 1.
Inserting elements of a Set collection into a 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
<
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 from the hashset to the array
list.
addAll
(
set
)
;
System
.
out
.
println
(
"Updated ArrayList: "
list
)
;
}
}
The output of the above program is:
Set: [Java, JavaScript, Python]
初始化 ArrayList: [English]
更新 ArrayList: [English, Java, JavaScript, Python]
In the example above, 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 was not passed the optional parameter index. Therefore, all elements are added at the end of the dynamic array.
Java ArrayList