Java Index Array Out of Bounds Exception
<p>This loop is strange. </p>

<p>I ran this in Java, it gives an Index out of bounds exception. I can't find an <code>int l</code> 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.</p>

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

<pre><code>for (int i = offset, l = Math.min(i + maxItemsInOnePage, totalSIs); i &lt; l; i++){
    resultSIList.get(i);
}
</code></pre>

<p><strong>EDIT</strong>: Thanks all.</p>

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

<pre><code>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 &lt; buildThis; zz ++){
            resultSIList.add("Hi " + zz);
        }

        try{
            for (int i = offset,
                    d = Math.min(i + maxItemsInOnePage, totalSIs);
                    i &lt; d; i++){

               System.out.println(resultSIList.get(i));
           }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
</code></pre>
