Android java将2d数组分配给3d数组

发布于 2024-10-10 13:59:29 字数 1874 浏览 0 评论 0原文

我在尝试将 2d 数组分配给 3d 数组时遇到问题,所以我想我会问一个有关 3d 和 2d 数组的问题。

假设我有一个 masterArray[][][] 并想将 childArray1[][] 和 childArray2[][] 放入其中。 这就是我所做的,并且想知道这是否是应用它的正确方法:

private int[][][] masterArray;
private int[][] childArray1 = {
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 0, 0, 1, 0, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
        {1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
        {1, 0, 1, 1, 0, 1, 0, 1, 0, 1},
        {1, 0, 1, 1, 0, 1, 1, 1, 0, 1},
        {1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
        {1, 0, 1, 1, 0, 1, 1, 1, 0, 1},
        {1, 0, 1, 1, 0, 1, 8, 1, 0, 1},
        {1, 0, 7, 1, 1, 1, 0, 1, 0, 1},
        {1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
        {1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 9, 0, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
private int[][] childArray2 = {
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
        {1, 1, 1, 1, 7, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 0, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 0, 0, 0, 1, 1, 1},
        {1, 1, 1, 9, 1, 1, 8, 0, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};

好的,所以在我的 init 方法中,我使用这些方法将子数组设置到主数组中。我很好奇这究竟是如何运作的。我假设了以下内容:

    masterLevel = new int[MAX_LEVELS][MAP_WIDTH][MAP_HEIGHT];
    for (int x = 0; x < MAP_WIDTH; x++) {
        for (int y = 0; y < MAP_HEIGHT; y++) {
            masterArray[currentLevel][x][y] = childArray1[x][y];
        }
    }

这可行吗? 在我的应用程序中,事情不起作用,所以我挑选了我不能 100% 确定的代码。

I'm running into problems trying to assign a 2d array to a 3d array, so I thought i'd ask a question about 3d and 2d arrays.

Say I have a masterArray[][][] and wanted to put childArray1[][] and childArray2[][] into it.
This is how I have done it and was wondering if that is the correct way of applying it:

private int[][][] masterArray;
private int[][] childArray1 = {
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 0, 0, 1, 0, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
        {1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
        {1, 0, 1, 1, 0, 1, 0, 1, 0, 1},
        {1, 0, 1, 1, 0, 1, 1, 1, 0, 1},
        {1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
        {1, 0, 1, 1, 0, 1, 1, 1, 0, 1},
        {1, 0, 1, 1, 0, 1, 8, 1, 0, 1},
        {1, 0, 7, 1, 1, 1, 0, 1, 0, 1},
        {1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
        {1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 9, 0, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
private int[][] childArray2 = {
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
        {1, 1, 1, 1, 7, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 0, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 0, 0, 0, 1, 1, 1},
        {1, 1, 1, 9, 1, 1, 8, 0, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};

Ok, so in my init method I use these some methods to set the child arrays into the master array. What I was curious about was how this exactly works. I assumed the following:

    masterLevel = new int[MAX_LEVELS][MAP_WIDTH][MAP_HEIGHT];
    for (int x = 0; x < MAP_WIDTH; x++) {
        for (int y = 0; y < MAP_HEIGHT; y++) {
            masterArray[currentLevel][x][y] = childArray1[x][y];
        }
    }

Would that work?
In my application things aren't working so I picking out code that I am not 100% sure on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南巷近海 2024-10-17 13:59:29

只要以同样的方式放置和取出内容,如何组织 3D 数组并不重要。

从您对另一个答案的评论来看,您似乎遇到了元素顺序问题 ([currentLevel][x][y] = childArray[y][x];)

看来您混合了 MAP_HEIGHT 和 MAP_WIDTH 。应该是:

masterLevel = new int[MAX_LEVELS][MAP_HEIGHT][MAP_WIDTH];

那么你可以使用:

master[currentLevel][x][y] = childArray[x][y];

It doesn't really matter how you organize a 3d array as long as you put things in the same way as you take them out.

From your comment on another answer it seems that you are having problem with element order ([currentLevel][x][y] = childArray[y][x];)

It seems you mixed MAP_HEIGHT and MAP_WIDTH. It should be:

masterLevel = new int[MAX_LEVELS][MAP_HEIGHT][MAP_WIDTH];

then you can use:

master[currentLevel][x][y] = childArray[x][y];
梦里梦着梦中梦 2024-10-17 13:59:29

在Java中,多维数组实际上是数组的数组。所以它们甚至可以是脱节的。
在您发布的代码中,您引用了一个您未定义的名为 currentLevel 的变量。我确信这是在您未发布的某些代码中定义的。另外不要忘记数组的索引为零。这段代码应该可以工作。

masterArray = new int[MAX_LEVELS][MAP_WIDTH][MAP_HEIGHT];
for (int currentLevel = 0; currentLevel < MAX_LEVELS; currentLevel++) {
    for (int x = 0; x < MAP_WIDTH; x++) {
        for (int y = 0; y < MAP_HEIGHT; y++) {
                masterArray[currentLevel][x][y] = childArray1[x][y];
            }
        }
    }

如果您曾经使用过大型数组并且需要速度,那么您可以查看 System.arrayCopy();

In Java multi-d arrays are actually arrays of arrays. So they can even be disjoint.
In the code you posted you refer to a variable called currentLevel that you did not define. I am sure that is defined in some code you did not post. Also don't forget that arrays are zero index. This code should work.

masterArray = new int[MAX_LEVELS][MAP_WIDTH][MAP_HEIGHT];
for (int currentLevel = 0; currentLevel < MAX_LEVELS; currentLevel++) {
    for (int x = 0; x < MAP_WIDTH; x++) {
        for (int y = 0; y < MAP_HEIGHT; y++) {
                masterArray[currentLevel][x][y] = childArray1[x][y];
            }
        }
    }

If you ever work with massive arrays and need speed then you could look at System.arrayCopy();

紧拥背影 2024-10-17 13:59:29
    String[]        arr1D;
    String[][]      arr2D; 
    String[][][]    arr3D; 

    arr1D = new String[] { "1", "2", "3" };

    //////////////////////////////////////////////////////////////////////////
    //assign 1D array to element of 2D array
    ////////////////////////////////////////////////////////////////////////// 
    arr2D = new String[][] {  
                                arr1D ,
                                arr1D ,
                                arr1D 
                           }; 
     /*
     //  OR
     arr2D = new String[3][];
        arr2D[0] = arr1D;
        arr2D[1] = arr1D;
        arr2D[2] = arr1D;       
    //  OR 
     arr2D = new String[][] {  
        new String[] { "1", "2", "3" } ,
        new String[] { "1", "2", "3" } ,
        new String[] { "1", "2", "3" } 
                };      

    */
    //////////////////////////////////////////////////////////////////////////
    //assign 2D array to element of 3D array
    //////////////////////////////////////////////////////////////////////////

    arr3D = new String[][][] {  
            arr2D ,
            arr2D ,
            arr2D 
          }; 
    /*
    // OR   
        arr3D = new String[3][][]; 
        arr3D[0] = arr2D;
        arr3D[1] = arr2D;
        arr3D[2] = arr2D; 
    */  
    String[]        arr1D;
    String[][]      arr2D; 
    String[][][]    arr3D; 

    arr1D = new String[] { "1", "2", "3" };

    //////////////////////////////////////////////////////////////////////////
    //assign 1D array to element of 2D array
    ////////////////////////////////////////////////////////////////////////// 
    arr2D = new String[][] {  
                                arr1D ,
                                arr1D ,
                                arr1D 
                           }; 
     /*
     //  OR
     arr2D = new String[3][];
        arr2D[0] = arr1D;
        arr2D[1] = arr1D;
        arr2D[2] = arr1D;       
    //  OR 
     arr2D = new String[][] {  
        new String[] { "1", "2", "3" } ,
        new String[] { "1", "2", "3" } ,
        new String[] { "1", "2", "3" } 
                };      

    */
    //////////////////////////////////////////////////////////////////////////
    //assign 2D array to element of 3D array
    //////////////////////////////////////////////////////////////////////////

    arr3D = new String[][][] {  
            arr2D ,
            arr2D ,
            arr2D 
          }; 
    /*
    // OR   
        arr3D = new String[3][][]; 
        arr3D[0] = arr2D;
        arr3D[1] = arr2D;
        arr3D[2] = arr2D; 
    */  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文