TheAlgorithms/Java · error · IllegalArgumentException
Array is empty
Error message
Array is empty
What it means
Thrown by InsertDeleteInArray.deleteElement when the array has length 0. Deleting from an empty array has no valid target index (the position check uses array.length-1, which would underflow), so the library rejects it before reaching that arithmetic.
Source
Thrown at src/main/java/com/thealgorithms/others/InsertDeleteInArray.java:96
* Deletes an element at the specified position from the array.
* <p>
* Creates a new array with size = original array size - 1.
* Elements after the deletion position are shifted left by one position.
* </p>
*
* @param array the original array
* @param position the index of the element to be deleted (0-based)
* @return a new array with the element at the specified position removed
* @throws IllegalArgumentException if position is negative or greater than or
* equal to array length
* @throws IllegalArgumentException if array is null or empty
*/
public static int[] deleteElement(int[] array, int position) {
if (array == null) {
throw new IllegalArgumentException("Array cannot be null");
}
if (array.length == 0) {
throw new IllegalArgumentException("Array is empty");
}
if (position < 0 || position >= array.length) {
throw new IllegalArgumentException("Position must be between 0 and " + (array.length - 1));
}
int[] newArray = new int[array.length - 1];
// Copy elements before deletion position
System.arraycopy(array, 0, newArray, 0, position);
// Copy elements after deletion position
System.arraycopy(array, position + 1, newArray, position, array.length - position - 1);
return newArray;
}
/**
* Main method demonstrating insert and delete operations on an array.View on GitHub (pinned to fdfb9a395b)
Solutions
- Guard the call with an emptiness check and short-circuit (return empty array or skip).
- Ensure the data source always has at least one element before offering a delete operation in the UI/logic.
- Treat length==0 as a no-op rather than forwarding it to deleteElement.
Example fix
// before
int[] out = InsertDeleteInArray.deleteElement(arr, 0); // arr may be empty
// after
if (arr.length == 0) {
return arr; // nothing to delete
}
int[] out = InsertDeleteInArray.deleteElement(arr, 0); Defensive patterns
Strategy: validation
Validate before calling
if (array == null || array.length == 0) {
return new int[0]; // nothing to delete
}
InsertDeleteInArray.deleteElement(array, position); Type guard
public static boolean isNonEmpty(int[] array) {
return array != null && array.length > 0;
} Try / catch
try {
out = InsertDeleteInArray.deleteElement(arr, pos);
} catch (IllegalArgumentException e) {
if (arr != null && arr.length == 0) return new int[0];
throw e;
} Prevention
- Treat an empty array as a no-op for deletion rather than forwarding it.
- Disable delete actions in the UI when the collection is empty.
- Default collection fields to empty arrays, not null, but still guard emptiness.
When it happens
Trigger: Calling deleteElement(new int[0], position) for any position; an empty result returned by a filter/map operation being passed straight in.
Common situations: Processing a collection that was filtered down to nothing; default-initializing to an empty array 'just in case' then attempting deletion; user clears all elements then triggers a remove action.
Related errors
- Position must be between 0 and {array.length}
- Position must be between 0 and + (array.length - 1)
- width and height must be > 0
- Cannot delete from an empty tree
- Input array should not contain negative number(s).
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/ce552872f84b08f5.
Report an issue: GitHub.