{"record":{"id":"883d2a1c8291b594","repo":"krahets/hello-algo","slug":"index-out-of-bounds-883d2a","errorCode":null,"errorMessage":"Index out of bounds","messagePattern":"Index out of bounds","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"en/codes/java/chapter_array_and_linkedlist/my_list.java","lineNumber":37,"sourceCode":"    public MyList() {\n        arr = new int[capacity];\n    }\n\n    /* Get list length (current number of elements) */\n    public int size() {\n        return size;\n    }\n\n    /* Get list capacity */\n    public int capacity() {\n        return capacity;\n    }\n\n    /* Update element */\n    public int get(int index) {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"Index out of bounds\");\n        return arr[index];\n    }\n\n    /* Add elements at the end */\n    public void set(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"Index out of bounds\");\n        arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    public void add(int num) {\n        // When the number of elements exceeds capacity, trigger the extension mechanism\n        if (size == capacity())\n            extendCapacity();\n        arr[size] = num;\n        // Update the number of elements\n        size++;","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/java/chapter_array_and_linkedlist/my_list.java#L19-L55","documentation":"A Java IndexOutOfBoundsException with message 'Index out of bounds' thrown by get() in MyList (en/codes/java/.../my_list.java:37) — the English-localized twin of error 192. MyList is a hand-rolled dynamic array; get(index) returns arr[index] after checking 0 <= index < size, preventing reads of uninitialized backing-array slots beyond the logical element count.","triggerScenarios":"Calling list.get(index) where index < 0 or index >= list.size(). Reading past the last logical element, or using capacity() (backing array length) as the upper bound instead of size() (element count).","commonSituations":"Off-by-one loops using `<= size`; confusing size() with capacity(); stale indices after insert/remove shifted elements; reading an index that was never populated by add().","solutions":["Ensure 0 <= index < list.size() before get.","Bound loops with `< list.size()`, never `<= size` or `< capacity()`.","Recompute indices after insert/remove.","Use size() as the valid range upper bound."],"exampleFix":"// before: <= size reads one past the last element\nfor (int i = 0; i <= list.size(); i++) {\n    int v = list.get(i);\n}\n\n// after: correct bound\nfor (int i = 0; i < list.size(); i++) {\n    int v = list.get(i);\n}","handlingStrategy":"validation","validationCode":"// Validate index before MyList.get\npublic static int safeGet(MyList list, int index) {\n    if (index < 0 || index >= list.size()) {\n        throw new IllegalArgumentException(\"index \" + index + \" out of [0,\" + list.size() + \")\");\n    }\n    return list.get(index);\n}","typeGuard":"static boolean inBounds(MyList list, int index) {\n    return index >= 0 && index < list.size();\n}","tryCatchPattern":"try {\n    int v = list.get(index);\n} catch (IndexOutOfBoundsException e) {\n    // index outside [0, size); log and recover\n}","preventionTips":["Bound loops with `< list.size()`, never `<= size` or `< capacity()`.","Use size() for the element count, not capacity().","Recompute indices after insert/remove.","Validate external input against size() before indexing."],"tags":["java","dynamic-array","list","index-out-of-bounds"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}