如何理解3d数组的初始化?

时间:2016-07-04 11:14:19

标签: c# multidimensional-array

谷歌搜索却无法理解这个阵列是如何成为1,8,3阵列的?你怎么看到X是1,Y是8,Z是这个数组的3?

        double[,,] points =
        {
            { {-1, 0, 3}, {-1, -1, -1}, {4, 1, 1 },
            {2, 0.5, 9}, {3.5, 2, -1}, {3, 1.5, 3},
            {-1.5, 4, 2}, { 5.5, 4, -0.5}}
        };

3 个答案:

答案 0 :(得分:2)

如果你将它分开,你就能看清楚:

double[,,] points ={
    { 
        {-1, 0, 3}, 
        {-1, -1, -1}, 
        {4, 1, 1 },
        {2, 0.5, 9}, 
        {3.5, 2, -1}, 
        {3, 1.5, 3},
        {-1.5, 4, 2}, 
        { 5.5, 4, -0.5}
    }
};

让我们把它分开:

1º这是你的阵列,它包含的一切是:

double[,,] points ={
   {//Top level element (1 dimension since there is only one array inside it)

   }
};

2º顶层内部有八个子阵列(现在我们有(1,8)个维度):

double[,,] points ={
    { 
        {}, //Subarray 1 (Second Level, with 8 elements)
        {}, //Subarray 2
        {}, //Subarray 3
        {}, //Subarray 4
        {}, //Subarray 5
        {}, //Subarray 6
        {}, //Subarray 7
        {}  //Subarray 8
    }
};

3º最后在8个子阵列的每一个内部有三个元素:

double[,,] points ={
    { 
        {-1, 0, 3}, //Third Level (with 3 elements)
        {-1, -1, -1}, 
        {4, 1, 1 },
        {2, 0.5, 9}, 
        {3.5, 2, -1}, 
        {3, 1.5, 3},
        {-1.5, 4, 2}, 
        { 5.5, 4, -0.5}
    }
};

有了这些,我们终于拥有了所有的维度(1,8,3)。我建议你格式化数组,看清楚它们。

我希望它可以帮到你。

答案 1 :(得分:1)

你只需重新格式化,基本上:

double[,,] points =
{
    { // One top-level element
         {-1, 0, 3}, // 8 "middle-level" elements, each of which has 3 elements
         {-1, -1, -1},
         {4, 1, 1 },
         {2, 0.5, 9},
         {3.5, 2, -1},
         {3, 1.5, 3},
         {-1.5, 4, 2},
         { 5.5, 4, -0.5}
    }
};

答案 2 :(得分:0)

我重新安排了数组,并添加了一些注释来澄清结构:

double[,,] points =
{   // one element: X
    { // having eight elements: Y
        {-1, 0, 3}, // each having three elements: Z
        {-1, -1, -1}, 
        {4, 1, 1 },
        {2, 0.5, 9}, 
        {3.5, 2, -1}, 
        {3, 1.5, 3},
        {-1.5, 4, 2}, 
        { 5.5, 4, -0.5}
    }
};

隐式点的第一个元素为X,第二个元素为Y,第三个元素为Z.所以元素的数量为1,8,3。