C#3维数组的定义问题 [英] C# 3 dimensional array definition issue

查看:119
本文介绍了C#3维数组的定义问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的关注code的编译错误,

My following code has compile error,

错误1无法隐式转换类型'TestArray1.Foo [ 的,*]'到'TestArray1.Foo [] [] []'C:\\用户\\ LMA \\文档\\ Visual Studio 2008中\\项目\\ TestArray1 \\ TestArray1 \\ Program.cs的17 30 TestArray1

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

有没有人有什么想法?这里是我的整个code,我使用VSTS 2008 + Vista的64位。

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

namespace TestArray1
{
    class Foo
    {    
    }

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

            return;
        }
    }
}

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

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

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;
        }
    }
}

编辑:第3版。我想,我想有一个锯齿形数组。从老乡的家伙学习后。这里是我的code修补程序,我要的是一个交错数组它编译VSTS 2008年精美,目前我需要只有一个元素。任何人都可以查看我的code是否正确实现我的目标吗?

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?

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

推荐答案

关于你提到的code第2版 - 不幸的是,你不能指定以这种方式交错数组。相反,你需要做的:

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

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

您必须单独填充每个阵列,基本上是这样。 富[] [] [] 意味着富的数组的数组的数组。像这样的初始化语句只能够初始化的有一个的阵列在一个时间的。随着矩形阵列,你还是落得只是一个单一的(多维)阵列,这就是为什么新富[1,1,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 one array 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.

如果这是对的方式真正code,我也希望你至少的认为的其他设计决策。数组的数组是有用的,但你可以很容易碰到这样的问题。数组的数组的数组甚至令人讨厌。有可能是你感兴趣的前pressing的更易读的方式。

If this is for real code by the way, I'd urge you to at least consider other 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.

这篇关于C#3维数组的定义问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆