Java Add Two Arrays using Apache Commons Lang

Tags: ArrayUtils Apache Commons Apache Commons Lang array

In this Java tutorial we learn how to add two arrays to return a new array using the ArrayUtils class of Apache Commons Lang library.

How to add Apache Commons Lang 3 library to your Java project

To use the Apache Commons Lang 3 library in the Gradle build project, add the following dependency into the build.gradle file.

implementation 'org.apache.commons:commons-lang3:3.12.0'

To use the Apache Commons Lang 3 library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.12.0</version>
</dependency>

To have more information about the Apache Commons Lang 3 library you can visit the library home page at commons.apache.org/proper/commons-lang/

How to add two arrays in Java

The Apache Commons Lang library provides the method ArrayUtils.addAll() to add two given arrays and return a new array containing all elements from two input arrays.

The Java program below shows you how to array two arrays of int values.

AddAllToArrayInt.java

import org.apache.commons.lang3.ArrayUtils;

public class AddAllToArrayInt {
    public static void main(String... args) {
        int[] arr1 = new int[] {123, 456, 789};
        int[] arr2 = new int[] {987, 654, 321};

        int[] newArr = ArrayUtils.addAll(arr1, arr2);

        System.out.println("\nNew int array:");
        for(int item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
New int array:
123
456
789
987
654
321

The Java program below shows you how to array two arrays of long values.

AddAllToArrayLong.java

import org.apache.commons.lang3.ArrayUtils;

public class AddAllToArrayLong {
    public static void main(String... args) {
        long[] arr1 = new long[] {123456L, 7890123L};
        long[] arr2 = new long[] {987654321L};

        long[] newArr = ArrayUtils.addAll(arr1, arr2);

        System.out.println("\nNew long array:");
        for(long item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
New long array:
123456
7890123
987654321

The Java program below shows you how to array two arrays of double values.

AddAllToArrayDouble.java

import org.apache.commons.lang3.ArrayUtils;

public class AddAllToArrayDouble {
    public static void main(String... args) {
        double[] arr1 = new double[] {10.1, 20.2, 30.3};
        double[] arr2 = new double[] {40.4, 50.5, 60.6};

        double[] newArr = ArrayUtils.addAll(arr1, arr2);

        System.out.println("\nNew double array:");
        for(double item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
New double array:
10.1
20.2
30.3
40.4
50.5
60.6

The Java program below shows you how to array two arrays of float values.

AddAllToArrayFloat.java

import org.apache.commons.lang3.ArrayUtils;

public class AddAllToArrayFloat {
    public static void main(String... args) {
        float[] arr1 = new float[] {1.2f, 2.3f};
        float[] arr2 = new float[] {3.4f, 4.5f, 5.6f};

        float[] newArr = ArrayUtils.addAll(arr1, arr2);

        System.out.println("\nNew float array:");
        for(float item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
New float array:
1.2
2.3
3.4
4.5
5.6

The Java program below shows you how to array two arrays of boolean values.

AddAllToArrayBoolean.java

import org.apache.commons.lang3.ArrayUtils;

public class AddAllToArrayBoolean {
    public static void main(String... args) {
        boolean[] arr1 = new boolean[] {false, true};
        boolean[] arr2 = new boolean[] {false, true, false};

        boolean[] newArr = ArrayUtils.addAll(arr1, arr2);

        System.out.println("\nNew boolean array:");
        for(boolean item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
New boolean array:
false
true
false
true
false

The Java program below shows you how to array two arrays of byte values.

AddAllToArrayByte.java

import org.apache.commons.lang3.ArrayUtils;

public class AddAllToArrayByte {
    public static void main(String... args) {
        byte[] arr1 = new byte[] {10, 20, 30};
        byte[] arr2 = new byte[] {40, 50};

        byte[] newArr = ArrayUtils.addAll(arr1, arr2);

        System.out.println("\nNew byte array:");
        for(byte item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
New byte array:
10
20
30
40
50

The Java program below shows you how to array two arrays of char values.

AddAllToArrayChar.java

import org.apache.commons.lang3.ArrayUtils;

public class AddAllToArrayChar {
    public static void main(String... args) {
        char[] arr1 = new char[] {'a', 'b', 'c'};
        char[] arr2 = new char[] {'d', 'e'};

        char[] newArr = ArrayUtils.addAll(arr1, arr2);

        System.out.println("\nNew char array:");
        for(char item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
New char array:
a
b
c
d
e

The Java program below shows you how to array two arrays of short values.

AddAllToArrayShort.java

import org.apache.commons.lang3.ArrayUtils;

public class AddAllToArrayShort {
    public static void main(String... args) {
        short[] arr1 = new short[] {10, 20, 30};
        short[] arr2 = new short[] {1, 2, 3};

        short[] newArr = ArrayUtils.addAll(arr1, arr2);

        System.out.println("\nNew short array:");
        for(short item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
New short array:
10
20
30
1
2
3

We also can add two arrays of object values, for example String objects.

AddAllToArrayString.java

import org.apache.commons.lang3.ArrayUtils;

public class AddAllToArrayString {
    public static void main(String... args) {
        String[] arr1 = new String[] {"Simple", "Solution"};
        String[] arr2 = new String[] {"Java", "Apache Commons Lang"};

        String[] newArr = ArrayUtils.addAll(arr1, arr2);

        System.out.println("\nNew String array:");
        for(String item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
New String array:
Simple
Solution
Java
Apache Commons Lang

Happy Coding 😊