简体   繁体   中英

java index out of bounds exception using lists

I made some code for bullets in a list and a list for mobs. I tried to make something that would detect collision and then delete both the bullet and the mob. However, I get a

java.lang.IndexOutOfBoundsException

at this line:

if(gmgr.bulletlist.get(i).x+x>=gmgr.moblist.get(mobnum).x&&gmgr.bulletlist.get(i).x+x<=gmgr.moblist.get(mobnum).x+32){

here is my full code:

for (int x = 0; x < gmgr.bulletlist.get(i).size; x++) {//size is the size of the bullet in pixels in this case 8
   for (int y = 0; y < gmgr.bulletlist.get(i).size; y++) {
       for (int mobnum = 0; mobnum < gmgr.moblist.size();//here size is the length of the list mobnum++) {
           if(gmgr.bulletlist.get(i).x+x>=gmgr.moblist.get(mobnum).x&&gmgr.bulletlist.get(i).x+x<=gmgr.moblist.get(mobnum).x+32){//here I take the position of the bullet and add and check for collision on every pixel
               if(gmgr.bulletlist.get(i).y+y>=gmgr.moblist.get(mobnum).y&&gmgr.bulletlist.get(i).y+y<=gmgr.moblist.get(mobnum).y+32){
                   gmgr.moblist.remove(mobnum);
                   mobnum-=1;//the problem is for as far as I know that I delete this while in this loop

                   gmgr.bulletlist.remove(i);
                   i-=1;
                   System.out.println("gotcha!!!!");//which means that the bullet hit the mob
               }
           }
       } 
   }
}

I need to find a way to remove these bullets. Any ideas to improve my code are also welcome

Either index i or mobnum is out of range. The most probable cause are these lines:

                       gmgr.bulletlist.remove(i);
                       i-=1;

Consider i==0 . You remove the item at position 0 and then set i-=1 , thus now i==-1 . Now in the next iteration of any of the three loops, you are trying to access gmgr.bulletlist.get(i) which will give the exception you posted.

code is kind of messy but:

gmgr.bulletlist.remove(i);
i-=1;

one thing here is that you keep iterating other mobs. so if it was bullet 0 next mob will look for bullet at index -1

to better find the issue, you should get the object beforehand, this will avoid index problem on removal and also give you a clear view of what is going on as currently the exception might refer to any list in the if.

also, use a flag to signal a hit, and skip to the next bullet

ie

boolean hit = false;
Bullet bullet = gmgr.bulletlist.get(i);
for (int x = 0; x < bullet.size; x++) {
    for (int y = 0; y < bullet .size; y++) {
        for (int mobnum = 0; mobnum < gmgr.moblist.size(); mobnum++) {
            Mob mob = gmgr.moblist.get(mobnum); 
            if(bullet.x+x>=mob.x && bullet.x+x<=mob.x+32){
                if(bullet.y+y>=mob.y&&bullet.y+y<=mob.y+32){
                    gmgr.moblist.remove(mobnum);
                    gmgr.bulletlist.remove(i);
                    hit = true;
                    break; //skip other mobs, bullet is invalid
                }
            }
         }
     if(hit) break; //skip other bullet pixel, bullet is invalid
     }
 if (hit) { //move to the next bullet, reset hit flag
    hit = false;
    break;
 } 
 }

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