{"record":{"id":"ce552872f84b08f5","repo":"TheAlgorithms/Java","slug":"array-is-empty","errorCode":null,"errorMessage":"Array is empty","messagePattern":"Array is empty","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/InsertDeleteInArray.java","lineNumber":96,"sourceCode":"     * Deletes an element at the specified position from the array.\n     * <p>\n     * Creates a new array with size = original array size - 1.\n     * 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.","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/InsertDeleteInArray.java#L78-L114","documentation":"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.","triggerScenarios":"Calling deleteElement(new int[0], position) for any position; an empty result returned by a filter/map operation being passed straight in.","commonSituations":"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.","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."],"exampleFix":"// before\nint[] out = InsertDeleteInArray.deleteElement(arr, 0); // arr may be empty\n\n// after\nif (arr.length == 0) {\n    return arr; // nothing to delete\n}\nint[] out = InsertDeleteInArray.deleteElement(arr, 0);","handlingStrategy":"validation","validationCode":"if (array == null || array.length == 0) {\n    return new int[0]; // nothing to delete\n}\nInsertDeleteInArray.deleteElement(array, position);","typeGuard":"public static boolean isNonEmpty(int[] array) {\n    return array != null && array.length > 0;\n}","tryCatchPattern":"try {\n    out = InsertDeleteInArray.deleteElement(arr, pos);\n} catch (IllegalArgumentException e) {\n    if (arr != null && arr.length == 0) return new int[0];\n    throw e;\n}","preventionTips":["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."],"tags":["validation","array","input-validation","empty-state"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}