{"record":{"id":"a39728ce94c0691a","repo":"krahets/hello-algo","slug":"error-a39728","errorCode":null,"errorMessage":"インデックスが範囲外です","messagePattern":"インデックスが範囲外です","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"ja/codes/java/chapter_array_and_linkedlist/my_list.java","lineNumber":37,"sourceCode":"    public MyList() {\n        arr = new int[capacity];\n    }\n\n    /* リストの長さを取得（現在の要素数） */\n    public int size() {\n        return size;\n    }\n\n    /* リスト容量を取得する */\n    public int capacity() {\n        return capacity;\n    }\n\n    /* 要素にアクセス */\n    public int get(int index) {\n        // インデックスが範囲外なら例外を送出する。以下同様\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"インデックスが範囲外です\");\n        return arr[index];\n    }\n\n    /* 要素を更新 */\n    public void set(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"インデックスが範囲外です\");\n        arr[index] = num;\n    }\n\n    /* 末尾に要素を追加 */\n    public void add(int num) {\n        // 要素数が容量を超えると、拡張機構が発動する\n        if (size == capacity())\n            extendCapacity();\n        arr[size] = num;\n        // 要素数を更新\n        size++;","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ja/codes/java/chapter_array_and_linkedlist/my_list.java#L19-L55","documentation":"Thrown by get() in the hand-written dynamic-array class MyList (ja translation of hello-algo). It fires whenever the requested index is negative or >= the current logical element count (size), NOT the backing array length (capacity). The class mimics how java.util.ArrayList enforces bounds: read access is guarded so callers can never reach uninitialized slots in the over-allocated arr[]. The Japanese message 'インデックスが範囲外です' means 'index is out of range'.","triggerScenarios":"Calling myList.get(-1); myList.get(myList.size()); myList.get(myList.size()+k); myList.get(0) on a freshly constructed list whose size()==0; using capacity() (the backing length) instead of size() as a loop bound; using an index value sourced from a different/smaller collection.","commonSituations":"Off-by-one read loop `for(int i=0;i<=size;i++) get(i)`; confusing capacity() with size() after the list was only partially filled; iterating with an index that becomes stale after a concurrent remove()/clear(); porting code from a 1-based index language.","solutions":["Bounds-check before access: `if (index >= 0 && index < list.size()) list.get(index);`","Fix the loop bound to use `<` not `<=`, and read size() (not capacity()) each iteration.","If you legitimately need end-position access use list.get(list.size()-1) after confirming size()>0.","Switch to java.util.ArrayList for production code; this class is a pedagogical reimplementation."],"exampleFix":"// before\nfor (int i = 0; i <= list.capacity(); i++) {\n    System.out.println(list.get(i)); // IndexOutOfBoundsException when i >= size\n}\n// after\nfor (int i = 0; i < list.size(); i++) {\n    System.out.println(list.get(i));\n}","handlingStrategy":"validation","validationCode":"if (index < 0 || index >= list.size()) {\n    throw new IllegalArgumentException(\"bad index: \" + index);\n}\nint v = list.get(index);","typeGuard":null,"tryCatchPattern":"try {\n    int v = list.get(index);\n} catch (IndexOutOfBoundsException e) {\n    // log and recover; index was outside [0, size)\n}","preventionTips":["Always loop with `i < list.size()` and re-read size() if the list mutates.","Never substitute capacity() for size().","Centralize index validation in a helper to avoid scattered bounds bugs."],"tags":["java","array","bounds-check","data-structures","i18n-ja"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}