Java Add Element at the end of Array using Apache Commons Lang

Tags: ArrayUtils Apache Commons Apache Commons Lang array

This Java tutorial shows you how at the end of the new array and returning 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 element at the end of an array in Java

The Apache Commons Lang library provides the method ArrayUtils.add() to copy a given array to a new array and add one element at the end of the new array.

The below example code shows you how to use ArrayUtils.add() with an int array.

AddElementToArrayInt.java

import org.apache.commons.lang3.ArrayUtils;

public class AddElementToArrayInt {
    public static void main(String... args) {
        int[] arr = new int[] {1, 2, 3};

        int[] newArr = ArrayUtils.add(arr, 4);

        System.out.println("Current int array:");
        for(int item : arr) {
            System.out.println(item);
        }

        System.out.println("\nNew int array:");
        for(int item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
Current int array:
1
2
3

New int array:
1
2
3
4

The below example code shows you how to use ArrayUtils.add() with a long array.

AddElementToArrayLong.java

import org.apache.commons.lang3.ArrayUtils;

public class AddElementToArrayLong {
    public static void main(String... args) {
        long[] arr = new long[] {900888000L, 8777666555L};

        long[] newArr = ArrayUtils.add(arr, 345444333L);

        System.out.println("Current long array:");
        for(long item : arr) {
            System.out.println(item);
        }

        System.out.println("\nNew long array:");
        for(long item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
Current long array:
900888000
8777666555

New long array:
900888000
8777666555
345444333

The below example code shows you how to use ArrayUtils.add() with a double array.

AddElementToArrayDouble.java

import org.apache.commons.lang3.ArrayUtils;

public class AddElementToArrayDouble {
    public static void main(String... args) {
        double[] arr = new double[] {10.1, 20.5, 99.7};

        double[] newArr = ArrayUtils.add(arr, 88.2);

        System.out.println("Current double array:");
        for(double item : arr) {
            System.out.println(item);
        }

        System.out.println("\nNew double array:");
        for(double item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
Current double array:
10.1
20.5
99.7

New double array:
10.1
20.5
99.7
88.2

The below example code shows you how to use ArrayUtils.add() with a float array.

AddElementToArrayFloat.java

import org.apache.commons.lang3.ArrayUtils;

public class AddElementToArrayFloat {
    public static void main(String... args) {
        float[] arr = new float[] {1.2f, 3.4F, 4.5f};

        float[] newArr = ArrayUtils.add(arr, 8.7f);

        System.out.println("Current float array:");
        for(float item : arr) {
            System.out.println(item);
        }

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

New float array:
1.2
3.4
4.5
8.7

The below example code shows you how to use ArrayUtils.add() with a boolean array.

AddElementToArrayBoolean.java

import org.apache.commons.lang3.ArrayUtils;

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

        boolean[] newArr = ArrayUtils.add(arr, false);

        System.out.println("Current boolean array:");
        for(boolean item : arr) {
            System.out.println(item);
        }

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

New boolean array:
false
true
false

The below example code shows you how to use ArrayUtils.add() with a byte array.

AddElementToArrayByte.java

import org.apache.commons.lang3.ArrayUtils;

public class AddElementToArrayByte {
    public static void main(String... args) {
        byte[] arr = new byte[] {12, 13, 14};

        byte[] newArr = ArrayUtils.add(arr, (byte) 19);

        System.out.println("Current byte array:");
        for(byte item : arr) {
            System.out.println(item);
        }

        System.out.println("\nNew byte array:");
        for(byte item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
Current byte array:
12
13
14

New byte array:
12
13
14
19

The below example code shows you how to use ArrayUtils.add() with a char array.

AddElementToArrayChar.java

import org.apache.commons.lang3.ArrayUtils;

public class AddElementToArrayChar {
    public static void main(String... args) {
        char[] arr = new char[] {'a', 'b', 'c'};

        char[] newArr = ArrayUtils.add(arr, 'd');

        System.out.println("Current char array:");
        for(char item : arr) {
            System.out.println(item);
        }

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

New char array:
a
b
c
d

The below example code shows you how to use ArrayUtils.add() with a short array.

AddElementToArrayShort.java

import org.apache.commons.lang3.ArrayUtils;

public class AddElementToArrayShort {
    public static void main(String... args) {
        short[] arr = new short[] {100, 200, 300};

        short[] newArr = ArrayUtils.add(arr, (short)400);

        System.out.println("Current short array:");
        for(short item : arr) {
            System.out.println(item);
        }

        System.out.println("\nNew short array:");
        for(short item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
Current short array:
100
200
300

New short array:
100
200
300
400

You also can use the ArrayUtils.add() method with the array of objects, for example the array of String as below example code.

AddElementToArrayString.java

import org.apache.commons.lang3.ArrayUtils;

public class AddElementToArrayString {
    public static void main(String... args) {
        String[] arr = new String[] {"Simple", "Java"};

        String[] newArr = ArrayUtils.add(arr, "Solution");

        System.out.println("Current String array:");
        for(String item : arr) {
            System.out.println(item);
        }

        System.out.println("\nNew String array:");
        for(String item : newArr) {
            System.out.println(item);
        }
    }
}
The output is:
Current String array:
Simple
Java

New String array:
Simple
Java
Solution

Happy Coding 😊