简体   繁体   中英

array index out of bounds exception for no reason

I am getting array index out of bounds exception at the last line of the prog, output[1][1] generates such an exception there. But the same thing remained normal in the previous loop.

{  
    int ict=66,total_slots=12,h1=15,index_count=65;
    String output[][] = new String[ict][total_slots+1];

    for (int x = 0; x < (ict); x++)
        output[x][0] = Integer.toString(x);
    for (int y = 0; y < (total_slots + 1); y++)
        output[0][y] = Integer.toString(y);
    for (int x = 1; x <= (index_count); x++ )
        for (int y = 1; y <= (total_slots); y++)
            output[x][y] = "n";

    for (int x=1; x <= h1; x++) {
        output[x][1]="y";//exception occurs here
        limit[x]++;
    }
}

Remember when you declare an array like String output[][]=new String[5][5]; then output[4][4] is the last element you can access since array indexes start at 0

you have declared your array as:

String output[][] = new String[ict][total_slots+1];

in your for loop you have

for( int x=1; x<=(ict); x++ )
    for( int y=1; y<=(total_slots); y++)
         output[x][y]="n";

On the very last iteration of the outer loop and the very first iteration of the inner loop, you are trying to access output[ict][0] which is what throws the exception. Remember that since 0 is the first index, ict - 1 will be the last valid index for the first dimension.

Try this:

String output[][] = new String[ict][total_slots]; //this is cleaner

for( int x=0; x<ict; x++ )
    for( int y=0; y<total_slots; y++)
         output[x][y]="n";

This way the last iteration of the outer loop would only go up to ict - 1

edit: It seems you have edited the question significantly. Please do not edit your question in the future in response to answers, because this will only confuse new readers.

As your code stands right now, the only error (compile time no less, irrelevant to exceptions) is the fact that limit[x]++; is invalid because limit has not been declared in this scope. otherwise the code is legal and runs fine.

    {  
        int ict=66,total_slots=12,h1=15,index_count=65;
..................................................
..................................................
        for (int x=1; x <= h1; x++) {
                output[x][1]="y";//exception occurs here
                limit[x]++;
            }
        }

At the last for loop, you recieve ArryIndexOutOfBoundsException , while trying to access the element output[13][1] cause your array width is only 13 ie max index will be 12 but not more than that

Use total_slots instead of h1

I am getting array index out of bounds exception at the last line of the prog, output[1][1] generates such an exception there.

Recheck the console output. You must have mistaken the erroring line, since there is no way to generate such an exception. The problem most probably lies in the limit[] , which we don't know how it's declared.

Btw, the last line is limit[x]++ ;)

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