此循环很奇怪。Java索引数组超出范围
我在Java中运行它,它给出了一个索引越界异常。我无法在源代码的任何地方找到一个int l
声明,我无法弄清楚它是什么,并发现这样声明是合法的。
但这里的交易是,我不明白这段代码在做什么。对于任何尺寸的resultSIList
,它都会给出ArrayIndexOutOfBoundsException
。
for (int i = offset, l = Math.min(i + maxItemsInOnePage, totalSIs); i < l; i++){
resultSIList.get(i);
}
编辑:感谢所有。
这是我用来试图理解这个整个循环的可运行代码。是的,这是一件可怕的垃圾。
public class IndexOutOfBoundsTest {
public static void main(String args[]){
int offset = 50;
int maxItemsInOnePage = 50;
int totalSIs = 50;
final int buildThis = 15;
List resultSIList = new ArrayList();
// build list
for(int zz = 0; zz < buildThis; zz ++){
resultSIList.add("Hi " + zz);
}
try{
for (int i = offset,
d = Math.min(i + maxItemsInOnePage, totalSIs);
i < d; i++){
System.out.println(resultSIList.get(i));
}
}catch(Exception e){
e.printStackTrace();
}
}
}
我有一个词给你:调试器。逐步了解行为如何不符合您的假设。 – duffymo 2012-03-28 09:23:20
你可以有一个消极的'抵消'? – 2012-03-28 09:24:01
resultSIList的大小是多少 – 2012-03-28 09:25:52