【发布时间】:2017-04-21 06:05:03
【问题描述】:
以下代码将一直运行,直到抛出 IndexOutOfBoundExceptionstating h is -2147483648。
一开始h=0它只会递增,不会递减。怎么会变成负数?
float[] powerArray = new float[8760];
int h = 0;
for (TShortIterator it = heightData.iterator(); it.hasNext();) {
short wspeed = it.next();
if (h < 8760) {
powerArray[h] = Math.min(wspeed, 4);
}
h++;
}
【问题讨论】:
-
用这样的基于范围的循环进行迭代有点奇怪
-
这是一个质量很差的问题和答案。目前状态下,正确答案只是
because array index has to be between 0 and the array length, and -2147483648 is not -
原来的代码不是我写的,但要自己处理。
-
@Jhamon:把它写成答案,我会接受的。 :) 这个问题的目的是在搜索 indexoutoufboundexception 和 -2147483648 时得到一个结果。
标签: java for-loop indexoutofboundsexception