Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 二维动态数组/列表语法问题_C#_List_Matrix_Grid - Fatal编程技术网

C# 二维动态数组/列表语法问题

C# 二维动态数组/列表语法问题,c#,list,matrix,grid,C#,List,Matrix,Grid,我正在尝试用c#创建一个动态2d数组。我想用一个列表,这样 List<List<int>> grid = new List<List<int>>(); 但那没用。 我发现我可以添加单个项目,以便 grid[0][0] = 1; 但我不想使用嵌套循环以这种方式填充网格。我只想在一条线上设置网格。这可能吗?您可以使用,这与您的第一次尝试非常相似。您只需在每个大括号前输入类型名称: List<List<int>> grid =

我正在尝试用c#创建一个动态2d数组。我想用一个列表,这样

List<List<int>> grid = new List<List<int>>();
但那没用。 我发现我可以添加单个项目,以便

grid[0][0] = 1;
但我不想使用嵌套循环以这种方式填充网格。我只想在一条线上设置网格。这可能吗?

您可以使用,这与您的第一次尝试非常相似。您只需在每个大括号前输入类型名称:

List<List<int>> grid = new List<List<int>> {
        new List<int> { 0, 0, 0, 0 },
        new List<int> { 0, 0, 0, 0 },
        new List<int> { 0, 0, 0, 0 },
        new List<int> { 0, 0, 0, 0 }
    };
突然你的
网格
就不再是网格了

<>你可以考虑使用一个<代码> iRealnListList<代码>,如果你的网格是常量的。如果仍要设置网格值,则可以使用
int[,]

int[,] grid = {
    {0,0,0,0},
    {0,0,0,0},
    {0,0,0,0},
    {0,0,0,0},
    };

这不是二维数组。二维数组声明为
int[,]
。它更接近于一个锯齿状数组(数组数组数组),它将被声明为
int[][]
,但是您正在处理列表,因此您有一个列表列表。有一个问题,如果我在末尾加上()是否重要?它似乎在编译双向列表grid=newlist(){…}
grid[0].Add(1);
int[,] grid = {
    {0,0,0,0},
    {0,0,0,0},
    {0,0,0,0},
    {0,0,0,0},
    };