{"record":{"id":"ff300d4569960323","repo":"Blankj/AndroidUtilCode","slug":"index-array1-length-0","errorCode":null,"errorMessage":"Index: {}, array1 Length: 0","messagePattern":"Index: (.+?), array1 Length: 0","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/ArrayUtils.java","lineNumber":763,"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":745,"sourceCodeEnd":781,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/ArrayUtils.java#L745-L781","documentation":"ArrayUtils.realAddArr throws IndexOutOfBoundsException(\"Index: N, array1 Length: 0\") when the first array is empty (length 0) and the requested insertion index is not 0. With an empty target array, only index 0 is valid; any other index has no position to insert at.","triggerScenarios":"Calling any typed ArrayUtils.add(emptyArray, index, array2) overload (Object[], int[], byte[], etc.) where array1.length==0 and index != 0. The private realAddArr is shared by every typed add overload.","commonSituations":"Passing an empty array plus a non-zero index from a variable that defaulted to -1 or a stale position; merging into an array that was filtered to empty without resetting the insertion index; off-by-one where callers assume len1-based index is allowed.","solutions":["When array1 is empty, pass index 0 (the only valid position) or simply use array2 directly.","Clamp/validate the index to [0, len1] before calling add: if len1==0 force index=0.","Prefer the single-arg ArrayUtils.add(array1, array2) (append) when you only need concatenation, to avoid manual indexing."],"exampleFix":"// before\nint[] empty = new int[0];\nint[] out = ArrayUtils.add(empty, 2, new int[]{7}); // throws: Index: 2, array1 Length: 0\n\n// after\nint[] out = ArrayUtils.add(empty, 0, new int[]{7}); // valid: only index 0 on empty array","handlingStrategy":"validation","validationCode":"private static int[] safeAdd(int[] a1, int index, int[] a2) {\n    int len1 = a1 == null ? 0 : a1.length;\n    if (len1 == 0) index = 0; // only valid position for an empty array\n    return ArrayUtils.add(a1, index, a2);\n}","typeGuard":"private static boolean isValidAddIndex(Object array1, int index) {\n    int len = java.lang.reflect.Array.getLength(array1);\n    return index >= 0 && index <= len && (len != 0 || index == 0);\n}","tryCatchPattern":null,"preventionTips":["When array1 is empty, pass index 0 only.","Prefer the append overload ArrayUtils.add(array1, array2) for plain concatenation.","Clamp the insertion index to [0, len1] before calling the indexed add."],"tags":["android","array","index-out-of-bounds","argument"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}