{"record":{"id":"6d85b643288fa947","repo":"krahets/hello-algo","slug":"error-6d85b6","errorCode":null,"errorMessage":"索引越界","messagePattern":"索引越界","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"zh-hant/codes/javascript/chapter_array_and_linkedlist/my_list.js","lineNumber":32,"sourceCode":"    /* 建構子 */\n    constructor() {\n        this.#arr = new Array(this.#capacity);\n    }\n\n    /* 獲取串列長度（當前元素數量）*/\n    size() {\n        return this.#size;\n    }\n\n    /* 獲取串列容量 */\n    capacity() {\n        return this.#capacity;\n    }\n\n    /* 訪問元素 */\n    get(index) {\n        // 索引如果越界，則丟擲異常，下同\n        if (index < 0 || index >= this.#size) throw new Error('索引越界');\n        return this.#arr[index];\n    }\n\n    /* 更新元素 */\n    set(index, num) {\n        if (index < 0 || index >= this.#size) throw new Error('索引越界');\n        this.#arr[index] = num;\n    }\n\n    /* 在尾部新增元素 */\n    add(num) {\n        // 如果長度等於容量，則需要擴容\n        if (this.#size === this.#capacity) {\n            this.extendCapacity();\n        }\n        // 將新元素新增到串列尾部\n        this.#arr[this.#size] = num;\n        this.#size++;","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/zh-hant/codes/javascript/chapter_array_and_linkedlist/my_list.js#L14-L50","documentation":"Thrown by get(index) on a dynamic list when index is negative or >= the current element count (#size). The message ('索引越界', Traditional Chinese for 'index out of bounds') guards the backing-array read #arr[index]. It distinguishes logical size from raw capacity, so unused trailing slots are also rejected.","triggerScenarios":"Calling get with an index >= #size (e.g. reading slot #size which exists in the array but is not a live element); passing a negative index; using a loop counter that overshoots by one.","commonSituations":"Confusing the list's capacity with its size; index math computed against capacity rather than size; off-by-one in for-loops that use <= instead of <.","solutions":["Validate 0 <= index < list.size() before calling get.","Prefer iterating with size() as the exclusive upper bound.","Remember capacity() >= size(); never index by capacity.","Catch the error when reading untrusted indices."],"exampleFix":"// before\nconst v = list.get(i); // throws if i >= size\n\n// after\nconst v = (i >= 0 && i < list.size()) ? list.get(i) : undefined;","handlingStrategy":"validation","validationCode":"if (index >= 0 && index < list.size()) {\n    return list.get(index);\n}","typeGuard":"function isValidReadIndex(list, index) {\n    return Number.isInteger(index) && index >= 0 && index < list.size();\n}","tryCatchPattern":"try {\n    return list.get(index);\n} catch (e) {\n    if (e instanceof Error && e.message === '索引越界') {\n        return undefined;\n    }\n    throw e;\n}","preventionTips":["Use size() (not capacity()) as the exclusive upper bound for reads.","Use < rather than <= in index loops.","Validate untrusted indices before access."],"tags":["dynamic-array","list","javascript","index-out-of-bounds","boundary-check"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}