【问题标题】:How to a populate a 3d array with specific values如何使用特定值填充 3d 数组
【发布时间】:2015-06-11 02:57:32
【问题描述】:

我希望这看起来不是一个简单的问题。 我之前已经能够通过这种方法填充二维数组。但我不确定如何使用这个网格或类似的网格来填充 3D 数组。这可能是一个非常简单的问题,但我希望有人能指出我正确的方向。 我想用它来制作 2d 游戏关卡。

int[][][] LevelGrid = new int[LevelGridHeight][LevelGridWidth][3]; 

int[][][] start_Grid = { {5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
                         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 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, 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, 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,},
                         {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,} }

谢谢

【问题讨论】:

  • 为什么要第三个参数?你不能只使用新的 int[LevelGridHeight][LevelGridWidth]
  • 关于关卡每个区域的具体信息

标签: java arrays multidimensional-array processing 2d-games


【解决方案1】:

您可以将一维数组视为“事物”的数组,事物是数组的任何类型。

int[] array1 = {1, 2, 3};
int[] array2 = {4, 5, 6};
int[] array3 = {7, 8, 9};
int x = array1[0];

您可以将二维数组视为数组数组

   int[][] array2d= {array1, array2, array3};
   int[] xArray = array2d[0];
   int x = xArray[0]; //array2d[0][0];

您可以将三维数组视为数组数组的数组

   int[][][] array3d = {array2d_1, array2d_2, array2d_3};
   int x = array3d[0][0][0];

所以您可以先构建子数组,然后将它们添加到 3D 数组中,也可以在一个块中完成所有操作:

int[][][] array3D = { 
{ {0,1,2}, {0,1,2} }, { {0,1,2}, {0,1,2} }, 
{ {0,1,2}, {0,1,2} }, { {0,1,2}, {0,1,2} }
};

编辑:这是一个先构建子数组,然后从它们构建 3d 数组的示例:

void setup() {

  //one-dimensional arrays are arrays of things
  int[] array1 = {1, 2, 3};
  int[] array2 = {4, 5, 6 };
  int[] array3 = {7, 8, 9};

  int[] array4 = {10, 11, 12};
  int[] array5 = {13, 14, 15};
  int[] array6 = {16, 17, 18};

  //two-dimensional arrays are arrays of arrays
  int[][] row1 = {array1, array2, array3};
  int[][] row2 = {array4, array5, array6};

  //three-dimensional arrays are arrays of arrays of arrays
  int[][][] array3d = {row1, row2};
}

而且我没有设置大小,因为我使用的是数组初始化速记,所以我不需要提前设置大小。请注意,您在初始化 start_Grid 时也没有设置大小。

还请注意,就像其他人所说的那样,使用这样的数组可能会变得非常笨拙。因此,除非您有充分的理由使用数组,否则您可能应该使用某种数据结构。我想无论你的“底层”数组代表什么,都可以用某种对象来表示,然后你只需要一个更容易思考的二维数组。但这完全取决于您 - 3D 数组只是数组的 2D 数组!

【讨论】:

  • 谢谢凯文,你能不能给我一个创建子数组并将其添加到数组中的例子?另外你为什么不声明你的数组的大小?
  • 谢谢凯文,我只想使用 3d 数组来存储一个或两个值,这些值表示 2d 数组中每个值的属性。你认为这会大大减慢我的程序吗?我基本上会像使用二维数组一样使用它,但偶尔会编辑 2 或 3 个属性值
  • @Espore 我真的不会担心速度。已经提到的任何方法之间都不会有很大的区别。你需要担心的是你自己的理智——换句话说,维护你的程序是多么容易。像这样的大数组比对象更难维护。
【解决方案2】:
int[][][] arr3d = new int[][][]{{{1 , 2} , {4 , 5} , {{1 , 3}}};

//or

int[][][] arr3d = new int[x][y][z];
int[2][1][4] = 5;

但如果可能的话,您真的应该寻找另一种表示数据的方式。存储 3d 数组所需的内存大小很快就会变得相当大。

【讨论】:

  • 您如何建议我更有效地表示数据?谢谢你的回答! :)
  • 嗯,这并不容易回答,因为我不知道您存储什么数据以及如何使用它。例如,动态加载区域而不是一次全部加载
  • 我认为我可以设置以便动态加载区域。这只是当前需要在屏幕上显示的所有内容,因此我目前无法像那样提高效率
【解决方案3】:

有索引:

        int[][][] start_Grid = {
            { { /*[0][0][0] */1, /*[0][0][1] */ 2, /*[0][0][2] */ 3, }, {/*[0][1][0] */ 1, /*[0][1][1] */ 2, /*[0][1][2] */ 3 } },
            { { /*[1][0][0] */1, /*[1][0][1] */56, /*[1][0][2] */ 55 }, { /*[1][1][0] */1, /*[1][1][1] */56, /*[1][1][2] */55 } }

    }

【讨论】:

  • 能否请您提供更多关于索引如何有用的信息?
  • 我只是将数组值的索引放在注释中,以澄清它们在数组中的位置,让您更清楚
猜你喜欢
  • 2018-11-30
  • 2021-03-20
  • 1970-01-01
  • 2012-09-06
  • 2017-09-11
  • 2022-08-04
  • 2011-07-02
  • 2022-09-29
  • 2021-09-27
相关资源
最近更新 更多