简体   繁体   English

3D锯齿状阵列

[英]3D jagged Array

Is there a way to access my three dimensional jagged array like this: 有没有办法像这样访问我的三维锯齿状数组:

jaggedArray[1,2,3];

I got the following code snippets so far: 到目前为止,我得到了以下代码片段:

        int[][] jaggedArray = new int[3][]
        {
            new int[] { 2, 3, 4, 5, 6, 7, 8 },
            new int[] { -4, -3, -2, -1, 0, 1},
            new int[] { 6, 7, 8, 9, 10 }
        };

        int[,,] dontWork = new int[,,] // expect 7 everywhere in the last dimension
        {
            { { 2, 3, 4, 5, 6, 7, 8 } },
            { { -4, -3, -2, -1, 0, 1} },
            { { 6, 7, 8, 9, 10 } }
        };

As for the first question, you're trying to access the 3rd element, of the 2nd element of the 1st element of the jagged array: 对于第一个问题,您正在尝试访问锯齿数组第1个元素的第2个元素的第3个元素:

jaggedArray[1][2][3]

As for the error, a 3D array expects the same number of elements in each element of a dimension. 至于错误,一个3D数组在一个维度的每个元素中期望元素数量相同。 Let's say, for simplicity's sake, that you have a 2D jagged array, a rough representation of what that looks like in memory would be: 假设,为简单起见,您有一个2D锯齿状数组,它大致表示了内存中的内容:

First row  -> 2,   3,  4,  5, 6, 7, 8
Second row -> -4, -3, -2, -1, 0, 1
Third row  -> 6,   7,  8,  9, 10

You can see that each row is seen as a different array, and can therefore differ in size. 您可以看到每一行被视为一个不同的数组,因此大小可能有所不同。 A multidimensional array, however, does not have this property. 但是,多维数组不具有此属性。 It needs to be filled completely: 它需要完全填充:

Column    :  0    1   2   3   4  5  6
------------------------------------
First row :  2,   3,  4,  5,  6, 7, 8
Second row: -4,  -3, -2, -1,  0, 1 
Third row :  6,   7,  8,  9, 10

Your table is missing some cells, which makes no sense. 您的表缺少一些单元格,这没有任何意义。 You need to use the same number of elements per dimension. 每个维度需要使用相同数量的元素。

You got the syntax for declaring 2D jagged array right, 3D jagged arrays are an extension of that. 您已经获得了正确声明2D锯齿状数组的语法,3D锯齿状数组是对它的扩展。 For example: 例如:

int[][][] jagged3d = new int[][][]
{
    new int[][] { new int[] { 111, 112 }, new int[] { 121, 122, 123 } },
    new int[][] { new int[] { 211 } }
}

But to access it, you need different syntax: 但是要访问它,您需要使用不同的语法:

jagged3d[0][1][2]

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

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