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

发布于 2024-12-01 23:27:45 字数 1049 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

世态炎凉 2024-12-08 23:27:45

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

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

编辑 为将来找到此线程的人们澄清

上面的代码示例也不够充分,因为它会产生一个数组数组(object[object []])而不是锯齿状数组(object[][]),它们在概念上是相同的东西,但类型不同。

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.

鸩远一方 2024-12-08 23:27:45

您正在尝试将数组引用分配给数组。有关更多详细信息,请阅读 - 锯齿状数组。

尝试一下,

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

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();
   }
自此以后,行同陌路 2024-12-08 23:27:45

我正在努力完全理解您真正想要实现的目标。如果我没猜错的话,你有一些字符串“列表”,你需要将它们存储在另一个列表中。

首先,我建议您使用比数组更现代的方法。 C# 为您提供 IEnumerable<>和 IList<>接口以及从它们派生的所有内容,因此无需坚持使用老式数组。

你可以这样做:

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

然后,如果你想访问“foo6”,你可以写:

var temp = listOfStrings[1][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];
咆哮 2024-12-08 23:27:45

以下工作正常:

var a = new object[] { 0, 1, 1, 2 };
var b = new object[] { "0", "5", "0", "0" };
var c = new object[] { true, true, true, false };

object[][] m = new object[][] { a, b, c };


 var two = m[0][3];
 var bar = m[1][1];
 var f = m[2][3];

The following works just fine:

var a = new object[] { 0, 1, 1, 2 };
var b = new object[] { "0", "5", "0", "0" };
var c = new object[] { true, true, true, false };

object[][] m = new object[][] { a, b, c };


 var two = m[0][3];
 var bar = m[1][1];
 var f = m[2][3];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文