TheAlgorithms/Java · error · IndexOutOfBoundsException

Index: {index}, Size: {size}

Error message

Index: {index}, Size: {size}

What it means

DynamicArray.get() performs a full bounds check: index must be in [0, size). Unlike put(), get does not expand capacity; accessing beyond the current element count is an error. The message includes both the requested index and the current size for diagnostics.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java:92

        elements[index] = element;
        if (index >= size) {
            size = index + 1;
        }
        modCount++; // Increment modification count
    }

    /**
     * Retrieves the element at the specified index.
     *
     * @param index the index of the element to retrieve
     * @return the element at the specified index
     * @throws IndexOutOfBoundsException if index is less than 0 or greater than or
     *                                   equal to the current size
     */
    @SuppressWarnings("unchecked")
    public E get(final int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        return (E) elements[index];
    }

    /**
     * Removes and returns the element at the specified index.
     *
     * @param index the index of the element to be removed
     * @return the element that was removed from the array
     * @throws IndexOutOfBoundsException if index is less than 0 or greater than or
     *                                   equal to the current size
     */
    public E remove(final int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        @SuppressWarnings("unchecked") E oldElement = (E) elements[index];
        fastRemove(index);

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check index >= 0 && index < array.getSize() before calling get()
  2. Fix loop bounds to use < instead of <=
  3. Use getSize() (not any capacity field) for boundary calculations

Example fix

// before
for (int i = 0; i <= arr.getSize(); i++) { // off-by-one
    process(arr.get(i));
}

// after
for (int i = 0; i < arr.getSize(); i++) {
    process(arr.get(i));
}
Defensive patterns

Strategy: validation

Validate before calling

if (index >= 0 && index < array.getSize()) {
    return array.get(index);
}
throw new IndexOutOfBoundsException("Invalid index: " + index);

Try / catch

try {
    return array.get(index);
} catch (IndexOutOfBoundsException e) {
    return defaultValue; // or rethrow with context
}

Prevention

When it happens

Trigger: Calling array.get(array.getSize()) (one past the end), array.get(-1), or any index >= size. Common in loops that use <= instead of <, or code that confuses capacity with size.

Common situations: Off-by-one loop bounds (using <= where < is needed). Using array length/capacity instead of getSize(). Accessing an index after elements were removed without adjusting the index.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/d079488e30948ecb. Report an issue: GitHub.