addAll
is a static method of the ArrayUtils
class that returns a new array containing all the elements of the given arrays.
The new array is a combination of the elements of the first array and the elements of the second array.
array1 = [1,2,3,4,5]
array2 = [4,3,1]
The array [1,2,3,4,5,4,3,1]
is a result of applying the addAll
function, becuase the elements of the first array are followed by the elements of the second array.
array1 = [1,2,3,4,5]
array2 = null
The array [1,2,3,4,5]
is a result of applying the addAll
function, because the second array is null
.
ArrayUtils
is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml
file.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the commons-lang package, refer to the Maven Repository.
ArrayUtils
You can import the ArrayUtils
class as follows:
import org.apache.commons.lang3.ArrayUtils;
public static int[] addAll(final int[] array1, final int... array2)
final int[] array1
is the first array.
final int... array2
is the second array.
A new array containing the elements of the first array, followed by the elements of the second array is returned.
import org.apache.commons.lang3.ArrayUtils;public class Main {private static void printArray(int[] array){System.out.print("[ ");for(int i:array) System.out.print( i + " ");System.out.println("]");}public static void main(String[] args) {int[] array1 = {1,2,3,4,5};System.out.print("Array1 is - ");printArray(array1);int[] array2 = {4, 3, 1};System.out.print("Array2 is - ");printArray(array2);int[] combinedArray = ArrayUtils.addAll(array1, array2);System.out.print("Combined Array is - ");printArray(combinedArray);}}
Array1 is - [ 1 2 3 4 5 ]
Array2 is - [ 4 3 1 ]
Combined Array is - [ 1 2 3 4 5 4 3 1 ]