java如何解决此阵列问题?
让我试着解释一下现在发生了什么:当我把3个“X”放在彼此旁边时,我只是想删除它们,但这只是部分时间发生的,有时它会删除所有它们,而有时它只删除1个或2个。这些块是放置在arraylist中的块类的实例。。。。 此外,为了更广泛的理解:我会在网格上随机放置块,并尝试填充它,有时当我将块添加到网格中时,其他块开始消失。另外,当我填满大部分网格时,我会继续并开始删除它们,但当我开始将鼠标悬停在块上并右键单击它们以删除它们时,其他块将被删除,而不是我悬停在上面的块。正如您所看到的,blockCount是当您将鼠标悬停在块上时返回的数字,换句话说,它是arraylist中对象的索引号(尽管有时看起来不是这样)。arraylist怎么了
下面是发生了什么的stackTrace:很明显,这与试图删除一个不再存在的索引有关,对吗?我能做什么?我做错了吗?这个stacktrace是我的大问题,无论我是单独删除块,还是当我尝试在一行中匹配3时,我都会得到比大小错误更大的索引。。请帮助一个迷失的灵魂
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)
这是TouchUp()方法:
@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;
}
这是mouseMoved()方法:
@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 楼答案
你应该考虑使用LIGBDX本身提供的数组。它基本上可以做到ArrayList所能做到的,但速度更快,效率更高
也许你可以解释一下,代码应该做什么?仅仅通过阅读你对问题的描述就很难理解你想做什么
数组还可以通过传递对象的索引和使用
也许这对你有帮助
编辑:
您应该以一种不会发生数组越界异常的方式对所有内容进行编程。尝试以下程序结构:
块类的外观示例如下:
然后,当鼠标单击[左键单击]时:
然后,当鼠标单击[右键单击]时:
也许这对你有帮助:)