krahets/hello-algo · error · IndexOutOfRangeException
インデックスが範囲外です
Error message
インデックスが範囲外です
What it means
IndexOutOfRangeException with message "インデックスが範囲外です" (Japanese for "index is out of range") is thrown by MyList.Get(int index) when index < 0 or index >= arrSize. This is the Japanese-localized equivalent of the English my_list.cs; the logic is identical.
Source
Thrown at ja/codes/csharp/chapter_array_and_linkedlist/my_list.cs:35
public MyList() {
arr = new int[arrCapacity];
}
/* リストの長さを取得(現在の要素数) */
public int Size() {
return arrSize;
}
/* リスト容量を取得する */
public int Capacity() {
return arrCapacity;
}
/* 要素にアクセス */
public int Get(int index) {
// インデックスが範囲外なら例外を送出する。以下同様
if (index < 0 || index >= arrSize)
throw new IndexOutOfRangeException("インデックスが範囲外です");
return arr[index];
}
/* 要素を更新 */
public void Set(int index, int num) {
if (index < 0 || index >= arrSize)
throw new IndexOutOfRangeException("インデックスが範囲外です");
arr[index] = num;
}
/* 末尾に要素を追加 */
public void Add(int num) {
// 要素数が容量を超えると、拡張機構が発動する
if (arrSize == arrCapacity)
ExtendCapacity();
arr[arrSize] = num;
// 要素数を更新
arrSize++;View on GitHub (pinned to 69932aed18)
Solutions
- Validate 0 <= index < list.Size() before calling Get().
- Fix loop bounds to use < Size() rather than <= Size().
- Clamp or reject out-of-range indices from external input.
Example fix
// before
MyList list = new();
list.Add(10);
int val = list.Get(1); // throws
// after
if (index >= 0 && index < list.Size())
int val = list.Get(index); Defensive patterns
Strategy: validation
Validate before calling
if (index >= 0 && index < list.Size()) {
int val = list.Get(index);
} Try / catch
try {
int val = list.Get(index);
} catch (IndexOutOfRangeException ex) when (ex.Message.Contains("範囲外")) {
// index out of bounds — handle
} Prevention
- Use i < list.Size() in loops, never i <= list.Size().
- Validate externally-sourced indices before calling Get().
- When catching by message, match the localized string "インデックスが範囲外です".
When it happens
Trigger: Calling Get(index) where index < 0 or index >= list.Size(). On a newly constructed MyList (arrSize 0), any Get call throws.
Common situations: Off-by-one loop bounds, unvalidated external index input, or reading from a list before populating it. Japanese-speaking developers may search for the localized message string.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/8a960d23bd3e07ce.
Report an issue: GitHub.