{"record":{"id":"20655ae30ad150c3","repo":"didi/DoKit","slug":"index-index-array1-length-0","errorCode":null,"errorMessage":"Index: \" + index + \", array1 Length: 0","messagePattern":"Index: \" \\+ index \\+ \", array1 Length: 0","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java","lineNumber":764,"sourceCode":"        if (result == null) return null;\n        return (float[]) result;\n    }\n\n    @Nullable\n    public static double[] add(@Nullable double[] array1, int index, @Nullable double[] array2) {\n        Object result = realAddArr(array1, index, array2, Double.TYPE);\n        if (result == null) return null;\n        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);","sourceCodeStart":746,"sourceCodeEnd":782,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java#L746-L782","documentation":"ArrayUtils.realAddArr implements 'insert array2 into array1 at index'. When array1 is empty (length 0), insertion is only legal at index 0 (prepend == append); any other index is out of range, so it throws IndexOutOfBoundsException('Index: N, array1 Length: 0').","triggerScenarios":"Calling any ArrayUtils.add(array1, index, array2) overload (int[], long[], Object[], etc.) with an empty first array and a non-zero index — e.g. defaulting an index to array2.length or a computed insertion point that assumed array1 was non-empty.","commonSituations":"Building arrays incrementally where the first insertion uses a computed position (cursor position, drop index) that is non-zero on the first call. Config defaults creating empty arrays combined with user-supplied indices.","solutions":["When the target array is empty, pass index 0 (or use add(array, array2) appending)","Clamp the index: int safeIndex = array1.length == 0 ? 0 : Math.min(index, array1.length)","Initialize array1 with the first element instead of starting from an empty array if index semantics matter"],"exampleFix":"// before\ndouble[] result = ArrayUtils.add(new double[0], insertPos, values);\n\n// after\ndouble[] result = ArrayUtils.add(new double[0], Math.min(insertPos, 0), values);","handlingStrategy":"validation","validationCode":"int safeIndex = (array1 == null || array1.length == 0) ? 0 : index;\nint[] out = ArrayUtils.add(array1, safeIndex, array2);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Normalize insertion index to 0 when the destination array is empty","Use the two-arg append overload when order does not matter"],"tags":["arrayutils","doraemonkit","index-out-of-bounds","java"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}