这是我遇到索引越界异常的代码,我不明白为什么,
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/