gpt4 book ai didi

java - 我该如何解决这个数组问题?

转载 作者:行者123 更新时间:2023-12-01 04:18:58 25 4
gpt4 key购买 nike

让我尝试解释一下现在发生的事情:当我将 3 个“X”彼此相邻放置时,我只是想删除它们,但它只在部分时间发生,有时它会删除所有它们,而其他时候它会删除它们只有其中 1 个或 2 个。这些 block 是放入数组列表中的 Block 类的实例......此外,为了更广泛的理解:我将在网格上随机放置 block 并尝试填充它,有时当我将 block 添加到网格时其他 block 开始消失。另外,当我填满大部分网格时,我将继续并开始删除它们,但是当我开始将鼠标悬停在 block 上并右键单击它们以删除它们时,其他 block 会被删除,而不是我悬停的 block 。正如您所看到的,blockCount 是当您将鼠标悬停在 block 上时返回的数字,换句话说,它是数组列表中对象的索引号(但有时看起来并非如此)。数组列表怎么了?

这是所发生事情的堆栈跟踪:显然与尝试删除不再存在的索引有关,对吧?我能做些什么?我会做错事吗?这个堆栈跟踪是我的大问题,无论我单独删除 block 还是当我尝试连续匹配 3 个 block 时,我都会得到这个索引大于大小错误..请帮助迷失的灵魂....

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

}




}

最佳答案

您应该考虑使用libgdx本身提供的数组。它基本上可以做 ArrayList 能够做的事情,但更快、更高效。

也许你可以解释一下,代码应该做什么?仅通过阅读您对问题的描述很难理解您想要做什么。

数组还可以通过传递索引并使用方法来删除对象

.removeIndex(int index);

也许这会对你有所帮助。

编辑:

您应该以一种不会发生数组越界异常的方式对所有内容进行编程。尝试这样的程序结构:

block 类的示例:

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;
}
}
}

然后当鼠标单击[左键单击]发生时:

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
}

然后当鼠标单击[右键单击]发生时:

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);
}

也许这对你有帮助:)

关于java - 我该如何解决这个数组问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19174823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com