【问题标题】:Creating multidimensional array at runtime在运行时创建多维数组
【发布时间】:2015-07-16 22:32:48
【问题描述】:

我在网络上进行了研究,但找不到任何建议如何动态创建多维(排名 >1)数组(它可能是锯齿状数组)。

换句话说,程序会先询问维数(等级),然后询问每个维度的元素数(等级),然后它会创建数组。

有可能吗?

【问题讨论】:

  • 参考这个article
  • @Nimesh:感谢您的链接。至少它谈到了重新调整数组的大小。
  • @Bjørn-Roger Kringsjå:你能更有建设性吗?
  • 为什么我的问题得了负分?
  • 关于动态重新排名(re-dimensoning)的任何信息?

标签: c#


【解决方案1】:

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

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。这有什么问题?

Play with it yourself in this fiddle.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 2013-04-21
    相关资源
    最近更新 更多