【问题标题】:Multidimensional array init without pre-existing values?没有预先存在的值的多维数组初始化?
【发布时间】:2013-12-26 21:24:29
【问题描述】:

我们如何在没有预先存在的值的情况下初始化多维数组?只有第三个是正确的,但它适用于预先存在的值。我希望我的多维数组包含 10 或 20 个值,然后用 numbers[y][x] 添加它们:

int[][] numbers = new int[10][];
//List<int[]> numbers = new List<int[]>();
//int[10][]{ new int[]{}};
//correct :  { new int[] {1, 2}, new int[] {3, 4, 5} };
numbers[0][0] = 58;

你知道怎么做吗? (不知道[,][][]是不是一样)

谢谢

【问题讨论】:

    标签: c# list multidimensional-array initialization


    【解决方案1】:

    Would you know how to do this? (I don't know if [,] is the same as [][] by the way) 没有一些 int[][] test = new int[10][]; 被称为 jagged array (数组数组)而 int[,] 是一个固定数组

    只需将您的数组声明为以下

     int[,] test = new int[10,30];    
    test[0,1] = 10;
    test[1,2] = 20;
    

    【讨论】:

    • 非常感谢朱莉香农
    【解决方案2】:

    你可以尝试用这种方式初始化值,这是创建jagged-array的一种方式

    int[][] test = new int[10][];
    test[0] = new int[20];
    test[1] = new int[30];
    test[2] = new[] { 3, 4, 5 };
    
    test[0][1] = 10;
    test[1][2] = 20;
    

    【讨论】:

    • 非常感谢AppDeveloper
    猜你喜欢
    • 2011-04-09
    • 2015-08-01
    • 1970-01-01
    • 2013-04-04
    • 2011-04-03
    • 2015-07-20
    相关资源
    最近更新 更多