Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

java - how do i stop this "IndexOutOfBoundsException" issue?

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..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...