{"record":{"id":"73e73faa2ec9a97a","repo":"TheAlgorithms/Java","slug":"index-elementindex-is-out-of-heap-range-1-m","errorCode":null,"errorMessage":"Index ${elementIndex} is out of heap range [1, ${maxHeap.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/MaxHeap.java","lineNumber":99,"sourceCode":"            HeapElement swap = maxHeap.get(elementIndex - 1);\n            maxHeap.set(elementIndex - 1, maxHeap.get(largest));\n            maxHeap.set(largest, swap);\n\n            heapifyDown(largest + 1);\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 > maxHeap.size())) {\n            throw new IndexOutOfBoundsException(\"Index \" + elementIndex + \" is out of heap range [1, \" + maxHeap.size() + \"]\");\n        }\n        return maxHeap.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 > maxHeap.size())) {\n            throw new IndexOutOfBoundsException(\"Index \" + elementIndex + \" is out of heap range [1, \" + maxHeap.size() + \"]\");\n        }\n        return maxHeap.get(elementIndex - 1).getKey();\n    }\n","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java#L81-L117","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use a 1-based index in the range [1, maxHeap.size()] (convert 0-based by adding 1).","Validate `elementIndex >= 1 && elementIndex <= currentSize` before calling.","Re-query size() after any mutation and discard cached indices."],"exampleFix":"// before\nHeapElement e = heap.getElement(arrayIndex); // arrayIndex is 0-based\n\n// after\nHeapElement e = heap.getElement(arrayIndex + 1); // convert to 1-based","handlingStrategy":"validation","validationCode":"// MaxHeap uses 1-based indices: valid range [1, size]\nint size = heapSize; // obtain current size\nif (elementIndex >= 1 && elementIndex <= size) {\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]; log and recover\n}","preventionTips":["Remember MaxHeap indices start at 1; convert 0-based positions with +1.","Re-query size after any insert/delete before using a stored index.","Validate bounds at the boundary of your API, not deep inside logic."],"tags":["heap","max-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"}