如何解决这个数组问题? [英] how do i fix this array issue?

查看:102
本文介绍了如何解决这个数组问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还是让我试图解释什么,现在发生的事情:当我把3X的相邻,我只是想删除它们,但它只是发生在部分时间,有时它会删除所有这些,和其他时间删除只有1或只是其中2。该块被放在一个ArrayList块类的实例....
而且,对于更广泛的理解:我会随意放置块遍布电网,并尝试以填补它,有时作为即时通讯添加块到网格中的其他块开始消失。此外,当我补了大部分的网格,我会继续前进,开始删除它们,但我开始盘旋在块和右键单击他们将其删除其它块被删除而不是一个IM上空盘旋。正如你所看到的blockCount是,当你将鼠标悬停在一个块其在ArrayList中的对象的索引号返回,换句话说数(有时它似乎并不喜欢,虽然)。怎么回事的数组列表?

继承人的发生了什么的堆栈跟踪:明明是与试图删除不存在了正确的索引?我能做什么?我要对这个错误?这堆栈跟踪是我阉我单独删除块大问题,或者当我尝试和连续比赛3,我得到这个指数更大然后尺寸误差.. plz帮助丢了魂......

 异常螺纹LWJGL应用jav​​a.lang.IndexOutOfBoundsException:指数:21,尺寸:14
在java.util.ArrayList.rangeCheck(来源不明)
在java.util.ArrayList.remove(来源不明)
在com.jrp.mygearapp.GameScreen.touchUp(GameScreen.java:1176)
在com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:297)
在com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:186)
在com.badlogic.gdx.backends.lwjgl.LwjglApplication $ 1.run(LwjglApplication.java:110)

这个润色()方法:

  @覆盖
  公共布尔润色(INT X,INT Y,INT指针,INT按钮){  如果(键== 0){     display_blockCheck = FALSE;     如果(!重叠){        Gdx.app.log(块增加,X+ X +Y:+ Y);        updateQueueBlocks();        //加挡        blocks_L.add(新块(新Vector2(rect_mouse.x,rect_mouse.y),blocks_.get(0).blockID,blocks_L.size(),0,0));        为(b座:getBlocks_L()){           //检查阿尔法/布拉沃交叉的十字路口
           如果(Intersector.overla prectangles(b.getBounds(),left_bounds_alpha)){              //设置indexNum,颜色和西装文本信息临时变量              // get块indexNum
              left_alpha_indexNum = b.indexNum;
              // get块颜色
              left_alpha_color = b.color_str;
              // get块套装
              left_alpha_suit = b.suit_str;
           }           如果(Intersector.overla prectangles(b.getBounds(),left_bounds_bravo)){              // get块indexNum
              left_bravo_indexNum = b.indexNum;
              // get块颜色
              left_bravo_color = b.color_str;
              // get块套装
              left_bravo_suit = b.suit_str;
           }           如果(Intersector.overla prectangles(b.getBounds(),right_bounds_alpha)){              // get块indexNum
              right_alpha_indexNum = b.indexNum;
              // get块颜色
              right_alpha_color = b.color_str;
              // get块套装
              right_alpha_suit = b.suit_str;           }           如果(Intersector.overla prectangles(b.getBounds(),right_bounds_bravo)){              // get块indexNum
              right_bravo_indexNum = b.indexNum;
              // get块颜色
              right_bravo_color = b.color_str;
              // get块套装
              right_bravo_suit = b.suit_str;           }           交叉检查//中心边框
              如果(Intersector.overla prectangles(rect_mouse,b.getBounds())){                 // get块indexNum
                 center_charlie_indexNum = b.indexNum;
                 // get块颜色
                 center_charlie_color = b.color_str;
                 // get块套装
                 center_charlie_su​​it = b.suit_str;              }
           如果(Intersector.overla prectangles(b.getBounds(),top_bounds_alpha)){              // get块indexNum
              top_alpha_indexNum = b.indexNum;
              // get块颜色
              top_alpha_color = b.color_str;
              // get块套装
              top_alpha_suit = b.suit_str;
           }           如果(Intersector.overla prectangles(b.getBounds(),top_bounds_bravo)){              // get块indexNum
              top_bravo_indexNum = b.indexNum;
              // get块颜色
              top_bravo_color = b.color_str;
              // get块套装
              top_bravo_suit = b.suit_str;
           }           如果(Intersector.overla prectangles(b.getBounds(),bottom_bounds_alpha)){              // get块indexNum
              bottom_alpha_indexNum = b.indexNum;
              // get块颜色
              bottom_alpha_color = b.color_str;
              // get块套装
              bottom_alpha_suit = b.suit_str;
           }           如果(Intersector.overla prectangles(b.getBounds(),bottom_bounds_bravo)){              // get块indexNum
              bottom_bravo_indexNum = b.indexNum;
              // get块颜色
              bottom_bravo_color = b.color_str;
              // get块套装
              bottom_bravo_suit = b.suit_str;           }
 //X*********************************************** ********************************************
 如果(left_alpha_suit ==X&放大器;&放大器; left_bravo_suit ==X&放大器;&放大器; center_charlie_su​​it ==X){           Gdx.app.log(3-对,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);        } 如果(left_bravo_suit ==X&放大器;&放大器; center_charlie_su​​it ==X&放大器;&放大器; right_bravo_suit ==X){           Gdx.app.log(3-对,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_,真实); * /
        } 如果(center_charlie_su​​it ==X&放大器;&放大器; right_bravo_suit ==X&放大器;&放大器; right_alpha_suit ==X){           Gdx.app.log(3-对,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_,真实); * /        }
 } button01bool = FALSE;   } 如果(按钮== 1){     如果(重叠){        //删除块
        //blocks_.removeValue(blocks_.get(blockCount),TRUE);        blocks_L.remove(blockCount);     }     button02bool = FALSE;  }  返回false;
}

这是的mouseMoved()方法:

  @覆盖
   公共布尔的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;
  为(b座:getBlocks_L()){
     如果(Intersector.overla prectangles(rect_mouse,b.getBounds())){        //鼠标和块重叠是真
        重叠= TRUE;        // get块指数
        blockCount = b.indexNum;        tmpColorStr = b.color_str;
        tmpSuitStr = b.suit_str;        打破;     }其他{        重叠= 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_su​​it =;
        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 =;     }
  }


解决方案

您建议立即进行删除考虑使用它是由libgdx本身提供的数组。它基本上可以做到,一个ArrayList是可以的,但速度更快,效率更高。

也许你能解释的那样,code应该做些什么呢?这是很难理解你想仅仅阅读你的问题的说明做什么。

阵列也可以通过传递其索引并且通过使用该方法删除对象

  .removeIndex(INT指数);

也许这将帮助你。

编辑:

您应该PROGRAMM一切都像一个办法,都不能出现任何阵列外的约束例外。尝试PROGRAMM结构是这样的:

你的块级如何看起来像一个例子:

 公共类模块{
   私人诠释的x,y;
   公共块(INT X,int y)对{
      this.x = X;
      this.y = Y;
   }
   公共布尔检查(INT X,int y)对{
      如果(this.x == X'放大器;&安培; this.y == Y){
         返回true;
      }其他{
         返回false;
      }
   }
}

然后,当鼠标点击一个[leftclick]发生:

  INT X = ...;
INT Y = ...;
通过使用一个公式推计算这里的点击块的x和y值(不要使用这种缓慢叠加-的东西,当你在一个gridworld);
布尔blockAlreadyExists = FALSE;
对于(block块:yourBlockArray){
   blockAlreadyExists = block.check(X,Y);
   如果(blockAlreadyExists){
      打破; //你不需要检查剩余区块,aslong你知道,给定x和y的值块已存在
   }
}
如果(!blockAlreadyExists){
   yourBlockArray.add(新块(X,Y); //添加新的模块,当它尚不存在
}

然后,当鼠标点击一个[单击鼠标右键]发生:

  INT X = ...;
INT Y = ...;
计算的x和y值与上述相同的方式;
块blockToDelete;
对于(block块:yourBlockArray){
   如果(block.check(X,Y)){
      blockToDelete =块;
      打破; //你不需要检查剩余区块,aslong您删除点击块,并有可能不存在另一个具有相同x和y的值。
   }
}
如果(blockToDelete!= NULL){
   yourBlockArray.remove(blockToDelete,真);
}

也许这可以帮助你:)

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 = "";

     }




  }

解决方案

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 :)

这篇关于如何解决这个数组问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆