HOWTO · Java
How to Remove Element From Array and Then Shift Other Elements in Java
Learn how to remove an array element by index or value in Java with a loop, System.arraycopy(), or ArrayList.
On this page
Java arrays have a fixed length. Shifting values left closes a gap, but it does not shorten the array. To return an array with one fewer element, create a smaller array or use a resizable collection. The examples below remove an element by its zero-based index; the final section explains how to remove by value.
Shift elements left and shrink the array with a loop
Copy each element after the selected index into the slot before it, then use Arrays.copyOf to create the shorter result. The original array is modified during the shift, so keep a separate copy first if the original values must remain available.
The valid index range is 0 through array.length - 1. This example checks the index before reading from it. If the index is outside that range, it leaves the array unchanged and prints a message.
Java arrays have fixed length. Assigning later values over an element shifts the remaining values left, but the array still has its original length. To return an array with one fewer element, copy the shifted prefix into a new array. Run this example:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
int indexToRemove = 2;
if (indexToRemove >= 0 && indexToRemove < array.length) {
for (int i = indexToRemove; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
array = Arrays.copyOf(array, array.length - 1);
System.out.println(Arrays.toString(array));
} else {
System.out.println("Index out of bounds");
}
}
}
Output:
[1, 2, 4, 5]
The loop stops at the second-to-last original index because each iteration reads array[i + 1]. After shifting, the old last slot contains a duplicate value; Arrays.copyOf drops that slot by allocating an array one element shorter. Removing the last element also works: the loop runs zero times, and the copy drops the last slot directly.
Copy around an index with System.arraycopy()
System.arraycopy() can copy the part before the removed element and the part after it into separate ranges of a new array. Its arguments are the source, source position, destination, destination position, and number of elements to copy. The first copy length is the removed index; the second is array.length - indexToRemove - 1.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] array = {2, 4, 6, 8, 10};
int indexToRemove = 2;
if (indexToRemove >= 0 && indexToRemove < array.length) {
int[] result = new int[array.length - 1];
System.arraycopy(array, 0, result, 0, indexToRemove);
int elementsAfter = array.length - indexToRemove - 1;
System.arraycopy(array, indexToRemove + 1, result, indexToRemove, elementsAfter);
System.out.println(Arrays.toString(result));
} else {
System.out.println("Index out of bounds");
}
}
}
Output:
[2, 4, 8, 10]
Here the destination array is already the desired size, so no trailing default values are included. System.arraycopy() also supports overlapping ranges when source and destination are the same array; Java handles the copy safely. Copying into a distinct result array, as above, makes the retained prefix and suffix explicit.
Remove an element through an ArrayList
Use ArrayList<Integer> when the data is already a collection or when several additions and removals are needed. An int[] cannot be passed directly to Arrays.asList as a list of integers: primitive arrays are treated as a single object. Box the values into Integer[], build the list, remove by index, then convert back to int[].
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
int indexToRemove = 2;
if (indexToRemove >= 0 && indexToRemove < array.length) {
Integer[] boxed = Arrays.stream(array).boxed().toArray(Integer[]::new);
ArrayList<Integer> values = new ArrayList<>(Arrays.asList(boxed));
values.remove(indexToRemove);
int[] result = values.stream().mapToInt(Integer::intValue).toArray();
System.out.println(Arrays.toString(result));
} else {
System.out.println("Index out of bounds");
}
}
}
Output:
[1, 2, 4, 5]
values.remove(indexToRemove) selects the overload that removes the item at that position. The conversion back to int[] is needed because the list stores boxed Integer objects while the source uses primitive int values. This route is convenient, but it creates a list and a new array; for one removal from an existing int[], either array-copying approach avoids the collection conversion.
Choose by index or value and handle edge cases
The examples remove one element by index, using zero-based indexing. A valid index satisfies index >= 0 && index < array.length. Decide how the calling code should respond when this condition is false: return the original array, return a status, or report the problem. Do not continue to array[index] unless the check succeeds. An array with no elements has no valid index, so the same check safely rejects every index.
To remove by value, scan for the first matching index, then use the same shift-and-shrink pattern. This example removes only the first occurrence. If no value matches, it keeps the original array unchanged; repeated matches after the first remain in the result.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] array = {3, 7, 3, 9};
int target = 3;
int indexToRemove = -1;
for (int i = 0; i < array.length; i++) {
if (array[i] == target) {
indexToRemove = i;
break;
}
}
if (indexToRemove >= 0) {
int[] result = new int[array.length - 1];
for (int i = 0; i < indexToRemove; i++) {
result[i] = array[i];
}
for (int i = indexToRemove + 1; i < array.length; i++) {
result[i - 1] = array[i];
}
array = result;
}
System.out.println(Arrays.toString(array));
}
}
With this input, the first 3 is removed and the result is [7, 3, 9]. If target is absent, indexToRemove stays -1, the copy is skipped, and the original array is printed unchanged. To remove every matching value instead, count the nonmatching values to size the result, then copy only those values.
Choose the loop when you want to see and control each shift. Choose System.arraycopy() when constructing a correctly sized array from two ranges is clearest. Choose ArrayList when the surrounding code already needs a resizable collection or multiple edits. All three approaches produce a new logical sequence without the removed element; only the first loop also changes the contents of its input array before returning a shorter copy.