krahets/hello-algo · error · IndexOutOfBoundsException

Index out of bounds

Error message

Index out of bounds

What it means

A Java IndexOutOfBoundsException with message 'Index out of bounds' thrown by get() in MyList (en/codes/java/.../my_list.java:37) — the English-localized twin of error 192. MyList is a hand-rolled dynamic array; get(index) returns arr[index] after checking 0 <= index < size, preventing reads of uninitialized backing-array slots beyond the logical element count.

Source

Thrown at en/codes/java/chapter_array_and_linkedlist/my_list.java:37

    public MyList() {
        arr = new int[capacity];
    }

    /* Get list length (current number of elements) */
    public int size() {
        return size;
    }

    /* Get list capacity */
    public int capacity() {
        return capacity;
    }

    /* Update element */
    public int get(int index) {
        // If the index is out of bounds, throw an exception, as below
        if (index < 0 || index >= size)
            throw new IndexOutOfBoundsException("Index out of bounds");
        return arr[index];
    }

    /* Add elements at the end */
    public void set(int index, int num) {
        if (index < 0 || index >= size)
            throw new IndexOutOfBoundsException("Index out of bounds");
        arr[index] = num;
    }

    /* Direct traversal of list elements */
    public void add(int num) {
        // When the number of elements exceeds capacity, trigger the extension mechanism
        if (size == capacity())
            extendCapacity();
        arr[size] = num;
        // Update the number of elements
        size++;

View on GitHub (pinned to 69932aed18)

Solutions

  1. Ensure 0 <= index < list.size() before get.
  2. Bound loops with `< list.size()`, never `<= size` or `< capacity()`.
  3. Recompute indices after insert/remove.
  4. Use size() as the valid range upper bound.

Example fix

// before: <= size reads one past the last element
for (int i = 0; i <= list.size(); i++) {
    int v = list.get(i);
}

// after: correct bound
for (int i = 0; i < list.size(); i++) {
    int v = list.get(i);
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate index before MyList.get
public static int safeGet(MyList list, int index) {
    if (index < 0 || index >= list.size()) {
        throw new IllegalArgumentException("index " + index + " out of [0," + list.size() + ")");
    }
    return list.get(index);
}

Type guard

static boolean inBounds(MyList list, int index) {
    return index >= 0 && index < list.size();
}

Try / catch

try {
    int v = list.get(index);
} catch (IndexOutOfBoundsException e) {
    // index outside [0, size); log and recover
}

Prevention

When it happens

Trigger: Calling list.get(index) where index < 0 or index >= list.size(). Reading past the last logical element, or using capacity() (backing array length) as the upper bound instead of size() (element count).

Common situations: Off-by-one loops using `<= size`; confusing size() with capacity(); stale indices after insert/remove shifted elements; reading an index that was never populated by add().

Related errors


AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13). Data as JSON: /api/errors/883d2a1c8291b594. Report an issue: GitHub.