Reputation: 21
I tried to remove a row from 2 dimensional arrays. But I didn't find something useful searching google, every thing I find is about remove from ArrayList
.
This is my 2 dimensional array :
int[][] Dirt = {{4, 2}, {0, 5}, {7, 1}, {3, 3}};
and for example I want to remove {0,5} here it must remove the row from the 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
}
}
Any suggestion ?
I tried to copry the arryay to new one,, But also not work
this is the output:
[ ][ ][ ][ ][ ][ ][ ][ ] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 [ ][ ][ ][ ][ ][ ][ ][ ] [ ][ ][ ][ ][ ][ ][ ][ ] [ ][ ][ ][ ][ ][ ][ ][ ] [ ][ ][ ][ ][ ][ ][ ][ ] [ ][ ][ ][ ][ ][ ][ ][ ] [ ][ ][ ][ ][ ][ ][ ][ ] [ ][ ][ ][ ][ ][ ][ ][ ]
choise taste equal5:4 5:4 nowX and nowY at test.Move(test.java:121) at test.main(test.java:149) Java Result: 1 BUILD SUCCESSFUL (total time: 3 seconds)
and This my code:
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 ??
Upvotes: 1
Views: 4971
Reputation: 425003
You would have to replace the array with a new one. Here's 4 lines that will do it:
// original array
int[][] dirt = { { 4, 2 }, { 0, 5 }, { 7, 1 }, { 3, 3 } };
int z = 1; // the element to remove
// create a new smaller array
int[][] dirt2 = new int[dirt.length - 1][];
// copy everything from 0 to z
System.arraycopy(dirt, 0, dirt2, 0, z);
// copy from z+1 to end
System.arraycopy(dirt, z + 1, dirt2, z, dirt.length - z - 1);
// reassign
dirt = dirt2;
Upvotes: 0
Reputation: 622
Java array is immutable object.
You want to remove or to add items, you have to regenerate array object. If adding and removing items flexibly, I also suggest to use Collection Objects such as ArrayList , HashMap and etc.
Upvotes: -1
Reputation: 834
Since you can't change the size of an array, perhaps it would be better if you create another array and only insert the elements you need
Upvotes: 1
Reputation: 24375
In Java arrays are static in size, meaning that you cannot change the size of the array once it has been created. That is the reason you have only found answers relating to ArrayLists.
Upvotes: 2
Reputation: 51030
You are trying to achieve something that's impossible.
Arrays are fixed so once created you can not modify their size.
You can create that effect of removing a row by creating a new array with one less row dynamically.
Upvotes: 0