在运行时创建多维数组

时间:2015-07-14 16:07:02

标签: c#

我在网上做了我的研究,但是我找不到任何建议如何动态创建多维(rank> 1)数组(它可能是一个锯齿状数组)。

换句话说,程序首先询问维度数(排名),然后询问每个维度(排名)的元素数量,然后创建数组。

有可能吗?

1 个答案:

答案 0 :(得分:0)

多维没有,但没有锯齿问题 - 这是一个简单的例子:

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {

        List<List<List<int>>> myJaggedIntList = new List<List<List<int>>>();

        myJaggedIntList.Add(new List<List<int>>());
        myJaggedIntList[0].Add(new List<int>());
        myJaggedIntList[0][0].Add(3);

        Console.WriteLine(myJaggedIntList[0][0][0].ToString());
    }
}

第一个List中的每个项目都是List,第二个List中的每个项目也是List,第三个List中的每个项目都是int是{{1}}。这有什么问题?

Play with it yourself in this fiddle.

相关问题