java - ArrayIndexOutOfBound 异常

标签 java exception

这是我遇到索引越界异常的代码,我不明白为什么,

 int index = array.length - 1;
 E item = array[index];

  while (item == null && index >= 0) {
  index--;
  item = array[index];
  }

我收到 java.lang.ArrayIndexOutOfBoundsException: -1 at 项目=数组[索引]; 我不知道我哪里错了。有人可以帮忙吗?

最佳答案

while (item == null && index >= 0) {
  index--;
  item = array[index];
}

应该是

while (item == null && index >= 0) {
  item = array[index--];
}

关于java - ArrayIndexOutOfBound 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19392240/

相关文章:

java - 处理不可访问的异常类型

java - Apache Tika 和文件访问而不是 Java 输入流

java - 在 fragment 上实现向上导航

java - 当从另一个 Activity 调用时,Android 中的 Sharedpreferences 返回 null

Java 错误未报告的异常 IOException;必须被捕获或宣布被抛出

python - Eclipse PyDev - 我的构建很干净,但在运行时出现语法错误

java - Glassfish 中的服务器身份验证模块转发

java - 实现接口(interface)的名称冲突

Java PatternSyntaxException : Illegal repetition on string substitution?

c# - 如何在多层程序中重新抛出异常?