{"record":{"id":"fef7c661d69e23b8","repo":"Blankj/AndroidUtilCode","slug":"index-array1-length","errorCode":null,"errorMessage":"Index: {}, array1 Length: {}","messagePattern":"Index: (.+?), array1 Length: (.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/ArrayUtils.java","lineNumber":771,"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":753,"sourceCodeEnd":789,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/ArrayUtils.java#L753-L789","documentation":"Thrown by ArrayUtils.realAddArr when concatenating two arrays (the add(array1, index, array2) family) at an insertion point that falls outside the closed range [0, array1.length]. The library guards the System.arraycopy boundaries by rejecting out-of-range indices before any allocation. Valid insertion points are 0 (prepend) through array1.length (append).","triggerScenarios":"Calling add(byte[]/short[]/int[]/long[]/float[]/double[]/boolean[]/char[]/Object[] array1, int index, array2) with index < 0, index > array1.length, or any non-zero index when array1 is empty (length 0). Note line 763 produces the 'Length: 0' variant when array1 is empty and index != 0; line 771 produces the general 'Length: N' variant.","commonSituations":"Off-by-one when computing insertion index (e.g., using array.length instead of array.length as the cap is fine, but length+1 is not); passing an index that was valid for a longer array to a now-empty one; treating the insertion index as 1-based; concatenating user-supplied indices without clamping.","solutions":["Clamp/validate the index to 0..array1.length before calling add (index 0 prepends, index array1.length appends).","When array1 is null or empty, force index to 0 (only index 0 is legal for an empty/null array1).","If the intent is pure append, always pass index = array1.length (or 0 when empty).","Add an assertion/guard in the caller computing the insertion point from external input."],"exampleFix":"// before\nbyte[] a = {1, 2, 3};\nbyte[] b = {9};\nbyte[] r = ArrayUtils.add(a, 5, b); // Index: 5, array1 Length: 3\n\n// after\nint insertAt = Math.max(0, Math.min(a.length, 5));\nbyte[] r = ArrayUtils.add(a, insertAt, b);","handlingStrategy":"validation","validationCode":"int len1 = (array1 == null) ? 0 : array1.length;\nint safeIndex = Math.max(0, Math.min(index, len1));\nif (len1 == 0) safeIndex = 0; // empty/null array1 only allows index 0\nbyte[] joined = ArrayUtils.add(array1, safeIndex, array2);","typeGuard":null,"tryCatchPattern":"try {\n    result = ArrayUtils.add(a, idx, b);\n} catch (IndexOutOfBoundsException e) {\n    // fall back to a safe append\n    result = ArrayUtils.add(a, a.length, b);\n}","preventionTips":["Treat the insertion index as a closed range [0, array1.length]: 0 prepends, length appends.","When array1 may be empty or null, always force index to 0.","Never derive the index from a different array's length without clamping."],"tags":["array","index-out-of-bounds","arrayutils","java"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}