TheAlgorithms/Java · error · IllegalArgumentException

Position must be between 0 and + (array.length - 1)

Error message

Position must be between 0 and  + (array.length - 1)

What it means

Thrown by InsertDeleteInArray.deleteElement when position is outside [0, array.length-1]. Unlike insert, deletion has no 'append' slot, so the upper bound is exclusive at array.length. The message shows array.length-1 as the last valid index.

Source

Thrown at src/main/java/com/thealgorithms/others/InsertDeleteInArray.java:99

     * 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.
     * <p>
     * This method interactively:
     * <ol>

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Bound position to [0, array.length-1] before calling deleteElement.
  2. If your index is 1-based, subtract 1 before passing it in.
  3. Recompute the index from the current array length at call time.
  4. Reject user-supplied indices that fall outside the visible range rather than forwarding them.

Example fix

// before
int[] out = InsertDeleteInArray.deleteElement(arr, arr.length); // throws

// after
int pos = Math.max(0, Math.min(position, arr.length - 1));
int[] out = InsertDeleteInArray.deleteElement(arr, pos);
Defensive patterns

Strategy: validation

Validate before calling

public static int safeDeletePosition(int[] array, int requested) {
    if (array == null || array.length == 0) throw new IllegalArgumentException("array empty");
    return Math.max(0, Math.min(requested, array.length - 1));
}
// usage:
InsertDeleteInArray.deleteElement(arr, safeDeletePosition(arr, requested));

Type guard

public static boolean isValidDeletePosition(int[] array, int position) {
    return array != null && array.length > 0 && position >= 0 && position < array.length;
}

Try / catch

try {
    out = InsertDeleteInArray.deleteElement(arr, pos);
} catch (IllegalArgumentException e) {
    // last valid index is arr.length - 1; clamp and retry once or surface to user
    throw e;
}

Prevention

When it happens

Trigger: Calling deleteElement(array, position) with position < 0 or position >= array.length; using the insert-style bound (array.length) by mistake.

Common situations: Confusing insert bounds (which allow array.length) with delete bounds (which stop at array.length-1); stale position computed before the array shrank; 1-based index from a UI passed into a 0-based API.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/ca71f22332223b66. Report an issue: GitHub.