{"record":{"id":"7c04eedf088bd4df","repo":"TheAlgorithms/Java","slug":"index-elementindex-is-out-of-heap-range-1-m-7c04ee","errorCode":null,"errorMessage":"Index ${elementIndex} is out of heap range [1, ${minHeap.size()}]","messagePattern":"Index (.+?) is out of heap range \\[1, (.+?)\\]","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java","lineNumber":76,"sourceCode":"            heapifyDown(i + 1);\n        }\n\n        if (minHeap.isEmpty()) {\n            System.out.println(\"No element has been added, empty heap.\");\n        }\n    }\n\n    /**\n     * Retrieves the element at the specified index without removing it.\n     * Note: The index is 1-based for consistency with heap operations.\n     *\n     * @param elementIndex 1-based index of the element to retrieve\n     * @return HeapElement at the specified index\n     * @throws IndexOutOfBoundsException if the index is invalid\n     */\n    public HeapElement getElement(int elementIndex) {\n        if ((elementIndex <= 0) || (elementIndex > minHeap.size())) {\n            throw new IndexOutOfBoundsException(\"Index \" + elementIndex + \" is out of heap range [1, \" + minHeap.size() + \"]\");\n        }\n        return minHeap.get(elementIndex - 1);\n    }\n\n    /**\n     * Retrieves the key value of an element at the specified index.\n     *\n     * @param elementIndex 1-based index of the element\n     * @return double value representing the key\n     * @throws IndexOutOfBoundsException if the index is invalid\n     */\n    private double getElementKey(int elementIndex) {\n        if ((elementIndex <= 0) || (elementIndex > minHeap.size())) {\n            throw new IndexOutOfBoundsException(\"Index \" + elementIndex + \" is out of heap range [1, \" + minHeap.size() + \"]\");\n        }\n        return minHeap.get(elementIndex - 1).getKey();\n    }\n","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/heaps/MinHeap.java#L58-L94","documentation":"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.","triggerScenarios":"Passing 0 from 0-based caller code; passing an index > size after the heap shrank via deletions; reusing a stale index captured before removals.","commonSituations":"Mixing 0-based array indexing with the heap's 1-based API; index stored before a delete and reused; size overestimated.","solutions":["Use a 1-based index within [1, minHeap.size()] (add 1 to convert from 0-based).","Validate `elementIndex >= 1 && elementIndex <= size` before calling.","Refresh size/index after every mutation; do not reuse cached positions."],"exampleFix":"// before\nHeapElement e = heap.getElement(arrayIndex); // 0-based\n\n// after\nHeapElement e = heap.getElement(arrayIndex + 1); // 1-based","handlingStrategy":"validation","validationCode":"// MinHeap uses 1-based indices: valid range [1, size]\nif (elementIndex >= 1 && elementIndex <= heapSize) {\n    HeapElement e = heap.getElement(elementIndex);\n}","typeGuard":null,"tryCatchPattern":"try {\n    HeapElement e = heap.getElement(elementIndex);\n} catch (IndexOutOfBoundsException ex) {\n    // index out of [1, size]; recover\n}","preventionTips":["MinHeap indices are 1-based; add 1 when converting from 0-based.","Refresh size after mutations before using a stored index.","Validate bounds at the boundary of your API surface."],"tags":["heap","min-heap","index-out-of-bounds","one-based-index"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}