{"record":{"id":"4186592f0fe4d95b","repo":"didi/DoKit","slug":"index-index-length-length","errorCode":null,"errorMessage":"Index: \" + index + \", Length: \" + length","messagePattern":"Index: \" \\+ index \\+ \", Length: \" \\+ length","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java","lineNumber":882,"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":864,"sourceCodeEnd":900,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java#L864-L900","documentation":"The non-null branch of realAdd validates the insertion index for a single element: it must satisfy 0 <= index <= length. Anything larger or negative would create an uninitialized gap in the new array, so it throws IndexOutOfBoundsException with the offending index and the actual length.","triggerScenarios":"Calling ArrayUtils.add(array, index, element) with index > array.length or index < 0 — off-by-one appends (index = length + 1), negative results from binarySearch/indexOf used unchecked, or indices computed against a longer copy of the data.","commonSituations":"Using arrays.binarySearch's negative insertion points incorrectly (must be -(idx)-1). Cursor positions from UI (list scroll position) applied to a shorter backing array after a data refresh. Loop counters that overshoot by one.","solutions":["Clamp the index before the call: index = Math.max(0, Math.min(index, array.length))","Convert binarySearch results correctly: int insertAt = -(pos) - 1;","For append, use ArrayUtils.add(array, element) (end insertion) instead of computing index manually"],"exampleFix":"// before\nint pos = Arrays.binarySearch(arr, key);\nint[] out = ArrayUtils.add(arr, pos, value); // pos is negative when absent\n\n// after\nint pos = Arrays.binarySearch(arr, key);\nint insertAt = pos >= 0 ? pos : -(pos) - 1;\nint[] out = ArrayUtils.add(arr, insertAt, value);","handlingStrategy":"validation","validationCode":"int safeIndex = Math.max(0, Math.min(index, array == null ? 0 : array.length));\nint[] out = ArrayUtils.add(array, safeIndex, element);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Convert binarySearch miss results with -(pos)-1 before inserting","Use the append overload (add(array, element)) instead of manual end-index math"],"tags":["arrayutils","doraemonkit","index-out-of-bounds","binarysearch","java"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}