{"record":{"id":"d079488e30948ecb","repo":"TheAlgorithms/Java","slug":"index-index-size-size","errorCode":null,"errorMessage":"Index: {index}, Size: {size}","messagePattern":"Index: (.+?), Size: (.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java","lineNumber":92,"sourceCode":"        elements[index] = element;\n        if (index >= size) {\n            size = index + 1;\n        }\n        modCount++; // Increment modification count\n    }\n\n    /**\n     * Retrieves the element at the specified index.\n     *\n     * @param index the index of the element to retrieve\n     * @return the element at the specified index\n     * @throws IndexOutOfBoundsException if index is less than 0 or greater than or\n     *                                   equal to the current size\n     */\n    @SuppressWarnings(\"unchecked\")\n    public E get(final int index) {\n        if (index < 0 || index >= size) {\n            throw new IndexOutOfBoundsException(\"Index: \" + index + \", Size: \" + size);\n        }\n        return (E) elements[index];\n    }\n\n    /**\n     * Removes and returns the element at the specified index.\n     *\n     * @param index the index of the element to be removed\n     * @return the element that was removed from the array\n     * @throws IndexOutOfBoundsException if index is less than 0 or greater than or\n     *                                   equal to the current size\n     */\n    public E remove(final int index) {\n        if (index < 0 || index >= size) {\n            throw new IndexOutOfBoundsException(\"Index: \" + index + \", Size: \" + size);\n        }\n        @SuppressWarnings(\"unchecked\") E oldElement = (E) elements[index];\n        fastRemove(index);","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java#L74-L110","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check index >= 0 && index < array.getSize() before calling get()","Fix loop bounds to use < instead of <=","Use getSize() (not any capacity field) for boundary calculations"],"exampleFix":"// before\nfor (int i = 0; i <= arr.getSize(); i++) { // off-by-one\n    process(arr.get(i));\n}\n\n// after\nfor (int i = 0; i < arr.getSize(); i++) {\n    process(arr.get(i));\n}","handlingStrategy":"validation","validationCode":"if (index >= 0 && index < array.getSize()) {\n    return array.get(index);\n}\nthrow new IndexOutOfBoundsException(\"Invalid index: \" + index);\n","typeGuard":null,"tryCatchPattern":"try {\n    return array.get(index);\n} catch (IndexOutOfBoundsException e) {\n    return defaultValue; // or rethrow with context\n}","preventionTips":["Always bound-check with getSize() before get()","Use < (not <=) in loop conditions","Use getSize() not capacity for bounds — size tracks actual elements"],"tags":["dynamic-array","index-bounds","off-by-one"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}