在c#中初始化多维数组(与其他数组)
在 C# 中,可以使用常量来初始化多维数组,如下所示:
Object[,] twodArray = new Object[,] { {"00", "01", "02"},
{"10", "11", "12"},
{"20", "21", "22"} };
我个人认为使用硬编码常量初始化数组对于测试练习之外的任何操作都是无用的。无论如何,我迫切需要做的是使用现有数组初始化一个新的多维数组,如上所述。 (它们具有相同的项目数,但内容当然仅在运行时定义)。
我想做的一个例子是。
Object[] first = new Object[] {"00", "01", "02"};
Object[] second = new Object[] {"10", "11", "12"};
Object[] third = new Object[] {"20", "21", "22"};
Object[,] twodArray = new Object[,] { first, second, third };
不幸的是,这不能编译为有效代码。有趣的是,当我尝试
Object[,] twodArray = new Object[,] { {first}, {second}, {third} };
编译并运行代码时,确实,但是结果并不理想 - 一个 3 x 3 的对象数组,结果是一个 3 x 1 的数组数组,每个数组其中有 3 个元素。发生这种情况时,我无法使用以下方式访问我的数组:
Object val = twodArray[3,3];
I have to go:
Object val = twodArray[3,1][3];
这显然不是所需的结果。
那么,有没有什么方法可以从多个现有数组初始化这个新的二维数组,而不需要迭代呢?
In C#, it's possible to initialize a multidimensional array using constants like so:
Object[,] twodArray = new Object[,] { {"00", "01", "02"},
{"10", "11", "12"},
{"20", "21", "22"} };
I personally think initializing an array with hard coded constants is kind of useless for anything other than test exercises. Anyways, what I desperately need to do is initialize a new multidimensional array as above using existing arrays. (Which have the same item count, but contents are of course only defined at runtime).
A sample of what I would like to do is.
Object[] first = new Object[] {"00", "01", "02"};
Object[] second = new Object[] {"10", "11", "12"};
Object[] third = new Object[] {"20", "21", "22"};
Object[,] twodArray = new Object[,] { first, second, third };
Unfortunately, this doesn't compile as valid code. Funny enough, when I tried
Object[,] twodArray = new Object[,] { {first}, {second}, {third} };
The code did compile and run, however the result was not as desired - a 3 by 3 array of Objects, what came out was a 3 by 1 array of arrays, each of which had 3 elements. When that happens, I can't access my array using:
Object val = twodArray[3,3];
I have to go:
Object val = twodArray[3,1][3];
Which obviously isn't the desired result.
So, is there any way to initialize this new 2D array from multiple existing arrays without resorting to iteration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您切换到锯齿状数组,这将起作用:
编辑 为将来找到此线程的人们澄清
上面的代码示例也不够充分,因为它会产生一个数组数组(object[object []])而不是锯齿状数组(object[][]),它们在概念上是相同的东西,但类型不同。
This would work if you switched to jagged arrays:
Edit To clarify for people finding this thread in the future
The code sample above is also inadequate as it results in an array of arrays (object[object[]]) rather than a jagged array (object[][]) which are conceptually the same thing but distinct types.
您正在尝试将数组引用分配给数组。有关更多详细信息,请阅读 - 锯齿状数组。
尝试一下,
You are trying to assign array references to an array. For more details please read - Jagged Arrays.
Try this,
我正在努力完全理解您真正想要实现的目标。如果我没猜错的话,你有一些字符串“列表”,你需要将它们存储在另一个列表中。
首先,我建议您使用比数组更现代的方法。 C# 为您提供 IEnumerable<>和 IList<>接口以及从它们派生的所有内容,因此无需坚持使用老式数组。
你可以这样做:
然后,如果你想访问“foo6”,你可以写:
I'm struggling to fully understand what you're really trying to achieve. If I got it right, you have some "lists" of strings, which you need to store in another list.
First of all, I'd recommend you to use a more modern approach than arrays. C# offers you IEnumerable<> and IList<> interfaces and all the stuff that derives from them, so no need to stick with old fashioned arrays.
You could do something like this:
Then if you want to access "foo6" you write:
以下工作正常:
The following works just fine: