TheAlgorithms/Java · error · IndexOutOfBoundsException

Index ${elementIndex} is out of heap range [1, ${maxHeap.siz

Error message

Index ${elementIndex} is out of heap range [1, ${maxHeap.size()}]

What it means

Thrown by MaxHeap.getElement(int) when the 1-based index is <= 0 or greater than the current heap size. MaxHeap uses 1-based indexing for all heap operations (root is index 1, children 2*index and 2*index+1), so a 0-based index from caller code is always invalid. IndexOutOfBoundsException signals an out-of-range position argument.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java:99

            HeapElement swap = maxHeap.get(elementIndex - 1);
            maxHeap.set(elementIndex - 1, maxHeap.get(largest));
            maxHeap.set(largest, swap);

            heapifyDown(largest + 1);
        }
    }

    /**
     * Retrieves the element at the specified index without removing it.
     * Note: The index is 1-based for consistency with heap operations.
     *
     * @param elementIndex 1-based index of the element to retrieve
     * @return HeapElement at the specified index
     * @throws IndexOutOfBoundsException if the index is invalid
     */
    public HeapElement getElement(int elementIndex) {
        if ((elementIndex <= 0) || (elementIndex > maxHeap.size())) {
            throw new IndexOutOfBoundsException("Index " + elementIndex + " is out of heap range [1, " + maxHeap.size() + "]");
        }
        return maxHeap.get(elementIndex - 1);
    }

    /**
     * Retrieves the key value of an element at the specified index.
     *
     * @param elementIndex 1-based index of the element
     * @return double value representing the key
     * @throws IndexOutOfBoundsException if the index is invalid
     */
    private double getElementKey(int elementIndex) {
        if ((elementIndex <= 0) || (elementIndex > maxHeap.size())) {
            throw new IndexOutOfBoundsException("Index " + elementIndex + " is out of heap range [1, " + maxHeap.size() + "]");
        }
        return maxHeap.get(elementIndex - 1).getKey();
    }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Use a 1-based index in the range [1, maxHeap.size()] (convert 0-based by adding 1).
  2. Validate `elementIndex >= 1 && elementIndex <= currentSize` before calling.
  3. Re-query size() after any mutation and discard cached indices.

Example fix

// before
HeapElement e = heap.getElement(arrayIndex); // arrayIndex is 0-based

// after
HeapElement e = heap.getElement(arrayIndex + 1); // convert to 1-based
Defensive patterns

Strategy: validation

Validate before calling

// MaxHeap uses 1-based indices: valid range [1, size]
int size = heapSize; // obtain current size
if (elementIndex >= 1 && elementIndex <= size) {
    HeapElement e = heap.getElement(elementIndex);
}

Try / catch

try {
    HeapElement e = heap.getElement(elementIndex);
} catch (IndexOutOfBoundsException ex) {
    // index out of [1, size]; log and recover
}

Prevention

When it happens

Trigger: Passing 0 (the classic 0-based vs 1-based mistake); passing an index > current size after the heap shrank; passing a stale index captured before deletions.

Common situations: Caller code mixing 0-based array/list indexing with the heap's 1-based convention; index captured before removeElement and reused after; size assumed larger than actual.

Related errors


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