中級 › ArrayIndexOutOfBoundsException の原因と直し方 › パターン5
ArrayIndexOutOfBoundsException の原因と直し方
添字が0始まりであることの帰結です。
1 public class Main { 2 public static void main(String[] args) { 3 int[] nums = new int[___________]; ^ 4 System.out.println(nums[0]); 5 } 6 }
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
Index 0 out of bounds for length 0exited with code 1
空欄を埋めてビルドを通す
サイズ0の配列には要素が1つもありません。使う前に配列の大きさを確認する習慣が大切です。
次の問題