TheAlgorithms/Java · error · IllegalArgumentException
Position must be between 0 and {array.length}
Error message
Position must be between 0 and {array.length} What it means
Thrown by InsertDeleteInArray.insertElement when the requested insertion position is outside the valid inclusive range [0, array.length]. The library enforces this so that System.arraycopy never reads out of bounds and the returned array stays well-formed. The message appends the actual array length so the caller sees the accepted upper bound.
Source
Thrown at src/main/java/com/thealgorithms/others/InsertDeleteInArray.java:60
* Creates a new array with size = original array size + 1.
* Elements at positions <= insertPos retain their positions,
* while elements at positions > insertPos are shifted right by one position.
* </p>
*
* @param array the original array
* @param element the element to be inserted
* @param position the index at which the element should be inserted (0-based)
* @return a new array with the element inserted at the specified position
* @throws IllegalArgumentException if position is negative or greater than
* array length
* @throws IllegalArgumentException if array is null
*/
public static int[] insertElement(int[] array, int element, int position) {
if (array == null) {
throw new IllegalArgumentException("Array cannot be null");
}
if (position < 0 || position > array.length) {
throw new IllegalArgumentException("Position must be between 0 and " + array.length);
}
int[] newArray = new int[array.length + 1];
// Copy elements before insertion position
System.arraycopy(array, 0, newArray, 0, position);
// Insert the new element
newArray[position] = element;
// Copy remaining elements after insertion position
System.arraycopy(array, position, newArray, position + 1, array.length - position);
return newArray;
}
/**
* Deletes an element at the specified position from the array.View on GitHub (pinned to fdfb9a395b)
Solutions
- Clamp or validate position to [0, array.length] before calling insertElement.
- If you meant to append, pass position = array.length explicitly.
- Re-derive the index from the same array instance you pass in, not a stale copy.
- Sanitize external/user input (round, bound, reject) before it reaches this method.
Example fix
// before int[] out = InsertDeleteInArray.insertElement(arr, val, arr.length + 1); // throws // after int pos = Math.max(0, Math.min(position, arr.length)); int[] out = InsertDeleteInArray.insertElement(arr, val, pos);
Defensive patterns
Strategy: validation
Validate before calling
public static int safePosition(int[] array, int requested) {
if (array == null) throw new IllegalArgumentException("array is null");
return Math.max(0, Math.min(requested, array.length));
}
// usage:
InsertDeleteInArray.insertElement(arr, val, safePosition(arr, requested)); Type guard
public static boolean isValidInsertPosition(int[] array, int position) {
return array != null && position >= 0 && position <= array.length;
} Try / catch
try {
out = InsertDeleteInArray.insertElement(arr, val, pos);
} catch (IllegalArgumentException e) {
// log pos and arr.length, fall back to appending at the end
out = InsertDeleteInArray.insertElement(arr, val, arr.length);
} Prevention
- Remember insert allows position == array.length (append) but nothing higher.
- Compute the index from the exact array instance you pass in.
- Bound user-supplied indices to [0, array.length] before calling.
When it happens
Trigger: Calling insertElement(array, element, position) with position < 0 or position > array.length. Inserting at exactly array.length IS allowed (appends); anything beyond it is rejected.
Common situations: Off-by-one from treating the valid insert slot as array.length-1 (forgetting append is legal); passing a position computed from a different/older array length; user-supplied index parsed from input without clamping; negative index coming from an arithmetic underflow.
Related errors
- Position must be between 0 and + (array.length - 1)
- Array is empty
- Node index out of bounds
- width and height must be > 0
- Key must contain integers from 1 to {}
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/8558ecf484d257df.
Report an issue: GitHub.