Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7700593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:44:41+00:00 2026-05-31T22:44:41+00:00

This loop is strange. I ran this in Java, it gives an Index out

  • 0

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();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:44:42+00:00Added an answer on May 31, 2026 at 10:44 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is very strange. itoa(); seems to create an infinite loop. for(int i =
Why was this loop introduced in java?Is it a java creation? What is its
/// \todo Loop must be rewritten ... /// \todo Delete this loop Can Doxygen
I'm trying to run this for loop; for (int col= 0; grid[0].length; col++) However
my app crashes often in this for-loop: for (int a = 0; a <=
While working on a WebSocket server in Java I came across this strange bug.
I am using a Bean Shell interpreter in a for loop like this for(int
the for loop: //for testing, this is usually a value of about 27 int
Can anyone tell me why I always have this strange output after running this
This loop checks if a record is in the sqlite database and builds a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.