{"record":{"id":"6fd2de5237081f89","repo":"Blankj/AndroidUtilCode","slug":"index-length","errorCode":null,"errorMessage":"Index: {}, Length: {}","messagePattern":"Index: (.+?), Length: (.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/ArrayUtils.java","lineNumber":881,"sourceCode":"\n    @NonNull\n    public static double[] add(@Nullable double[] array, int index, double element) {\n        return (double[]) realAdd(array, index, element, Double.TYPE);\n    }\n\n    @NonNull\n    private static Object realAdd(@Nullable Object array, int index, @Nullable Object element, Class clss) {\n        if (array == null) {\n            if (index != 0) {\n                throw new IndexOutOfBoundsException(\"Index: \" + index + \", Length: 0\");\n            }\n            Object joinedArray = Array.newInstance(clss, 1);\n            Array.set(joinedArray, 0, element);\n            return joinedArray;\n        }\n        int length = Array.getLength(array);\n        if (index > length || index < 0) {\n            throw new IndexOutOfBoundsException(\"Index: \" + index + \", Length: \" + length);\n        }\n        Object result = Array.newInstance(clss, length + 1);\n        System.arraycopy(array, 0, result, 0, index);\n        Array.set(result, index, element);\n        if (index < length) {\n            System.arraycopy(array, index, result, index + 1, length - index);\n        }\n        return result;\n    }\n\n    ///////////////////////////////////////////////////////////////////////////\n    // remove\n    ///////////////////////////////////////////////////////////////////////////\n\n    /**\n     * <p>Removes the element at the specified position from the specified array.\n     * All subsequent elements are shifted to the left (substracts one from\n     * their indices).</p>","sourceCodeStart":863,"sourceCodeEnd":899,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/ArrayUtils.java#L863-L899","documentation":"Thrown by ArrayUtils.realAdd when inserting a single element at an index outside the closed range [0, array.length] for a non-null array. The guard rejects index > length and index < 0 before allocating the length+1 result array, protecting the subsequent System.arraycopy.","triggerScenarios":"Calling add(array, index, element) on a non-null array with a negative index or an index greater than array.length (e.g., length+1 to 'append' — illegal; append requires exactly array.length).","commonSituations":"Appending with index = array.length + 1 instead of array.length; passing a 1-based index; off-by-one loop counters; using size() of a different collection as the index.","solutions":["To append, pass index = array.length exactly (not length+1).","Validate 0 <= index && index <= array.length before calling add.","Use ArrayUtils.add(array, element) overload (no index) when you only need append semantics if available, or compute index defensively.","When the index comes from external input, clamp to the valid range."],"exampleFix":"// before\nint[] a = {1, 2, 3};\nint[] r = ArrayUtils.add(a, 5, 9); // Index: 5, Length: 3\n\n// after\nint[] r = ArrayUtils.add(a, a.length, 9); // append at end","handlingStrategy":"validation","validationCode":"int len = (array == null) ? 0 : array.length;\nint safeIndex = (index < 0) ? 0 : Math.min(index, len);\nint[] r = ArrayUtils.add(array, safeIndex, element);","typeGuard":null,"tryCatchPattern":"try {\n    r = ArrayUtils.add(array, index, element);\n} catch (IndexOutOfBoundsException e) {\n    r = ArrayUtils.add(array, array.length, element); // safe append\n}","preventionTips":["Valid insert range is [0, length]; to append use exactly length, not length+1.","Clamp externally sourced indices before calling add.","Distinguish add (inclusive of length) from remove (exclusive of length)."],"tags":["array","index-out-of-bounds","off-by-one","arrayutils","java"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}