开发者

What's wrong with my multidimensional - array declaration? C#

开发者 https://www.devze.com 2022-12-18 08:37 出处:网络
private int[, ,] table = new int[4 , 5 , 5]{ {{0,6,2,6,4},{2,2,4,2,8},{4,4,8,4,6},{6,6,2,6,4},{8,8,6,8,2}},
private int[, ,] table = new int[4 , 5 , 5]{
    {{0,6,2,6,4},{2,2,4,2,8},{4,4,8,4,6},{6,6,2,6,4},{8,8,6,8,2}},
    {{0,2,8,8,4},{2,4,6,6,8},{4,8,2,2,6},{6,2,8,8,4},{8,6,4,4,2}},
    {{0,4,2,4,4},{2,8,4,8,8},{4,6,8,6,6},{6,4,2,4,4},{8,2,6,2,2}},
    {{0,8,8,2,4},{2,6,6,4,8},{4,2,2,8,6},{6,8,8,2,4},{8,4,4,6,2}}
};

I want this table:

k|l     0      1       2      3      4

0       0626开发者_如何学JAVA4  22428  44846  66264  88682
1       02884  24668  48226  62884  86442
2       04244  28488  46866  64244  82622
3       08824  26648  42286  68824  84462

thanks for help


There's nothing wrong with your declaration. It compiles fine and I can write something like this:

        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                for (int k = 0; k < 5; k++)
                {
                    var x = table[i, j, k];
                }
            }
        }

Update: sure you can shorten it a tiny bit. Since the array dimensions are given by your initial values, you can write it like this:

        private int[,,] table = new [,,]{
             <your numbers go here>
        };


you need a 2 dimensional array, its [4,5] array


Try skipping the new int[4 , 5 , 5] part, the compiler should be able to find out the dimensions. Otherwise I don't see any problem.


Nothing is wrong. It works flawlessly for me. What problem exactly you have?


The compiler can figure out the length of each dimension for you, so let it do all the hard work:

private int[,,] table = new int[,,]{
    {{0,6,2,6,4},{2,2,4,2,8},{4,4,8,4,6},{6,6,2,6,4},{8,8,6,8,2}},
    {{0,2,8,8,4},{2,4,6,6,8},{4,8,2,2,6},{6,2,8,8,4},{8,6,4,4,2}},
    {{0,4,2,4,4},{2,8,4,8,8},{4,6,8,6,6},{6,4,2,4,4},{8,2,6,2,2}},
    {{0,8,8,2,4},{2,6,6,4,8},{4,2,2,8,6},{6,8,8,2,4},{8,4,4,6,2}}
};

And this gives you the output you want, apart from the header:

    StringBuilder stringBuilder = new StringBuilder();

    for (int row = 0; row < table.GetLength(0); ++row)
    {
        stringBuilder.Append(row.ToString() + "\t");
        for (int column = 0; column < table.GetLength(1); ++column)
        {
            for (int valueIndex = 0; valueIndex < table.GetLength(2); ++valueIndex)
            {
                stringBuilder.Append(table[row, column, valueIndex].ToString());
            }
            stringBuilder.Append("\t");
        }
        stringBuilder.AppendLine();
    }

    string result = stringBuilder.ToString();
    Console.WriteLine(result);


Just change new int[4 , 5 , 5] with new[,,]

this is what you should have :

var table = new[,,]
            {
                {{0, 6, 2, 6, 4}, {2, 2, 4, 2, 8}, {4, 4, 8, 4, 6}, {6, 6, 2, 6, 4}, {8, 8, 6, 8, 2}},
                {{0, 2, 8, 8, 4}, {2, 4, 6, 6, 8}, {4, 8, 2, 2, 6}, {6, 2, 8, 8, 4}, {8, 6, 4, 4, 2}},
                {{0, 4, 2, 4, 4}, {2, 8, 4, 8, 8}, {4, 6, 8, 6, 6}, {6, 4, 2, 4, 4}, {8, 2, 6, 2, 2}},
                {{0, 8, 8, 2, 4}, {2, 6, 6, 4, 8}, {4, 2, 2, 8, 6}, {6, 8, 8, 2, 4}, {8, 4, 4, 6, 2}}
            };


That piece of code won't compile if you have it in the wrong place - if you have it inside a function then drop the private modifier. (If it is declared at class level then it is fine).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号