Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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#_Python 3.x - Fatal编程技术网

二维数组动态c#

二维数组动态c#,c#,python-3.x,C#,Python 3.x,我是一名python开发人员,我想在C#上转换以下代码: 谢谢你的帮助 这段代码很容易翻译成C,因为在C中,数组是用零初始化的: var n = 3; var tab = new int[n, n]; 我对此进行了测试,它可以工作,但不像python方法那样是动态的: int[][] newtab = new int[4][]; newtab[0] = new int[4] { 0,0,0,0 }; newtab[1] = new int[4] { 0, 0, 0, 0 }; newtab[2

我是一名python开发人员,我想在C#上转换以下代码:


谢谢你的帮助

这段代码很容易翻译成C,因为在C中,数组是用零初始化的:

var n = 3;
var tab = new int[n, n];

我对此进行了测试,它可以工作,但不像python方法那样是动态的:

int[][] newtab = new int[4][];
newtab[0] = new int[4] { 0,0,0,0 };
newtab[1] = new int[4] { 0, 0, 0, 0 };
newtab[2] = new int[4] { 0, 0, 0, 0 };
newtab[3] = new int[4] { 0, 0, 0, 0 };


for (int i = 0; i < newtab.Length; i++)
{
    for (int j = 0; j < newtab[i].Length; j++)
    {
        Console.Write(newtab[i][j]);
    }
    Console.WriteLine();
}
int[]newtab=newint[4][];
newtab[0]=newint[4]{0,0,0};
newtab[1]=newint[4]{0,0,0,0};
newtab[2]=newint[4]{0,0,0,0};
newtab[3]=newint[4]{0,0,0,0};
对于(int i=0;i
由于需要动态列表大小,我建议使用C#generic
list
集合类型

在本例中,我创建了一个int列表。这与int数组的动态数组非常相似(如果在C#中可能的话)

列表
列表
都可以增长和改变大小。在我的示例中,我硬编码了每个列表中元素的数量,但它不一定是固定的

下面是一个示例代码,其中我列出了int的列表:

    static void Main(string[] args)
    {

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

        for (int count = 0; count < 3; count++)
        {
            List<int> list = new List<int>();
            list.AddRange(new int[] { 1, 2, 4 });
            stuff.Add(list);
        }

        Console.WriteLine($"stuff is a list of {stuff.Count} items");
        Console.WriteLine($"the first list item in stuff has {stuff[0].Count} items");
        Console.ReadKey();
    }

希望这有帮助

堆栈溢出不是一种代码编写服务。自己尝试一下,如果有问题,请向我们寻求帮助。没有简单的翻译,因为C#对数据的看法如此不同。我想我会建议在数组上使用
列表
。好的,谢谢,我现在来测试一下,在c#中追加的等价物是什么?我在回答中使用的push?数组没有append/push方法,它们的大小是固定的。如果您需要动态可扩展列表,请使用
list
,它是
Add
方法
    static void Main(string[] args)
    {

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

        for (int count = 0; count < 3; count++)
        {
            List<int> list = new List<int>();
            list.AddRange(new int[] { 1, 2, 4 });
            stuff.Add(list);
        }

        Console.WriteLine($"stuff is a list of {stuff.Count} items");
        Console.WriteLine($"the first list item in stuff has {stuff[0].Count} items");
        Console.ReadKey();
    }
stuff is a list of 3 items 
the first list item in stuff has 3 items