I have a 2D array freeSpace[][]
that represents x
, y
coordinates. If the space is "not free" then I have it marked as 77
, other 1
.
I want to put all the elements marked as 77
into it's own array, with those particular array coordinates. I think it should be simple, but I just can't get the syntax correct.
Here is my code:
for (int v = 0; v < info.getScene().getHeight(); v++) {
for (int h = 0; h < info.getScene().getWidth(); h++) {
//System.out.print(freeSpace[h][v] != 77 ? "." : "#");
if (freeSpace[h][v] == 77) {
blockedCoordinates = new int[][]{{h, v}};
}
}
System.out.println();
}
I have already declared the blockedCoordinates[][]
array.
Most of my attempts have lead to an empty array.
You are doing some error while copying your data, here is why:
// assuming following definition
int[][] blockedCoordinate = new int[][]{};
for (int v = 0; v < info.getScene().getHeight(); v++) {
for (int h = 0; h < info.getScene().getWidth(); h++) {
//System.out.print(freeSpace[h][v] != 77 ? "." : "#");
if (freeSpace[h][v] == 77) {
// Make a copy
int[][] copyBlockedCoordinate = blockedCoordinates;
// extend the array length by 1
blockedCoordinates = new int[copyBlockedCoordinate.length + 1][2];
for (int i = 0; i < copyBlockedCoordinate.length; i++) {
for (int j = 0; j < copyBlockedCoordinate[i].length; j++) {
blockedCoordiante[i][j] = copyBlockedCoordinate[i][j];
}
}
// add new array at new or last index position in blockedCoordinate array
blockedCoordinate[copyBlockedCoordinate.length] = {h, v};
}
}
// Moake sure you write what you want to the console here to debug :)
System.out.println();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.