how do i fix this array issue?

夙愿已清 提交于 2019-12-13 00:26:57

问题


lemme try to explain whats happening now: when I place 3 "X's" next to each other I simply want to remove them, but its only happening part of the time, sometimes it removes all of them, and other times it removes just 1 or just 2 of them. The blocks are instances of the Block class being put in an arraylist.... Furthermore for a wider understanding: I'll randomly place blocks all over the grid and try to fill it, sometimes as im adding the blocks to the grid other blocks start disappearing. Also when I fill up most of the grid, I'll go ahead and start removing them, but as I start hovering over the blocks and right clicking them to delete them other blocks get deleted not the one im hovering over. As you can see the blockCount is the number that is returned when you hover your mouse over a block, in other words its the index number of the object within the arraylist(sometimes it doesn't seem like that though). Whats going on with the arraylist?

Heres the stackTrace of whats happening: obviously something to do with trying to remove an index that doesn't exist anymore right? what can I do? am i going about this wrong? This stacktrace is my Big problem wether I delete blocks individually or when I try and match 3 in a row, i get this Index bigger then size error.. plz help a lost soul....

     Exception in thread "LWJGL Application" java.lang.IndexOutOfBoundsException: Index: 21, Size: 14
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.remove(Unknown Source)
at com.jrp.mygearapp.GameScreen.touchUp(GameScreen.java:1176)
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 the TouchUp() method:

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

  if (button == 0)  {

     display_blockCheck = false;

     if (!overlap) {

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

        updateQueueBlocks();

        //add block

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

        for ( Block b : getBlocks_L()) {

           //check alpha/bravo cross intersections
           if (Intersector.overlapRectangles(b.getBounds(), left_bounds_alpha)) {

              //set indexNum, color, and suit to temporary variables for text info   

              //get block indexNum
              left_alpha_indexNum = b.indexNum;
              //get block color
              left_alpha_color =   b.color_str;
              //get block suit
              left_alpha_suit = b.suit_str;


           }

           if (Intersector.overlapRectangles(b.getBounds(), left_bounds_bravo)) {

              //get block indexNum
              left_bravo_indexNum = b.indexNum;
              //get block color
              left_bravo_color =   b.color_str;
              //get block suit
              left_bravo_suit = b.suit_str;


           }

           if (Intersector.overlapRectangles(b.getBounds(), right_bounds_alpha)) {

              //get block indexNum
              right_alpha_indexNum = b.indexNum;
              //get block color
              right_alpha_color =   b.color_str;
              //get block suit
              right_alpha_suit = b.suit_str;



           }

           if (Intersector.overlapRectangles(b.getBounds(), right_bounds_bravo)) {

              //get block indexNum
              right_bravo_indexNum = b.indexNum;
              //get block color
              right_bravo_color =   b.color_str;
              //get block suit
              right_bravo_suit = b.suit_str;



           }

           //center bounding box for cross checker
              if (Intersector.overlapRectangles(rect_mouse, b.getBounds())) {

                 //get block indexNum
                 center_charlie_indexNum = b.indexNum;
                 //get block color
                 center_charlie_color =   b.color_str;
                 //get block suit
                 center_charlie_suit = b.suit_str;

              }


           if (Intersector.overlapRectangles(b.getBounds(), top_bounds_alpha)) {

              //get block indexNum
              top_alpha_indexNum = b.indexNum;
              //get block color
              top_alpha_color =   b.color_str;
              //get block suit
              top_alpha_suit = b.suit_str;


           }

           if (Intersector.overlapRectangles(b.getBounds(), top_bounds_bravo)) {

              //get block indexNum
              top_bravo_indexNum = b.indexNum;
              //get block color
              top_bravo_color =   b.color_str;
              //get block suit
              top_bravo_suit = b.suit_str;


           }

           if (Intersector.overlapRectangles(b.getBounds(), bottom_bounds_alpha)) {

              //get block indexNum
              bottom_alpha_indexNum = b.indexNum;
              //get block color
              bottom_alpha_color =   b.color_str;
              //get block suit
              bottom_alpha_suit = b.suit_str;


           }

           if (Intersector.overlapRectangles(b.getBounds(), bottom_bounds_bravo)) {

              //get block indexNum
              bottom_bravo_indexNum = b.indexNum;
              //get block color
              bottom_bravo_color =   b.color_str;
              //get block suit
              bottom_bravo_suit = b.suit_str;



           }
 //X*******************************************************************************************
 if (left_alpha_suit == "X" && left_bravo_suit == "X" && center_charlie_suit == "X") {

           Gdx.app.log("3-pair", "X:123:lr");

           blocks_L.remove(left_alpha_indexNum);         
           blocks_L.remove(left_bravo_indexNum);      
           blocks_L.remove(center_charlie_indexNum -2);

           //blocks_.removeValue(blocks_.get(left_alpha_indexNum), true);            
           //blocks_.removeValue(blocks_.get(left_bravo_indexNum), true);         
           //blocks_.removeValue(blocks_.get(blockCount), true);            
            //blocks_.removeValue(blocks_.get(center_charlie_indexNum), true);   

        }

 if (left_bravo_suit == "X" && center_charlie_suit == "X" && right_bravo_suit == "X") {

           Gdx.app.log("3-pair", "X:234:lr");

           blocks_L.remove(left_bravo_indexNum);      
           blocks_L.remove(right_bravo_indexNum);
           blocks_L.remove(center_charlie_indexNum -2);

           /*blocksRemove_.add(blocks_.get(left_bravo_indexNum));
           blocksRemove_.add(blocks_.get(center_charlie_indexNum));
           blocksRemove_.add(blocks_.get(right_bravo_indexNum));

           blocks_.removeAll(blocksRemove_, true);*/
        }

 if (center_charlie_suit == "X" && right_bravo_suit == "X" && right_alpha_suit == "X") {

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

           blocks_L.remove(right_alpha_indexNum);
           blocks_L.remove(right_bravo_indexNum);
           blocks_L.remove(center_charlie_indexNum -2);      

           /*blocksRemove_.add(blocks_.get(center_charlie_indexNum));   
           blocksRemove_.add(blocks_.get(right_alpha_indexNum));
           blocksRemove_.add(blocks_.get(right_bravo_indexNum));   
           blocks_.removeAll(blocksRemove_, true);*/

        }
 }

 button01bool = false;

   }

 if (button == 1) {

     if (overlap) {

        //remove block      
        //blocks_.removeValue(blocks_.get(blockCount), true);      

        blocks_L.remove(blockCount);

     }

     button02bool = false;

  }

  return false;
}

this is the mouseMoved() method:

   @Override
   public boolean mouseMoved(int x, int y) {

  camera.unproject(tmp1.set(Gdx.input.getX(), Gdx.input.getY(), 0));

  mouseVec.x = tmp1.x ;
  mouseVec.y = tmp1.y ;

  rect_mouse.x = (int)mouseVec.x;
  rect_mouse.y = (int)mouseVec.y;


  for (Block b : getBlocks_L()) {


     if (Intersector.overlapRectangles(rect_mouse, b.getBounds())) {

        //mouse and block overlap is true                  
        overlap = true;

        //get block index
        blockCount = b.indexNum;

        tmpColorStr = b.color_str;
        tmpSuitStr = b.suit_str;

        break;

     } else {

        overlap = false;

        left_alpha_indexNum = 0;   
        left_alpha_color =   "";
        left_alpha_suit = "";

        left_bravo_indexNum = 0;   
        left_bravo_color =   "";
        left_bravo_suit = "";

        right_alpha_indexNum = 0;   
        right_alpha_color =   "";
        right_alpha_suit = "";

        right_bravo_indexNum = 0;
        right_bravo_color =   "";
        right_bravo_suit = "";

        center_charlie_color =   "";
        center_charlie_suit = "";
        center_charlie_indexNum = 0;

        top_alpha_indexNum = 0;   
        top_alpha_color =   "";
        top_alpha_suit = "";

        top_bravo_indexNum = 0;   
        top_bravo_color =   "";
        top_bravo_suit = "";

        bottom_alpha_indexNum = 0;   
        bottom_alpha_color =   "";
        bottom_alpha_suit = "";

        bottom_bravo_indexNum = 0;
        bottom_bravo_color =   "";
        bottom_bravo_suit = "";

     }




  }

回答1:


You shoul consider using Array which is provided by libgdx itself. It basically can do, what an ArrayList is able to, but is faster and more efficient.

Maybe you can explain, what the code should do? It's very hard to understand what you're trying to do by just reading your description of the problem.

Array can also remove an object by passing its index and by using the method

.removeIndex(int index);

Maybe this will help you.

EDIT:

You should programm everything like a way, that no array-out-of-bound exception can ever occur. Try a programm structure like this:

An example of how your block-class could look like:

public class Block {
   private int x,y;
   public Block(int x, int y) {
      this.x = x;
      this.y = y;
   }
   public boolean check(int x, int y) {
      if(this.x == x && this.y == y) {
         return true;
      } else {
         return false;
      }
   }
}

Then when a Mouseclick [leftclick] occured:

int x = ...;
int y = ...;
calculate here x and y values of the clicked block by using a formular ( don't use this slow overlay-stuff, when you're in a gridworld);
boolean blockAlreadyExists = false;
for(Block block: yourBlockArray) {
   blockAlreadyExists = block.check(x, y);
   if(blockAlreadyExists) {
      break; // you don't need to check the remaining blocks, aslong you know that the block with the given x and y values already exists
   }
}
if(!blockAlreadyExists) {
   yourBlockArray.add(new Block(x, y); // add the new block, when it's not already there
}

Then when a Mouseclick [rightclick] occured:

int x = ...;
int y = ...;
calculate your x and y values the same way as above;
Block blockToDelete;
for(Block block: yourBlockArray) {
   if(block.check(x, y)) {
      blockToDelete = block;
      break; // you don't need to check the remaining blocks, aslong you deleted the clicked block and there cannot exist another one with the same x and y values.
   }
}
if(blockToDelete != null) {
   yourBlockArray.remove(blockToDelete, true);
}

maybe this helps you :)



来源:https://stackoverflow.com/questions/19174823/how-do-i-fix-this-array-issue

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