how do i stop this “IndexOutOfBoundsException” issue?

六眼飞鱼酱① 提交于 2019-12-13 09:59:57

问题


So I the app lets a user place down blocks on a grid, if the user lines up 3 or more blocks with the same suit, or color, then something happens. When player places a block I call this method:

   blocks_.add(new Block(new Vector2(rect_mouse.x, rect_mouse.y), blocks_.get(0).blockID, blockCount)); 

When you place 3 or more together I call these methods:

    blocks_.removeValue(blocks_.get(left_bravo_indexNum), true);
    blocks_.removeValue(blocks_.get(center_charlie_indexNum), true);
    blocks_.removeValue(blocks_.get(right_alpha_indexNum), true);


    stack:
    Exception in thread "LWJGL Application" java.lang.IndexOutOfBoundsException: 13
at com.badlogic.gdx.utils.Array.get(Array.java:125)
at com.jrp.mygearapp.GameScreen.touchUp(GameScreen.java:1443)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:297)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:186)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)

This was intended to remove the blocks, but it resulted in this IndexOutOfBoundsException. Is there a way to prevent this error?

This could be occurring because the array auto sorts the number of elements and lowers the number to the correct number of elements in the array, and I still have elements that are labeled higher then the size of the array. I am still a novice, so my analysis could be incorrect. Please alert me if this is the case and help me find a fix. Thanks.

edirted* TouchUp() function-------

   @Override
     public boolean touchUp(int x, int y, int pointer, int button) {

    if (button == 0)  {

        display_blockCheck = false;

        ////set blockCount to the size of blockArray so blocks can properly be indexed              
        blockCount = blocks_.size;

        if (!overlap) {

            Gdx.app.log("Block Added", "x: " + x + " y: " + y);

            updateQueueBlocks();


            //add block
            Vector2 rect_vector = new Vector2(rect_mouse.x, rect_mouse.y);
            Block block = new Block(rect_vector,blocks_.get(0).blockID, blocks_.size);

            blocks_.add(block);             

     if (center_charlie_suit == "Square") {

                center_charlie_bool = true;

                if (right_bravo_suit == "Square") {

                    right_bravo_bool = true;

                    if (right_alpha_suit == "Square") {

                        Gdx.app.log("3-pair", "Square:345:lr");

                        right_alpha_bool = true;

                        //call 3-pair event
                        blocks_.removeValue(blocks_.get(center_charlie_indexNum), true);
                        blocks_.removeValue(blocks_.get(right_alpha_indexNum), true);
                        blocks_.removeValue(blocks_.get(right_bravo_indexNum), true);
                        }
                }
            }

the rest is just really long and just checks for other blocks next to each other..


回答1:


You're right, as you remove the blocks, the indexes change.

You don't show what type of Collection blocks_ is (Vector2?, did you write it?), however, rather than tracking the indices of the elements, simply track the elements themselves and call remove() to find and remove that element.



来源:https://stackoverflow.com/questions/19128241/how-do-i-stop-this-indexoutofboundsexception-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!