{"record":{"id":"60dc114bd2d7d04f","repo":"krahets/hello-algo","slug":"error-60dc11","errorCode":null,"errorMessage":"インデックスが範囲外です","messagePattern":"インデックスが範囲外です","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ja/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/ja/codes/typescript/chapter_array_and_linkedlist/my_list.ts#L14-L50","documentation":"Thrown by the custom dynamic-array list's get method when index is outside [0, _size). The guard runs before reading arr[index], ensuring the returned value is a real stored element. Note the bound is _size (logical length), not _capacity (allocated length).","triggerScenarios":"Calling get(index) with index < 0 or index >= _size; reading an index that holds stale data beyond the logical length; off-by-one in a loop bound (using capacity or array length instead of size).","commonSituations":"Iterating with this.arr.length instead of this.size(); using an externally cached index after removals; negative indices from indexOf returning -1.","solutions":["Validate 0 <= index < list.size() before get().","Always use size() for loop bounds, never the raw backing array length.","Recompute indices after insert/remove shifts them.","Treat indexOf === -1 as not-found before using it as an index."],"exampleFix":"// before\nconst v = list.get(i);  // throws if i out of [0, size)\n\n// after\nif (i >= 0 && i < list.size()) {\n  const v = list.get(i);\n}","handlingStrategy":"validation","validationCode":"function safeGet(list: { size(): number; get(i: number): number }, index: number) {\n  if (index < 0 || index >= list.size()) return null;\n  return list.get(index);\n}","typeGuard":"const isValidReadIndex = (list: { size(): number }, i: number): boolean =>\n  Number.isInteger(i) && i >= 0 && i < list.size();","tryCatchPattern":"try {\n  return list.get(index);\n} catch (e) {\n  if (e instanceof Error && e.message === 'インデックスが範囲外です') return null;\n  throw e;\n}","preventionTips":["Loop with size(), never the raw backing array length.","Treat indexOf === -1 as not-found before using as an index.","Recompute indices after insert/remove."],"tags":["list","typescript","index-out-of-bounds","dynamic-array"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}