C# 3 维数组定义问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/694510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:45:43  来源:igfitidea点击:

C# 3 dimensional array definition issue

c#arrays

提问by George2

My following code has compile error,

我的以下代码有编译错误,

Error 1 Cannot implicitly convert type 'TestArray1.Foo[,,*]' to 'TestArray1.Foo[][][]' C:\Users\lma\Documents\Visual Studio 2008\Projects\TestArray1\TestArray1\Program.cs 17 30 TestArray1

错误 1 ​​无法将类型 'TestArray1.Foo[ ,,*]'隐式转换为 'TestArray1.Foo[][][]' C:\Users\lma\Documents\Visual Studio 2008\Projects\TestArray1\TestArray1\Program.cs 17 30 测试阵列1

Does anyone have any ideas? Here is my whole code, I am using VSTS 2008 + Vista 64-bit.

有没有人有任何想法?这是我的全部代码,我使用的是 VSTS 2008 + Vista 64 位。

namespace TestArray1
{
    class Foo
    {    
    }

    class Program
    {
        static void Main(string[] args)
        {
            Foo[][][] foos = new Foo[1, 1, 1];

            return;
        }
    }
}

EDIT: version 2. I have another version of code, but still has compile error. Any ideas?

编辑:版本 2。我有另一个版本的代码,但仍然有编译错误。有任何想法吗?

Error   1   Invalid rank specifier: expected ',' or ']' C:\Users\lma\Documents\Visual Studio 2008\Projects\TestArray1\TestArray1\Program.cs 17  41  TestArray1
Error   2   Invalid rank specifier: expected ',' or ']' C:\Users\lma\Documents\Visual Studio 2008\Projects\TestArray1\TestArray1\Program.cs 17  44  TestArray1


namespace TestArray1
{
    class Foo
    {    
    }

    class Program
    {
        static void Main(string[] args)
        {
            Foo[][][] foos = new Foo[1][1][1];

            return;
        }
    }
}

EDIT: version 3. I think I want to have a jagged array. And after learning from the fellow guys. Here is my code fix, and it compile fine in VSTS 2008. What I want is a jagged array, and currently I need to have only one element. Could anyone review whether my code is correct to implement my goal please?

编辑:版本 3。我想我想要一个锯齿状的数组。并在向小伙伴们学习之后。这是我的代码修复,它在 VSTS 2008 中编译得很好。我想要的是一个锯齿状数组,目前我只需要一个元素。任何人都可以检查我的代码是否正确实现我的目标?

namespace TestArray1
{
    class Foo
    {    
    }

    class Program
    {
        static void Main(string[] args)
        {
            Foo[][][] foos = new Foo[1][][];
            foos[0] = new Foo[1][];
            foos[0][0] = new Foo[1];
            foos[0][0][0] = new Foo();
            return;
        }
    }
}

thanks in advance, George

提前致谢,乔治

回答by Jon Grant

Make up your mind :)

下定决心:)

You either want:

你要么想要:

Foo[,,] foos = new Foo[1, 1, 1];

or:

或者:

Foo[][][] foos = new Foo[1][1][1];

回答by AaronLS

Depends on whether you want them to be jagged or not:

取决于您是否希望它们是锯齿状的:

//makes a 5 by 4 by 3 array:

string[,,] foos = new string[5,4,3];

http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

Oh and here is initializing values:

哦,这里是初始化值:

    char[, ,] blah = new char[2, 2, 2] { 
        {{ '1', '2' }, { '3', '4' }},
        {{ '5', '6' }, { '7', '8' }}
         };

Note that this will not work:

请注意,这将不起作用:

Foo[][][] foos = new Foo[1][1][1];

Because you are using the jagged array syntax, which does not let you define the size of nested arrays. Instead use do this:

因为您使用的是锯齿状数组语法,它不允许您定义嵌套数组的大小。而是使用这样做:

Foo[,,] foos = new Foo[1][1][1];  //1 by 1 by 1

or this

或这个

Foo[][][] foos = new Foo[1][][];  //1 array allowing two nested levels of jagged arrays
foos[0] = new Foo[1];  //for the first element, create a new array nested in it
foos[0][0] = new Foo[1]; //create a third level new array nested in it

回答by Joey

You are declaring a double-nested array (array of array of array) and assigning a three-dimensional array. Those two are definitely different. You can change your declaration to

您正在声明一个双嵌套数组(数组数组的数组)并分配一个三维数组。这两个肯定是不一样的。您可以将声明更改为

Foo[,,] foos = new Foo[1,1,1]

if you want a truly three-dimensional array. Jagged arrays (the [][][]kind) are not that needed in C# as in, say, Java.

如果你想要一个真正的三维数组。锯齿状数组([][][]那种)在 C# 中不像在 Java 中那样需要。

回答by Henk Holterman

The simplest answer is to use

最简单的答案是使用

Foo[,,] foos = new Foo[2, 3, 4];

Which gives you a 3-dimensional array as a contiguous block of memory of 2*3*4=24 Foo's.

这为您提供了一个 3 维数组作为 2*3*4=24 个 Foo 的连续内存块。

The alternative looks like :

替代方案如下:

Foo[][][] foos = new Foo[2][][];

for (int a = 0; a < foos.Length; a++)
{
  foos[a] = new Foo[3][];
  for (int b = 0; b < foos[a].Length; b++)
  {
     foos[a][b] = new Foo [4];

     for (int c = 0; c < foos[a][b].Length; c++)
        foos[a][b][c] = new Foo();
  }
}

Although this jagged (= array of array) approach is a bit more work, using it is actually faster. This is caused by a shortcoming of the compiler that will always do a range check when accessing an element in the Foo[,,] case, while it is able to at least optimize for-loops that use the Length property in the Foo[][][] scenario.

虽然这种锯齿状(= 数组数组)方法需要更多的工作,但使用它实际上更快。这是由编译器的一个缺点引起的,它在访问 Foo[,,] 情况下的元素时总是会进行范围检查,而它至少能够优化使用 Foo[] 中的 Length 属性的 for 循环[][] 设想。

Also see this question

也看到这个问题

回答by Keith

These two different array declarations create very different things.

这两个不同的数组声明创建了非常不同的东西。

Let's look at simpler case:

让我们看一个更简单的案例:

Foo[,] twoDimensionArray = new Foo[5,5];

This array has two dimensions - you can think of it as a table. You need both axis in order to return anything:

这个数组有两个维度——你可以把它想象成一个表格。您需要两个轴才能返回任何内容:

Foo item = twoDimensionArray[2,3]

The indexes always have the same lengths - in this case 0-4.

索引始终具有相同的长度 - 在这种情况下为 0-4。

A jagged array is actually an array of arrays:

锯齿状数组实际上是一个数组数组:

Foo[][] jaggedArray = new Foo[5][];

jaggedArray[0] = new Foo[2];
jaggedArray[1] = new Foo[4];
...

If you only use one axis index it will return an array:

如果您只使用一个轴索引,它将返回一个数组:

Foo[] oneRow = jaggedArray[3];

If you use both you make your selection from the sub-array:

如果同时使用两者,则从子数组中进行选择:

Foo item = jaggedArray[3][2];

//would be the same as:
Foo item = oneRow[2];

Each of these sub-arrays can have a different length, or even not be populated.

这些子数组中的每一个都可以具有不同的长度,甚至可以不被填充。

回答by Jon Skeet

Regarding version 2 of your code - unfortunately you can't specify jagged arrays in that way. Instead, you need to do:

关于您的代码的第 2 版 - 不幸的是,您不能以这种方式指定锯齿状数组。相反,您需要执行以下操作:

Foo[][][] foos = new Foo[1][][];
foos[0] = new Foo[1][];
foos[0][0] = new Foo[1];

You have to populate each array separately, basically. Foo[][][]means "an array of arrays of arrays of Foo." An initialization statement like this is only capable of initializing onearray at a time. With the rectangular array, you still end up with just a single (multi-dimensional) array, which is why new Foo[1,1,1]is valid.

基本上,您必须分别填充每个数组。Foo[][][]意思是“一组 Foo 数组的数组”。像这样的初始化语句一次只能初始化一个数组。使用矩形数组,您最终仍然只有一个(多维)数组,这new Foo[1,1,1]就是有效的原因。

If this is for real code by the way, I'd urge you to at least considerother design decisions. Arrays of arrays can be useful, but you can easily run into problems like this. Arrays of arrays of arrays are even nastier. There may be more readable ways of expressing what you're interested in.

顺便说一下,如果这是真正的代码,我建议您至少考虑其他设计决策。数组的数组可能很有用,但您很容易遇到这样的问题。数组数组数组甚至更讨厌。可能有更多可读的方式来表达您感兴趣的内容。

回答by Jon Skeet

If it means anything, for just the C# declaration:

如果这意味着什么,对于 C# 声明:

int[,,] ary = new int[,,]
{
      { { 111, 112 }, { 121, 122 }, { 131, 132 } } 
    , { { 211, 212 }, { 221, 222 }, { 231, 232 } } 
    , { { 311, 312 }, { 321, 322 }, { 331, 332 } } 
    , { { 411, 412 }, { 421, 422 }, { 431, 432 } } 
};

回答by alireza

Array Initialization

数组初始化

int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                { { 7, 8, 9 }, { 10, 11, 12 } } }

source: Multidimensional Arrays (C# Programming Guide)

来源:多维数组(C# 编程指南)