didi/DoKit · error · IndexOutOfBoundsException

Index: " + index + ", array1 Length: " + len1

Error message

Index: " + index + ", array1 Length: " + len1

What it means

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.

Source

Thrown at Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java:772

        return (double[]) result;
    }

    @Nullable
    private static Object realAddArr(@Nullable Object array1, int index, @Nullable Object array2, Class clss) {
        if (array1 == null && array2 == null) return null;
        int len1 = getLength(array1);
        int len2 = getLength(array2);
        if (len1 == 0) {
            if (index != 0) {
                throw new IndexOutOfBoundsException("Index: " + index + ", array1 Length: 0");
            }
            return realCopy(array2);
        }
        if (len2 == 0) {
            return realCopy(array1);
        }
        if (index > len1 || index < 0) {
            throw new IndexOutOfBoundsException("Index: " + index + ", array1 Length: " + len1);
        }
        Object joinedArray = Array.newInstance(array1.getClass().getComponentType(), len1 + len2);
        if (index == len1) {
            System.arraycopy(array1, 0, joinedArray, 0, len1);
            System.arraycopy(array2, 0, joinedArray, len1, len2);
        } else if (index == 0) {
            System.arraycopy(array2, 0, joinedArray, 0, len2);
            System.arraycopy(array1, 0, joinedArray, len2, len1);
        } else {
            System.arraycopy(array1, 0, joinedArray, 0, index);
            System.arraycopy(array2, 0, joinedArray, index, len2);
            System.arraycopy(array1, index, joinedArray, index + len2, len1 - index);
        }
        return joinedArray;
    }

    /**
     * <p>Inserts the specified element at the specified position in the array.

View on GitHub (pinned to 626827cddb)

Solutions

  1. Validate/clamp the index before the call: index = Math.max(0, Math.min(index, array1.length))
  2. Check for sentinel -1 from indexOf-style lookups before using them as insert positions
  3. Prefer the append overload ArrayUtils.add(array1, array2) when the intent is concatenation

Example fix

// before
int idx = list.indexOf(item); // -1 when absent
String[] out = ArrayUtils.add(arr, idx, insertion);

// after
int idx = list.indexOf(item);
if (idx >= 0 && idx <= arr.length) {
  String[] out = ArrayUtils.add(arr, idx, insertion);
}
Defensive patterns

Strategy: validation

Validate before calling

if (index >= 0 && index <= array1.length) { int[] out = ArrayUtils.add(array1, index, array2); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14). Data as JSON: /api/errors/6def3543ffefcf98. Report an issue: GitHub.