java - 为什么这段代码会导致 ArrayIndexOutOfBoundsException?
问题描述
我试图了解为什么我的代码会导致 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");
}
}
解决方案
问题是数组的索引从 0 开始。给定数组大小为 5,数组的最后一个元素是 x[4](第一个元素是 x[0])
推荐阅读
- c# - 如何在 C# 中正确构建项目以包含 tessnet2?
- spring-boot - 如何 JUnit MapperToJsonString 类
- javascript - JavaScript 触发太多事件?
- java - Java 通用整数数据与原始 int 数据
- python-3.x - 如何使用 selenium 和 python 单击下拉菜单项?
- microsoft-graph-api - Microsoft Graph:如何从电子邮件中获取别名?
- r - 如何在回归模型中保留分类变量的某些值
- flutter - Flutter Webview Google OAuth
- r - RSelenium,Chrome,如何设置下载目录,文件下载错误
- django - Django Admin 编辑相关自定义用户模型