在C#中动态存储矩阵值

时间:2013-01-07 06:17:05

标签: c# arrays multidimensional-array jagged-arrays

我正在尝试在c#.net

中构建一个应用程序

这里我有两个相同大小的单维数组 例如,我有矩阵M和N,如下面的结构:

       M[0] M[1] M[2] M[3] M[4]
 N[0]   
 N[1]
 N[2]
 N[3]
 N[4]

这里我已经指定了我的M [0] ....& N [0] ......给他们以便我得到一个矩阵:

    5     6     4     8
4

8

7

2

注意:我将此值设置为动态生成。我已经成功了,直到这一步。

但我喜欢以这种格式将值存储在2x2矩阵中的另一个数组(可能是锯齿状数组或其他数组)中:

      A[0]  A[1]
 B[0]  5     4       (this is the values of M[0] and N[0])

 B[1]  6     4       (this is the values of M[1] and N[0])

 ..............
 B[4]  5     8       (this is the values of M[0] and N[1])

当N [0]的第一行完成时,它必须继续下一行。 我只需要一些如何在C#??

中实现它

3 个答案:

答案 0 :(得分:2)

对于动态存储,您应该了解2d和3d

的基础知识

请参阅此处

二维数组:dotnetperls.com/2d-array

多维数组:msdn.microsoft.com/en-us/library/2yd9wwz4(v=vs.71).aspx

答案 1 :(得分:1)

您无法延迟为数组赋值。我建议你使用List<List<int>>,这是一个例子:

List<List<int>> val = new List<List<int>>();
List<int> M = new List<int>() { 1, 2, 3, 4, 5 };
List<int> N = new List<int>() { 5, 4, 3, 2, 1 };

foreach (int m in M)
{
    foreach (int n in N)
    {
        val.Add(new List<int> { m, n });
    }
}

答案 2 :(得分:1)

stackoverflow.com/questions/594853/dynamic-array-in-c-sharp 结账线程在上面。或者查看下面的来源。

msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx