TheAlgorithms/Java · error · IndexOutOfBoundsException

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

Error message

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

What it means

Thrown by MinHeap.getElement(int) when the 1-based index is <= 0 or greater than the current heap size. MinHeap mirrors MaxHeap's 1-based indexing convention (root at 1, children at 2*index and 2*index+1), so a 0-based index is always out of range. IndexOutOfBoundsException flags an invalid position argument.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java:76

            heapifyDown(i + 1);
        }

        if (minHeap.isEmpty()) {
            System.out.println("No element has been added, empty heap.");
        }
    }

    /**
     * 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 > minHeap.size())) {
            throw new IndexOutOfBoundsException("Index " + elementIndex + " is out of heap range [1, " + minHeap.size() + "]");
        }
        return minHeap.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 > minHeap.size())) {
            throw new IndexOutOfBoundsException("Index " + elementIndex + " is out of heap range [1, " + minHeap.size() + "]");
        }
        return minHeap.get(elementIndex - 1).getKey();
    }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Use a 1-based index within [1, minHeap.size()] (add 1 to convert from 0-based).
  2. Validate `elementIndex >= 1 && elementIndex <= size` before calling.
  3. Refresh size/index after every mutation; do not reuse cached positions.

Example fix

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

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

Strategy: validation

Validate before calling

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

Try / catch

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

Prevention

When it happens

Trigger: Passing 0 from 0-based caller code; passing an index > size after the heap shrank via deletions; reusing a stale index captured before removals.

Common situations: Mixing 0-based array indexing with the heap's 1-based API; index stored before a delete and reused; size overestimated.

Related errors


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