Insert an element into an array at a specific index in Java
This post will discuss how to insert an element into an array at the specified index in Java. The insertion should shift the element currently at that index and any subsequent elements to the right by one position.
We know that unlike an ArrayList, arrays in Java are fixed-size and non-dynamic. Therefore, the insertion of an element at the specified position in an array is not feasible if the array is full. This post provides an overview of some of the available alternatives to accomplish this.
1. Naive solution
We can also write our own routine for this simple task. The idea is to declare a new array with one more element, populate it with relevant values from the old array and a specified element at its correct position.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.Arrays; class Main { private static int[] insert(int[] a, int key, int index) { int[] result = new int[a.length + 1]; for (int i = 0; i < index; i++) { result[i] = a[i]; } result[index] = key; for (int i = index + 1; i <= a.length; i++) { result[i] = a[i - 1]; } return result; } public static void main(String[] args) { int[] a = { 1, 2, 4, 5 }; int key = 3; int index = 2; a = insert(a, key, index); System.out.println(Arrays.toString(a)); } } |
Output:
[1, 2, 3, 4, 5]
We can even do this in a single loop.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
private static int[] insert(int[] a, int key, int index) { int[] result = new int[a.length + 1]; for (int i = 0, j = 0; i < a.length; i++, j++) { if (i == index) { result[j] = key; j++; } result[j] = a[i]; } return result; } |
Output:
[1, 2, 3, 4, 5]
2. Using System.arraycopy() method
We can replace the above solution with two calls to System.arraycopy(), which can efficiently copy an array from the specified source array, beginning at the specified position, to the specified position in the destination array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.Arrays; class Main { private static int[] insert(int[] a, int key, int index) { int[] result = new int[a.length + 1]; System.arraycopy(a, 0, result, 0, index); result[index] = key; System.arraycopy(a, index, result, index + 1, a.length - index); return result; } public static void main(String[] args) { int[] a = { 1, 2, 4, 5 }; int key = 3; int index = 2; a = insert(a, key, index); System.out.println(Arrays.toString(a)); } } |
Output:
[1, 2, 3, 4, 5]
3. Using List
The idea is to convert the array into a list and call the add() method on it, which inserts the specified element at the specified position. Finally, after inserting, we convert the list back to the array. This is demonstrated below in Java 8 and above using Stream.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { private static int[] insert(int[] a, int key, int index) { List<Integer> result = IntStream.of(a) // IntStream .boxed() .collect(Collectors.toList()); result.add(index, key); return result.stream() .mapToInt(Integer::intValue) .toArray(); } public static void main(String[] args) { int[] a = { 1, 2, 4, 5 }; int key = 3; int index = 2; a = insert(a, key, index); System.out.println(Arrays.toString(a)); } } |
Output:
[1, 2, 3, 4, 5]
4. Using Java 8
In Java 8 and above, we can do something like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.Arrays; import java.util.stream.IntStream; class Main { private static int[] insert(int[] a, int key, int index) { return IntStream.range(0, a.length + 1) .map(i -> { if (i < index) { return a[i]; } else if (i == index) { return key; } else { return a[i - 1]; } }) .toArray(); } public static void main(String[] args) { int[] a = { 1, 2, 4, 5 }; int key = 3; int index = 2; a = insert(a, key, index); System.out.println(Arrays.toString(a)); } } |
Output:
[1, 2, 3, 4, 5]
5. Using Apache Commons Lang
We can also leverage Apache Commons Lang’s ArrayUtils class, which offers the insert() method. It internally uses System.arraycopy() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; class Main { public static void main(String[] args) { int[] a = { 1, 2, 4, 5 }; int key = 3; int index = 2; a = ArrayUtils.insert(index, a, key); System.out.println(Arrays.toString(a)); } } |
That’s all about inserting an element into an array at a specific index in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)