简体   繁体   English

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

[英]Android java assigning 2d array to 3d array

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. 我在尝试将2d数组分配给3d数组时遇到问题,所以我想问一个有关3d和2d数组的问题。

Say I have a masterArray[][][] and wanted to put childArray1[][] and childArray2[][] into it. 假设我有一个masterArray [] [] []并想将childArray1 [] []和childArray2 [] []放入其中。 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. 好的,所以在我的init方法中,我使用这些方法将子数组设置为主数组。 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. 在我的应用程序中,事情不起作用,所以我选择了我不是100%肯定的代码。

In Java multi-d arrays are actually arrays of arrays. 在Java中,multi-d数组实际上是数组的数组。 So they can even be disjoint. 所以他们甚至可以脱节。 In the code you posted you refer to a variable called currentLevel that you did not define. 在您发布的代码中,您将引用一个未定义的名为currentLevel的变量。 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(); 如果您曾经使用过大型数组并且需要速度,那么可以看看System.arrayCopy();。

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. 只要你把东西放在你拿出它们的方式上,你组织一个3d数组并不重要。

From your comment on another answer it seems that you are having problem with element order ( [currentLevel][x][y] = childArray[y][x]; ) 从对另一个答案的评论来看,您似乎对元素顺序有[currentLevel][x][y] = childArray[y][x];[currentLevel][x][y] = childArray[y][x];

It seems you mixed MAP_HEIGHT and MAP_WIDTH. 看来你混合了MAP_HEIGHT和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];
    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; 
    */  

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

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