此循環很奇怪。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