Remove an element at a specific index from an array in Java
This post will discuss how to remove an element at the specified position from a primitive integer array in Java.
We know that Java arrays are fixed-length, unlike ArrayList, which is dynamic. Therefore, Java doesn’t permit the removal of an element at the specified position in an array. This post provides an overview of some of the available alternatives to accomplish this.
1. Using Java 8 Stream
The recommended approach is to use Stream in Java 8 and above, as shown below:
|
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 |
import java.util.Arrays; import java.util.stream.IntStream; class Main { public static int[] remove(int[] a, int index) { if (a == null || index < 0 || index >= a.length) { return a; } return IntStream.range(0, a.length) .filter(i -> i != index) .map(i -> a[i]) .toArray(); } public static void main(String[] args) { int[] a = { 1, 2, 3, 4, 5 }; int index = 2; a = remove(a, index); System.out.println(Arrays.toString(a)); } } |
Output:
[1, 2, 4, 5]
2. Using ArrayList
Another plausible way of removing an element at the specified position from the specified array involves using the List data structure, as demonstrated below:
- Insert all array elements into a
ArrayList. - Remove the element present at the specified position in the
ArrayListusingremove()method. - Convert the
ArrayListback to the array and return it.
|
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.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static int[] remove(int[] a, int index) { if (a == null || index < 0 || index >= a.length) { return a; } List<Integer> result = IntStream.of(a) // IntStream .boxed() .collect(Collectors.toList()); result.remove(index); return result.stream() .mapToInt(Integer::intValue) .toArray(); } public static void main(String[] args) { int[] a = { 1, 2, 3, 4, 5 }; int index = 2; a = remove(a, index); System.out.println(Arrays.toString(a)); } } |
Output:
[1, 2, 4, 5]
3. Using System.arraycopy() method
Another efficient solution is to make two calls to System.arraycopy(), which can 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 26 27 |
import java.util.Arrays; class Main { private static int[] remove(int[] a, int index) { if (a == null || index < 0 || index >= a.length) { return a; } int[] result = new int[a.length - 1]; System.arraycopy(a, 0, result, 0, index); System.arraycopy(a, index + 1, result, index, a.length - index - 1); return result; } public static void main(String[] args) { int[] a = { 1, 2, 3, 4, 5 }; int index = 2; a = remove(a, index); System.out.println(Arrays.toString(a)); } } |
Output:
[1, 2, 4, 5]
4. Naive solution
Instead of using System.arraycopy(), we can write our own routine, which logically works in a similar way. The idea is to declare a new array with one less element and copy the relevant values from the original array into the new 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 26 27 28 29 30 31 |
import java.util.Arrays; class Main { private static int[] remove(int[] a, int index) { if (a == null || index < 0 || index >= a.length) { return a; } int[] result = new int[a.length - 1]; for (int i = 0; i < index; i++) { result[i] = a[i]; } for (int i = index; i < a.length - 1; i++) { result[i] = a[i + 1]; } return result; } public static void main(String[] args) { int[] a = { 1, 2, 3, 4, 5 }; int index = 2; a = remove(a, index); System.out.println(Arrays.toString(a)); } } |
Output:
[1, 2, 4, 5]
5. Using Apache Commons Lang
We can also leverage Apache Commons Lang’s ArrayUtils class, which offers the remove() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; class Main { public static void main(String[] args) { int[] a = { 1, 2, 3, 4, 5 }; int index = 2; a = ArrayUtils.remove(a, index); System.out.println(Arrays.toString(a)); } } |
That’s all about removing an element at a specific index from an array 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 :)