{"record":{"id":"4498549965c5bc2e","repo":"krahets/hello-algo","slug":"error-449854","errorCode":null,"errorMessage":"索引越界","messagePattern":"索引越界","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"zh-hant/codes/typescript/chapter_array_and_linkedlist/my_list.ts","lineNumber":32,"sourceCode":"    /* 建構子 */\n    constructor() {\n        this.arr = new Array(this._capacity);\n    }\n\n    /* 獲取串列長度（當前元素數量）*/\n    public size(): number {\n        return this._size;\n    }\n\n    /* 獲取串列容量 */\n    public capacity(): number {\n        return this._capacity;\n    }\n\n    /* 訪問元素 */\n    public get(index: number): number {\n        // 索引如果越界，則丟擲異常，下同\n        if (index < 0 || index >= this._size) throw new Error('索引越界');\n        return this.arr[index];\n    }\n\n    /* 更新元素 */\n    public set(index: number, num: number): void {\n        if (index < 0 || index >= this._size) throw new Error('索引越界');\n        this.arr[index] = num;\n    }\n\n    /* 在尾部新增元素 */\n    public add(num: number): void {\n        // 如果長度等於容量，則需要擴容\n        if (this._size === this._capacity) this.extendCapacity();\n        // 將新元素新增到串列尾部\n        this.arr[this._size] = num;\n        this._size++;\n    }\n","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/zh-hant/codes/typescript/chapter_array_and_linkedlist/my_list.ts#L14-L50","documentation":"Thrown by MyList.get(index) (message: '索引越界' = 'index out of bounds') when index < 0 or index >= _size. The guard mirrors standard dynamic-array bounds checking before returning arr[index].","triggerScenarios":"Calling get(index) with a negative index, or an index >= the number of stored elements.","commonSituations":"Off-by-one loop bounds; using capacity instead of size for bounds; iterating past the last valid element; negative index from subtraction underflow.","solutions":["Validate 0 <= index < list.size() before calling get.","Use list.size() (not capacity()) as the loop upper bound.","When computing an index via subtraction, clamp to >= 0."],"exampleFix":"// before\nconst val = list.get(index); // throws '索引越界' if out of range\n\n// after\nif (index >= 0 && index < list.size()) {\n    const val = list.get(index);\n}","handlingStrategy":"validation","validationCode":"if (index >= 0 && index < list.size()) {\n    const val = list.get(index);\n}","typeGuard":"function isValidIndex(list: MyList, index: number): index is number {\n    return Number.isInteger(index) && index >= 0 && index < list.size();\n}","tryCatchPattern":"try {\n    const val = list.get(index);\n} catch (e) {\n    if (e.message === '索引越界') {\n        // index out of bounds — handle\n    } else throw e;\n}","preventionTips":["Use list.size() — not capacity() — as the loop upper bound.","Clamp computed indices to >= 0 to avoid negative underflow.","Prefer for...of or indexed loops bounded by size() for reads."],"tags":["list","dynamic-array","typescript","index-out-of-bounds","precondition"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}