{"record":{"id":"c0f1d0996d6ae9be","repo":"krahets/hello-algo","slug":"error-c0f1d0","errorCode":null,"errorMessage":"インデックスが範囲外です","messagePattern":"インデックスが範囲外です","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ja/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/ja/codes/javascript/chapter_array_and_linkedlist/my_list.js#L14-L50","documentation":"Thrown by MyList.get (Japanese: 'インデックスが範囲外です' = 'index out of range') when index is outside [0, size). The list uses a fixed backing array with a logical size separate from capacity, so get must refuse reads beyond the logical length to avoid returning uninitialized slots.","triggerScenarios":"Calling get with an index >= current size; passing a negative index; using capacity() as the upper bound instead of size().","commonSituations":"Confusing size with capacity; stale loop bounds after add/remove/insert; off-by-one in for loops.","solutions":["Bounds-check 0 <= index < list.size() before get.","Iterate using size(), never capacity().","After inserts/removes, recompute the bound before indexing."],"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":"const v = (i >= 0 && i < list.size()) ? list.get(i) : undefined;","typeGuard":"function isValidReadIndex(list, i) {\n  return Number.isInteger(i) && i >= 0 && i < list.size();\n}","tryCatchPattern":"try { return list.get(i); }\ncatch (e) { if (!/範囲外/.test(e.message)) throw e; return undefined; }","preventionTips":["Always iterate using size(), not capacity().","Refresh bounds after add/remove/insert.","Clamp external indices to [0, size) before get."],"tags":["list","javascript","validation","guard-clause","bounds","japanese"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}