krahets/hello-algo · error · IndexOutOfBoundsException
インデックスが範囲外です
Error message
インデックスが範囲外です
What it means
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'.
Source
Thrown at ja/codes/java/chapter_array_and_linkedlist/my_list.java:37
public MyList() {
arr = new int[capacity];
}
/* リストの長さを取得(現在の要素数) */
public int size() {
return size;
}
/* リスト容量を取得する */
public int capacity() {
return capacity;
}
/* 要素にアクセス */
public int get(int index) {
// インデックスが範囲外なら例外を送出する。以下同様
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("インデックスが範囲外です");
return arr[index];
}
/* 要素を更新 */
public void set(int index, int num) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("インデックスが範囲外です");
arr[index] = num;
}
/* 末尾に要素を追加 */
public void add(int num) {
// 要素数が容量を超えると、拡張機構が発動する
if (size == capacity())
extendCapacity();
arr[size] = num;
// 要素数を更新
size++;View on GitHub (pinned to 69932aed18)
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.
Example fix
// before
for (int i = 0; i <= list.capacity(); i++) {
System.out.println(list.get(i)); // IndexOutOfBoundsException when i >= size
}
// after
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
} Defensive patterns
Strategy: validation
Validate before calling
if (index < 0 || index >= list.size()) {
throw new IllegalArgumentException("bad index: " + index);
}
int v = list.get(index); Try / catch
try {
int v = list.get(index);
} catch (IndexOutOfBoundsException e) {
// log and recover; index was outside [0, size)
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/a39728ce94c0691a.
Report an issue: GitHub.