{"record":{"id":"ca71f22332223b66","repo":"TheAlgorithms/Java","slug":"position-must-be-between-0-and-array-length-ca71f2","errorCode":null,"errorMessage":"Position must be between 0 and  + (array.length - 1)","messagePattern":"Position must be between 0 and  \\+ \\(array\\.length - 1\\)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/InsertDeleteInArray.java","lineNumber":99,"sourceCode":"     * Elements after the deletion position are shifted left by one position.\n     * </p>\n     *\n     * @param array    the original array\n     * @param position the index of the element to be deleted (0-based)\n     * @return a new array with the element at the specified position removed\n     * @throws IllegalArgumentException if position is negative or greater than or\n     *                                  equal to array length\n     * @throws IllegalArgumentException if array is null or empty\n     */\n    public static int[] deleteElement(int[] array, int position) {\n        if (array == null) {\n            throw new IllegalArgumentException(\"Array cannot be null\");\n        }\n        if (array.length == 0) {\n            throw new IllegalArgumentException(\"Array is empty\");\n        }\n        if (position < 0 || position >= array.length) {\n            throw new IllegalArgumentException(\"Position must be between 0 and \" + (array.length - 1));\n        }\n\n        int[] newArray = new int[array.length - 1];\n\n        // Copy elements before deletion position\n        System.arraycopy(array, 0, newArray, 0, position);\n\n        // Copy elements after deletion position\n        System.arraycopy(array, position + 1, newArray, position, array.length - position - 1);\n\n        return newArray;\n    }\n\n    /**\n     * Main method demonstrating insert and delete operations on an array.\n     * <p>\n     * This method interactively:\n     * <ol>","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java#L81-L117","documentation":"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.","triggerScenarios":"Calling deleteElement(array, position) with position < 0 or position >= array.length; using the insert-style bound (array.length) by mistake.","commonSituations":"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.","solutions":["Bound position to [0, array.length-1] before calling deleteElement.","If your index is 1-based, subtract 1 before passing it in.","Recompute the index from the current array length at call time.","Reject user-supplied indices that fall outside the visible range rather than forwarding them."],"exampleFix":"// before\nint[] out = InsertDeleteInArray.deleteElement(arr, arr.length); // throws\n\n// after\nint pos = Math.max(0, Math.min(position, arr.length - 1));\nint[] out = InsertDeleteInArray.deleteElement(arr, pos);","handlingStrategy":"validation","validationCode":"public static int safeDeletePosition(int[] array, int requested) {\n    if (array == null || array.length == 0) throw new IllegalArgumentException(\"array empty\");\n    return Math.max(0, Math.min(requested, array.length - 1));\n}\n// usage:\nInsertDeleteInArray.deleteElement(arr, safeDeletePosition(arr, requested));","typeGuard":"public static boolean isValidDeletePosition(int[] array, int position) {\n    return array != null && array.length > 0 && position >= 0 && position < array.length;\n}","tryCatchPattern":"try {\n    out = InsertDeleteInArray.deleteElement(arr, pos);\n} catch (IllegalArgumentException e) {\n    // last valid index is arr.length - 1; clamp and retry once or surface to user\n    throw e;\n}","preventionTips":["Delete bounds are [0, length-1]; do not reuse insert bounds.","Convert 1-based UI indices to 0-based before calling.","Recompute the index from the current array length."],"tags":["validation","array","input-validation","off-by-one"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}