简体   繁体   English

将一个二维数组的坐标复制到另一个数组

[英]Copy coordinates of one 2D array to another array

I have a 2D array freeSpace[][] that represents x , y coordinates.我有一个代表xy坐标的二维数组freeSpace[][] If the space is "not free" then I have it marked as 77 , other 1 .如果空间是“不空闲的”,那么我将它标记为77 ,其他1

I want to put all the elements marked as 77 into it's own array, with those particular array coordinates.我想将所有标记为77的元素放入它自己的数组中,并带有这些特定的数组坐标。 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.我已经声明了blockedCoordinates[][]数组。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM