Code Fix

中級ArrayIndexOutOfBoundsException の原因と直し方 › パターン2

ArrayIndexOutOfBoundsException の原因と直し方

添字が0始まりであることの帰結です。

 1  public class Main {
 2      public static void main(String[] args) {
 3          int[] nums = {10, 20, 30};
 4          System.out.println(nums[_______________]);
                                           ^
 5      }
 6  }
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3exited with code 1

空欄を埋めてビルドを通す

広告
広告スロット(未設定)

ArrayIndexOutOfBoundsException の原因と直し方

ループ条件の < と <= の使い分けを整理します。

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)