【发布时间】:2019-05-14 08:26:57
【问题描述】:
我试图了解为什么我的代码会导致 ArrayIndexOutOfBoundsException。谁能给我解释一下?
public class Test {
final static int x[] = new int[5];
public static void main(String[] args) {
final int x = new Test().x[5];
if (x <= 10)
System.out.println("javachamp");
}
}
【问题讨论】:
-
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? 的可能重复项。那里接受的答案解释了你更深入地看到的问题,但基本上 - 你的数组的索引是:0、1、2、3、4,而不是:1、2、3、4、5。所以第一个元素是
x[0],最后一个是x[4]。没有x[5]。
标签: java arrays indexoutofboundsexception