{"record":{"id":"6def3543ffefcf98","repo":"didi/DoKit","slug":"index-index-array1-length-len1","errorCode":null,"errorMessage":"Index: \" + index + \", array1 Length: \" + len1","messagePattern":"Index: \" \\+ index \\+ \", array1 Length: \" \\+ len1","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java","lineNumber":772,"sourceCode":"        return (double[]) result;\n    }\n\n    @Nullable\n    private static Object realAddArr(@Nullable Object array1, int index, @Nullable Object array2, Class clss) {\n        if (array1 == null && array2 == null) return null;\n        int len1 = getLength(array1);\n        int len2 = getLength(array2);\n        if (len1 == 0) {\n            if (index != 0) {\n                throw new IndexOutOfBoundsException(\"Index: \" + index + \", array1 Length: 0\");\n            }\n            return realCopy(array2);\n        }\n        if (len2 == 0) {\n            return realCopy(array1);\n        }\n        if (index > len1 || index < 0) {\n            throw new IndexOutOfBoundsException(\"Index: \" + index + \", array1 Length: \" + len1);\n        }\n        Object joinedArray = Array.newInstance(array1.getClass().getComponentType(), len1 + len2);\n        if (index == len1) {\n            System.arraycopy(array1, 0, joinedArray, 0, len1);\n            System.arraycopy(array2, 0, joinedArray, len1, len2);\n        } else if (index == 0) {\n            System.arraycopy(array2, 0, joinedArray, 0, len2);\n            System.arraycopy(array1, 0, joinedArray, len2, len1);\n        } else {\n            System.arraycopy(array1, 0, joinedArray, 0, index);\n            System.arraycopy(array2, 0, joinedArray, index, len2);\n            System.arraycopy(array1, index, joinedArray, index + len2, len1 - index);\n        }\n        return joinedArray;\n    }\n\n    /**\n     * <p>Inserts the specified element at the specified position in the array.","sourceCodeStart":754,"sourceCodeEnd":790,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java#L754-L790","documentation":"In realAddArr, once the empty-array special cases are past, the insertion index must satisfy 0 <= index <= len1. An index greater than the first array's length (or negative) would leave a gap with no data, so it throws IndexOutOfBoundsException including both the index and len1 for diagnosis.","triggerScenarios":"Calling ArrayUtils.add(array1, index, array2) with index > array1.length or index < 0. Common with off-by-one errors (using <= instead of <), indices from a different data structure (list position vs array position), or stale indices computed before the array shrank.","commonSituations":"Mixed ArrayList/array code where list.indexOf results are used as array insertion points after the array was copied/truncated. Off-by-one from using size() instead of size()-1. Negative indices from unsigned/signed confusion or failed lookups returning -1.","solutions":["Validate/clamp the index before the call: index = Math.max(0, Math.min(index, array1.length))","Check for sentinel -1 from indexOf-style lookups before using them as insert positions","Prefer the append overload ArrayUtils.add(array1, array2) when the intent is concatenation"],"exampleFix":"// before\nint idx = list.indexOf(item); // -1 when absent\nString[] out = ArrayUtils.add(arr, idx, insertion);\n\n// after\nint idx = list.indexOf(item);\nif (idx >= 0 && idx <= arr.length) {\n  String[] out = ArrayUtils.add(arr, idx, insertion);\n}","handlingStrategy":"validation","validationCode":"if (index >= 0 && index <= array1.length) { int[] out = ArrayUtils.add(array1, index, array2); }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Reject -1 sentinels from indexOf before using them as indices","Recompute indices after the array shrinks or is copied"],"tags":["arrayutils","doraemonkit","index-out-of-bounds","off-by-one","java"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}