在c#中初始化多维数组(与其他数组一起)

时间:2021-03-24 21:34:04

In C#, it's possible to initialize a multidimensional array using constants like so:

在c#中,可以使用如下常量初始化多维数组:

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:

代码确实进行了编译和运行,但是结果并不理想——一个3×3的数组,结果是一个3×1的数组,每个数组都有3个元素。当发生这种情况时,我不能使用:

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?

那么,有没有什么方法可以在不使用迭代的情况下从多个现有数组初始化这个新的2D数组呢?

3 个解决方案

#1


4  

This would work if you switched to jagged arrays:

如果切换到锯齿状数组,这将会起作用:

int[] arr1 = new[] { 1, 2, 3 };
int[] arr2 = new[] { 4, 5, 6 };
int[] arr3 = new[] { 7, 8, 9 };

int[][] jagged = new[] { arr1, arr2, arr3 };

int six = jagged[1][2];

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.

上面的代码示例也不够,因为它导致数组(object[object][]])而不是一个不规则的数组(object[][][]]),这些数组在概念上是相同的,但类型是不同的。

#2


1  

You are trying to assign array references to an array. For more details please read - Jagged Arrays.

您正在尝试为数组分配数组引用。有关更多细节,请阅读锯齿状数组。

Try this,

试试这个,

Object[] first = new Object[] { "00", "01", "02" };
Object[] second = new Object[] { "10", "11", "12" };
Object[] third = new Object[] { "20", "21", "22" };
Object[][] result = { first, second, third };

foreach (object [] ar in result)
   {
       foreach (object ele in ar)
        {
            Console.Write(" " + ele);
          }
       Console.WriteLine();
   }

#3


0  

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.

首先,我建议您使用比数组更现代的方法。c#为您提供了IEnumerable<>和IList<>接口,以及所有从它们派生出来的接口,因此不需要使用老式数组。

You could do something like this:

你可以这样做:

var list1 = new List<string> { "foo1", "foo2", "foo3" };
var list2 = new List<string> { "foo4", "foo5", "foo6" };
var list3 = new List<string> { "foo7", "foo8", "foo9" };
var listOfStrings = new List<List<string>> { list1, list2, list3 };

Then if you want to access "foo6" you write:

如果你想访问“foo6”,你可以这样写:

var temp = listOfStrings[1][2];

#1


4  

This would work if you switched to jagged arrays:

如果切换到锯齿状数组,这将会起作用:

int[] arr1 = new[] { 1, 2, 3 };
int[] arr2 = new[] { 4, 5, 6 };
int[] arr3 = new[] { 7, 8, 9 };

int[][] jagged = new[] { arr1, arr2, arr3 };

int six = jagged[1][2];

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.

上面的代码示例也不够,因为它导致数组(object[object][]])而不是一个不规则的数组(object[][][]]),这些数组在概念上是相同的,但类型是不同的。

#2


1  

You are trying to assign array references to an array. For more details please read - Jagged Arrays.

您正在尝试为数组分配数组引用。有关更多细节,请阅读锯齿状数组。

Try this,

试试这个,

Object[] first = new Object[] { "00", "01", "02" };
Object[] second = new Object[] { "10", "11", "12" };
Object[] third = new Object[] { "20", "21", "22" };
Object[][] result = { first, second, third };

foreach (object [] ar in result)
   {
       foreach (object ele in ar)
        {
            Console.Write(" " + ele);
          }
       Console.WriteLine();
   }

#3


0  

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.

首先,我建议您使用比数组更现代的方法。c#为您提供了IEnumerable<>和IList<>接口,以及所有从它们派生出来的接口,因此不需要使用老式数组。

You could do something like this:

你可以这样做:

var list1 = new List<string> { "foo1", "foo2", "foo3" };
var list2 = new List<string> { "foo4", "foo5", "foo6" };
var list3 = new List<string> { "foo7", "foo8", "foo9" };
var listOfStrings = new List<List<string>> { list1, list2, list3 };

Then if you want to access "foo6" you write:

如果你想访问“foo6”,你可以这样写:

var temp = listOfStrings[1][2];