中級 › ArrayIndexOutOfBoundsException の原因と直し方 › パターン3
ArrayIndexOutOfBoundsException の原因と直し方
添字が0始まりであることの帰結です。
1 public class Main { 2 public static void main(String[] args) { 3 int[] nums = new int[3]; 4 for (int i = ___________; i < nums.length; i++) { ^ 5 nums[i] = i; 6 } 7 System.out.println(nums[0]); 8 } 9 }
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
Index -1 out of bounds for length 3exited with code 1
空欄を埋めてビルドを通す
負の添字も範囲外です。Javaでは末尾から数える書き方はできません。
次の問題