简体   繁体   中英

Java Index Array Out of Bounds Exception

This loop is strange.

I ran this in Java, it gives an Index out of bounds exception. I can't find an int l declaration anywhere in the source code and i can't figure out what it is and found out that it is legal for it to be declared this way.

But the deal here is, i don't understand what this piece of code is doing. For any size of resultSIList , it gives an ArrayIndexOutOfBoundsException .

for (int i = offset, l = Math.min(i + maxItemsInOnePage, totalSIs); i < l; i++){
    resultSIList.get(i);
}

EDIT : Thanks all.

Here is a runnable code i am using to try to understand this entire loop. Yes it is a horrible piece of junk.

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();
        }
    }
}

This code loops 'resulSIList' from 'offset' position to the minimum of 'offset + maxItemsInOnePage' and 'totalSIs'.

If offset > 0 and totalSIs = resultSIList.size() I think that it won't give you a out of bounds exception.

In your example you have an offset of 50 and the list size it's only 15. You have to check that your offset is less than the list size.

to get some more info you could run it with some logging added:

System.out.println("offset: " + offset);
System.out.println("maxItemsInOnePage: " + maxItemsInOnePage);
System.out.println("totalSIs: " + totalSIs);
System.out.println("resultSIList.size(): " + resultSIList.size());
for (int i = offset, l = Math.min(i + maxItemsInOnePage, totalSIs); i < l; i++){
    System.out.println("i: " + i);
    resultSIList.get(i);
}

running this should shed some light on your problem; the last i that gets printed was out of bound and by analyzing the four first printouts, you should find your problem.

It works as expected if and only if totalSIs is equal to or less then the size of the resultSIList . Double check that value. Here's a working example with some random values:

List<Integer> resultSIList = Arrays.asList(1,2,3,4,5,6,7,8);
int totalSIs = resultSIList.size();
int maxItemsInOnePage = 2;
int offset = 1;

for (int i = offset, l = Math.min(i + maxItemsInOnePage, totalSIs); i < l; i++){
  resultSIList.get(i);
}

From you added code I see, that totalSIs is bigger (=50) then the list size (=15).

You could add

 totalSIs = resultsSIList.size();

right behind the for loop for a quick fix.

检查offset是否小于列表的大小。

You should always check whether your index i is less the size

for (int i = offset, l = Math.min(i + maxItemsInOnePage, totalSIs);
      i < l && i < resultSIList.size ();
      i++){
  resultSIList.get(i);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM