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

Tags: ArrayUtils Apache Commons Apache Commons Lang array

This Java tutorial shows you how to add an element at the beginning of an array by returning 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 an element at the beginning of an array in Java

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

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

AddFirstToArrayInt.java

import org.apache.commons.lang3.ArrayUtils;

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

        int[] newArr = ArrayUtils.addFirst(arr, 1);

        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:
2
3
4

New int array:
1
2
3
4

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

AddFirstToArrayLong.java

import org.apache.commons.lang3.ArrayUtils;

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

        long[] newArr = ArrayUtils.addFirst(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:
345444333
900888000
8777666555

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

AddFirstToArrayDouble.java

import org.apache.commons.lang3.ArrayUtils;

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

        double[] newArr = ArrayUtils.addFirst(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:
88.2
10.1
20.5
99.7

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

AddFirstToArrayFloat.java

import org.apache.commons.lang3.ArrayUtils;

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

        float[] newArr = ArrayUtils.addFirst(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:
8.7
1.2
3.4
4.5

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

AddFirstToArrayBoolean.java

import org.apache.commons.lang3.ArrayUtils;

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

        boolean[] newArr = ArrayUtils.addFirst(arr, true);

        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
false

New boolean array:
true
false
false

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

AddFirstToArrayByte.java

import org.apache.commons.lang3.ArrayUtils;

public class AddFirstToArrayByte {
    public static void main(String... args) {
        byte[] arr = new byte[] {10, 20, 30};

        byte[] newArr = ArrayUtils.addFirst(arr, (byte) 40);

        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:
10
20
30

New byte array:
40
10
20
30

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

AddFirstToArrayChar.java

import org.apache.commons.lang3.ArrayUtils;

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

        char[] newArr = ArrayUtils.addFirst(arr, 'a');

        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:
b
c
d

New char array:
a
b
c
d

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

AddFirstToArrayShort.java

import org.apache.commons.lang3.ArrayUtils;

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

        short[] newArr = ArrayUtils.addFirst(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:
400
100
200
300

You can also use the ArrayUtils.addFirst() with an array of objects, for example String objects.

AddFirstToArrayString.java

import org.apache.commons.lang3.ArrayUtils;

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

        String[] newArr = ArrayUtils.addFirst(arr, "Simple");

        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:
Solution
Java

New String array:
Simple
Solution
Java

Happy Coding 😊