【发布时间】:2023-03-31 10:55:02
【问题描述】:
我试图从二维数组中删除一行。但是我在google上没有找到有用的东西,我发现的每一个都是关于从ArrayList中删除的。
这是我的二维数组:
int[][] Dirt = {{4, 2}, {0, 5}, {7, 1}, {3, 3}};
例如我想删除 {0,5} 在这里它必须从 Dirt 中删除该行。
for (int z = 0; z < 3; z++) {
if(pos[0]==Dirt[z][0]&& pos[1]==Dirt[z][1]){
System.out.println("Dirt[z][0]"+Dirt[z][0] +"Dirt[z][1]"+Dirt[z][1]);
//here it must remove the row from the Dirt
}
}
有什么建议吗?
我试图将数组复制到新的,但也不起作用
这是输出:
[ ][ ][ ][ ][ ][ ][ ][ ] 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 3 [ ][ ][ ][ ][ ][ ][ ][ ] [ ][ ][ ][ ][ ][ ][ ][ ] [ ][ ][ ][ ][ ][ ][ ][ ] [ ][ ][ ][ ][ ][ ][ ][ ] [ ][ ][ ][ ][ ][ ][ ][ ] [ ][ ][ ][ ][ ][ ][ ][ ] [ ][ ][ ][ ][ ][ ][ ][ ]
在 test.Move(test.java:121) 处选择等于 5:4 5:4 nowX 和 nowY 的味道 在 test.main(test.java:149) Java Result: 1 BUILD SUCCESSFUL (total 时间:3秒)
这是我的代码:
int[][] Dirt = {{4, 2}, {0, 5}, {7, 1}, {3, 3}};
int[][] temp = Dirt;
int size = Dirt.length - 1;
public void Move() {
Collections.shuffle(orientation);
int nowX = 4, nowY = 4;
int counter = 0;
int strightCounter = 1;
Dirt = new int[size][2];
int newIndex = 0;
int x = 0;
int y = 0;
while (!(Dirt.length == 0)) {
for (int i = 0; i < 3; i++) {
choices.add(orientation.get(i));
board[nowX][nowY] = "1";
int[] pos = gostright(nowX, nowY);
nowX = pos[0];
nowY = pos[1];
System.out.println("" + nowX + ":" + nowY);
System.out.println("nowX and nowY" + board[nowX][nowY]);
board[nowX][nowY] = "#";
moves++;
for (int z = 0; z < temp.length; z++) {
int[] oneDimension = temp[z];
if (pos[0] == oneDimension[0] && pos[1] == oneDimension[1]) {
continue;
}else{
Dirt[newIndex][0] = oneDimension[0];
Dirt[newIndex][1] = oneDimension[1];
newIndex++;
}
}
System.out.println("The array After removing : ");
for (int s = 0; s < Dirt.length; s++) {
System.out.println(Dirt[s][0] + "," + Dirt[s][1]);
}
System.out.println(toString());
System.out.println(orientation.get(i));
System.out.println("Choices " + choices);
System.out.println("# move" + moves);
if (Wall(nowX, nowY)) {
break;
}
}
counter++;
}
}
any suggestion ??
【问题讨论】:
-
我试过这个:但也报错: