2

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.

1
  • 1
    For each not free space you’re completely declaring a new blockedCoordinates array, overwriting anything previously there Can you give a small example of a grid (say, 3x3) and what you expect the blockedCoordinates array to look like afterwards?Additionally, for only two states, a boolean would suffice, or better yet, an enum So more can be added easily later. Commented Jun 9, 2021 at 1:13

1 Answer 1

1

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};
        }
    }
    // Make sure you write what you want to the console here to debug :)
    System.out.println();
}
Sign up to request clarification or add additional context in comments.

7 Comments

I get a new array which I have tested with blockedCoordinates.length, that seems to work (not sure if it copies the coordinates? Trying to figure out how to test that). I think it works tho. I had to comment the line blockedCoordinate[copyBlockedCoordinate] = {h, v}; because it was giving me a type error. Not sure what this does? Does it copy the coordinate location, to the next matrix -- in which case, it would not work, gonna see if I can figure out a more proper syntax for that line.
You´re right, my bad, at that place i forgot. to add attribute .length at that line :)
I also fixed it now, make sure to check it and mark it as answer.
I was getting an error with the second part, "Arrays constants can only be used in initializers error" I fixed by editing the line: blocksArray[copyBlockedCoordinate.length] = new int[]{h, v}; It all seems to work, at least better than before, gotta keep working it. but I will mark it as answered for now =)
After a couple days off this topic, I have returned to it. I am still getting an error, particularly on this line. Could someone explain to me what is happening, beyond the comment? =) // add new array at new index position blocksArray[blocksArray.length] = new int[]{xPixels, yPixels}; I am getting an error: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1. I am researching that now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.