{"record":{"id":"9993373f593b44a0","repo":"krahets/hello-algo","slug":"error-999337","errorCode":null,"errorMessage":"索引越界","messagePattern":"索引越界","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"codes/java/chapter_array_and_linkedlist/my_list.java","lineNumber":37,"sourceCode":"    public MyList() {\n        arr = new int[capacity];\n    }\n\n    /* 获取列表长度（当前元素数量） */\n    public int size() {\n        return size;\n    }\n\n    /* 获取列表容量 */\n    public int capacity() {\n        return capacity;\n    }\n\n    /* 访问元素 */\n    public int get(int index) {\n        // 索引如果越界，则抛出异常，下同\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"索引越界\");\n        return arr[index];\n    }\n\n    /* 更新元素 */\n    public void set(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"索引越界\");\n        arr[index] = num;\n    }\n\n    /* 在尾部添加元素 */\n    public void add(int num) {\n        // 元素数量超出容量时，触发扩容机制\n        if (size == capacity())\n            extendCapacity();\n        arr[size] = num;\n        // 更新元素数量\n        size++;","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/codes/java/chapter_array_and_linkedlist/my_list.java#L19-L55","documentation":"A Java IndexOutOfBoundsException with message '索引越界' thrown by get() in MyList (my_list.java:37). MyList is a hand-rolled dynamic array; get(index) returns arr[index] after checking 0 <= index < size. The guard prevents reading uninitialized/garbage slots beyond the logical element count (size) even though the backing array may be larger (capacity).","triggerScenarios":"Calling list.get(index) where index < 0 or index >= list.size(). Reading past the last logical element, or reading before the first; using list.capacity() (the backing array length) as the upper bound instead of list.size().","commonSituations":"Off-by-one loops (`<= size` instead of `< size`); confusing size() (element count) with capacity() (array length); using an index captured before remove() shifted elements left; stale indices after insert() shifted elements right.","solutions":["Ensure 0 <= index < list.size() before get.","Always bound loops with `< list.size()`, never `<= size` or `< list.capacity()`.","Recompute indices after any insert/remove since they shift elements.","Use size(), not capacity(), as the valid range upper bound."],"exampleFix":"// before: off-by-one 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":"// Java has no structural type guards; use an explicit bounds predicate\nstatic 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":["Always bound loops with `< list.size()`, never `<= size` or `< capacity()`.","Use size() for the element count; capacity() is the backing array length.","Recompute indices after insert/remove since elements shift.","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"}