Java Insert Element into an Array at Index using Apache Commons Lang
Tags: ArrayUtils Apache Commons Apache Commons Lang array
In this Java tutorial we learn how to insert an element into an array at a given index using 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 Insert Element into Array in Java
The Apache Commons Lang library provides the method ArrayUtils.insert() to insert an element into an array at a specific index (starting from zero).
Below code example shows you how to use the ArrayUtils.insert() with an array of int values.
InsertToArrayInt.java
import org.apache.commons.lang3.ArrayUtils;
public class InsertToArrayInt {
public static void main(String... args) {
int[] arrayToInsert = new int[] { 1000, 2000, 3000, 5000 };
// insert value at index 3
int[] resultArray = ArrayUtils.insert(3, arrayToInsert, 4000);
System.out.println("The output int array: ");
for(int value : resultArray) {
System.out.println(value);
}
}
}
The output int array:
1000
2000
3000
4000
5000
Below code example shows you how to use the ArrayUtils.insert() with an array of long values.
InsertToArrayLong.java
import org.apache.commons.lang3.ArrayUtils;
public class InsertToArrayLong {
public static void main(String... args) {
long[] arrayToInsert = new long[] { 111111, 222222, 333333, 555555 };
// insert value at index 3
long[] resultArray = ArrayUtils.insert(3, arrayToInsert, 444444);
System.out.println("The output long array: ");
for(long value : resultArray) {
System.out.println(value);
}
}
}
The output long array:
111111
222222
333333
444444
555555
Below code example shows you how to use the ArrayUtils.insert() with an array of double values.
InsertToArrayDouble.java
import org.apache.commons.lang3.ArrayUtils;
public class InsertToArrayDouble {
public static void main(String... args) {
double[] arrayToInsert = new double[] { 1000.1, 2000.2, 4000.4 };
// insert value at index 2
double[] resultArray = ArrayUtils.insert(2, arrayToInsert, 3000.3);
System.out.println("The output double array: ");
for(double value : resultArray) {
System.out.println(value);
}
}
}
The output double array:
1000.1
2000.2
3000.3
4000.4
Below code example shows you how to use the ArrayUtils.insert() with an array of float values.
InsertToArrayFloat.java
import org.apache.commons.lang3.ArrayUtils;
public class InsertToArrayFloat {
public static void main(String... args) {
float[] arrayToInsert = new float[] { 10.1f, 20.2f, 40.4f };
// insert value at index 2
float[] resultArray = ArrayUtils.insert(2, arrayToInsert, 30.3f);
System.out.println("The output float array: ");
for(float value : resultArray) {
System.out.println(value);
}
}
}
The output float array:
10.1
20.2
30.3
40.4
Below code example shows you how to use the ArrayUtils.insert() with an array of boolean values.
InsertToArrayBoolean.java
import org.apache.commons.lang3.ArrayUtils;
public class InsertToArrayBoolean {
public static void main(String... args) {
boolean[] arrayToInsert = new boolean[] { true, true, true };
// insert value at index 1
boolean[] resultArray = ArrayUtils.insert(1, arrayToInsert, false);
System.out.println("The output boolean array: ");
for(boolean value : resultArray) {
System.out.println(value);
}
}
}
The output boolean array:
true
false
true
true
Below code example shows you how to use the ArrayUtils.insert() with an array of byte values.
InsertToArrayByte.java
import org.apache.commons.lang3.ArrayUtils;
public class InsertToArrayByte {
public static void main(String... args) {
byte[] arrayToInsert = new byte[] { 1, 2, 3, 4, 5 };
// insert value at index 2
byte[] resultArray = ArrayUtils.insert(2, arrayToInsert, (byte)0);
System.out.println("The output byte array: ");
for(byte value : resultArray) {
System.out.println(value);
}
}
}
The output byte array:
1
2
0
3
4
5
Below code example shows you how to use the ArrayUtils.insert() with an array of char values.
InsertToArrayChar.java
import org.apache.commons.lang3.ArrayUtils;
public class InsertToArrayChar {
public static void main(String... args) {
char[] arrayToInsert = new char[] { 'a', 'b', 'c', 'd' };
// insert value at index 2
char[] resultArray = ArrayUtils.insert(2, arrayToInsert, 'z');
System.out.println("The output char array: ");
for(char value : resultArray) {
System.out.println(value);
}
}
}
The output char array:
a
b
z
c
d
Below code example shows you how to use the ArrayUtils.insert() with an array of short values.
InsertToArrayShort.java
import org.apache.commons.lang3.ArrayUtils;
public class InsertToArrayShort {
public static void main(String... args) {
short[] arrayToInsert = new short[] { 100, 200, 300, 500 };
// insert value at index 3
short[] resultArray = ArrayUtils.insert(3, arrayToInsert, (short)400);
System.out.println("The output short array: ");
for(short value : resultArray) {
System.out.println(value);
}
}
}
The output short array:
100
200
300
400
500
We can also use the method ArrayUtils.insert() with an array of objects, for example String objects.
InsertToArrayString.java
import org.apache.commons.lang3.ArrayUtils;
public class InsertToArrayString {
public static void main(String... args) {
String[] arrayToInsert = new String[] { "Simple", "Solution" };
// insert value at index 1
String[] resultArray = ArrayUtils.insert(1, arrayToInsert, "Java");
System.out.println("The output String array: ");
for(String value : resultArray) {
System.out.println(value);
}
}
}
The output String array:
Simple
Java
Solution
Happy Coding 😊