Initializing multidimensional arrays in c# (with other arrays)

非 Y 不嫁゛ 提交于 2020-02-23 08:51:30

问题


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?


回答1:


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.




回答2:


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:

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:

var temp = listOfStrings[1][2];



回答3:


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


来源:https://stackoverflow.com/questions/7205402/initializing-multidimensional-arrays-in-c-sharp-with-other-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!