Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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#_Arrays - Fatal编程技术网

C# 如何在二维数组中赋值

C# 如何在二维数组中赋值,c#,arrays,C#,Arrays,在c语言中如何在二维数组中赋值 我的代码是 int[,] matarix = new int[4, 5]; for (int x = 0; x < 4; x++) { for (int y = 0; y < 5; y++) { matarix[x, y] = x+"-"+y; } } 我尝试了上面的代码,但它的显示错误不能隐式地将

在c语言中如何在二维数组中赋值

我的代码是

int[,] matarix = new int[4, 5];

        for (int x = 0; x < 4; x++)
        {
            for (int y = 0; y < 5; y++)
            {
                matarix[x, y] = x+"-"+y;
            }
        }
我尝试了上面的代码,但它的显示错误不能隐式地将字符串转换为int
怎么做?谢谢

你走对了方向。只需指定一个值

int[,] matarix = new int[4, 5];

for (int x = 0; x < 4; x++) {
    for (int y = 0; y < 5; y++) {
        matarix[x, y] = VALUE_HERE;
    }
}

传入数组的一个维度,它会告诉您该维度存在多少索引。

这是我通常用于填充二维数组的模板。 它只有一行,很容易改变

        int[,] Foo = new int[4,5];
        for(int i=0;i < Foo.Length; i++, Foo[i % Foo.GetLength(0),i / Foo.GetLength(1)] = 0; // Or value to assign
另外,请注意,当您为数组指定字符串时,数组中包含整数。当x-y将数组转换为字符串时,数组中包含整数
希望这对你有帮助

你的方法是正确的。你想怎么做?只需指定一个整数值,如matarix[x,y]=5;那么,您希望为每个单元格指定什么值?现在还不清楚你在问什么,因为你已经得到了除了值以外的所有东西…x+-+y是一个字符串。你的matarix是一个int数组。你想要一个字符串数组吗?
        int[,] Foo = new int[4,5];
        for(int i=0;i < Foo.Length; i++, Foo[i % Foo.GetLength(0),i / Foo.GetLength(1)] = 0; // Or value to assign