{"record":{"id":"12ad67bcb66cc256","repo":"krahets/hello-algo","slug":"error-12ad67","errorCode":null,"errorMessage":"インデックスが範囲外です","messagePattern":"インデックスが範囲外です","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"ja/codes/python/chapter_array_and_linkedlist/my_list.py","lineNumber":30,"sourceCode":"        \"\"\"コンストラクタ\"\"\"\n        self._capacity: int = 10  # リスト容量\n        self._arr: list[int] = [0] * self._capacity  # 配列（リスト要素を格納）\n        self._size: int = 0  # リストの長さ（現在の要素数）\n        self._extend_ratio: int = 2  # リスト拡張時の増加倍率\n\n    def size(self) -> int:\n        \"\"\"リストの長さを取得（現在の要素数）\"\"\"\n        return self._size\n\n    def capacity(self) -> int:\n        \"\"\"リスト容量を取得する\"\"\"\n        return self._capacity\n\n    def get(self, index: int) -> int:\n        \"\"\"要素にアクセス\"\"\"\n        # インデックスが範囲外なら例外を送出する。以下同様\n        if index < 0 or index >= self._size:\n            raise IndexError(\"インデックスが範囲外です\")\n        return self._arr[index]\n\n    def set(self, num: int, index: int):\n        \"\"\"要素を更新\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"インデックスが範囲外です\")\n        self._arr[index] = num\n\n    def add(self, num: int):\n        \"\"\"末尾に要素を追加\"\"\"\n        # 要素数が容量を超えると、拡張機構が発動する\n        if self.size() == self.capacity():\n            self.extend_capacity()\n        self._arr[self._size] = num\n        self._size += 1\n\n    def insert(self, num: int, index: int):\n        \"\"\"中間に要素を挿入\"\"\"","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ja/codes/python/chapter_array_and_linkedlist/my_list.py#L12-L48","documentation":"MyList.get raises IndexError('インデックスが範囲外です', 'index out of range') when index is negative or >= self._size, before indexing into the backing array self._arr. It is the explicit bounds check of this hand-rolled dynamic array, enforcing that reads stay within the logical element count rather than the larger allocated capacity. The message is in Japanese because the file is the ja/ localized variant of the textbook code.","triggerScenarios":"Calling get(index) with index >= size after elements were removed; negative index; using a cached index from before a remove() shrank _size; off-by-one loops such as range(0, size()+1).","commonSituations":"Index drift after remove(); confusion between size (logical count) and capacity (allocated slots); 0-based vs 1-based numbering mistakes; loops that overshoot the upper bound by one.","solutions":["Validate against live size: `if 0 <= index < lst.size(): lst.get(index)`.","Prefer the provided size() accessor over cached counts when computing indices.","Recompute indices after any mutation that changes size.","In try/except, catch IndexError and surface a localized/domain message to the user."],"exampleFix":"// before\nval = lst.get(maybe_stale_index)\n// after\nif 0 <= maybe_stale_index < lst.size():\n    val = lst.get(maybe_stale_index)\nelse:\n    val = None","handlingStrategy":"validation","validationCode":"if 0 <= index < lst.size():\n    val = lst.get(index)","typeGuard":"def valid_list_index(l, index) -> bool:\n    return 0 <= index < l.size()","tryCatchPattern":"try:\n    val = lst.get(index)\nexcept IndexError:\n    val = None","preventionTips":["Validate against live size() before indexing.","Recompute indices after remove() mutations.","Distinguish size (logical count) from capacity (allocated slots)."],"tags":["list","dynamic-array","indexerror","bounds-check","python","i18n","data-structures"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}