新到c#,不能做多维数组。

时间:2022-01-10 02:02:19

Hi i am moving to c# now from VBS and .net this is my first attempt at making anything in C# so it is a very basic question. How do i make an array?

你好,我现在从VBS和。net转到c#,这是我第一次尝试用c#做任何事情,所以这是一个非常基本的问题。如何创建数组?

Below is my current attempt

下面是我目前的尝试

        string[] arr1 = new string[1, 2];
        arr1[0, 0] = "One";
        arr1[0, 1] = "two";
        arr1[0, 2] = "three";
        arr1[1, 0] = "four";
        arr1[1, 1] = "five";
        arr1[1, 2] = "six";

3 个解决方案

#1


1  

Look at your declaration: string[] arr1 = new string[1, 2]; You are trying to asign a 2 dimensional array to a one dimensional array. The proper declaration would be:
string[,] arr1 = new string[1, 2]; //notice the , in the declaration

查看您的声明:string[] arr1 = new string[1,2];你在尝试将一个二维数组赋值给一个一维数组。正确的声明应该是:string[,] arr1 = new string[1,2];//注意,在声明中。

Furthermore, you have defined your first dimension with a length of 1 and your second with a length of 2, while in your code:

此外,您在代码中定义了长度为1的第一个维度和长度为2的第二个维度:

arr1[1, 0] = "four";
arr1[1, 1] = "five";
arr1[1, 2] = "six";

You are treating it as if your first dimension had a length of 2 and your second as if it had a length of 3. This will throw an exception.

你把它当作第一个维度的长度是2,第二个维度的长度是3。这将抛出一个异常。

#2


2  

There is a site named MSDN :D , You can find all basic syntax related stuffs with detailed examples there :D , http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

有一个名为MSDN:D的网站,你可以在那里找到所有与语法相关的基本内容,并提供详细的示例:D, http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

#3


1  

The array you declared has one row and two columns and you are trying to access two rows and three columns. With your code you will get exception. You also need a comma for two dimension on left side. You can read more about using multi-dimension here.

您声明的数组有一行和两列,您正在尝试访问两行和三列。对于您的代码,您将得到异常。左边的二维也需要一个逗号。您可以在这里阅读更多关于使用多维的信息。

string[,] arr1 = new string[1, 2];
arr1[0, 0] = "One";
arr1[0, 1] = "two";

#1


1  

Look at your declaration: string[] arr1 = new string[1, 2]; You are trying to asign a 2 dimensional array to a one dimensional array. The proper declaration would be:
string[,] arr1 = new string[1, 2]; //notice the , in the declaration

查看您的声明:string[] arr1 = new string[1,2];你在尝试将一个二维数组赋值给一个一维数组。正确的声明应该是:string[,] arr1 = new string[1,2];//注意,在声明中。

Furthermore, you have defined your first dimension with a length of 1 and your second with a length of 2, while in your code:

此外,您在代码中定义了长度为1的第一个维度和长度为2的第二个维度:

arr1[1, 0] = "four";
arr1[1, 1] = "five";
arr1[1, 2] = "six";

You are treating it as if your first dimension had a length of 2 and your second as if it had a length of 3. This will throw an exception.

你把它当作第一个维度的长度是2,第二个维度的长度是3。这将抛出一个异常。

#2


2  

There is a site named MSDN :D , You can find all basic syntax related stuffs with detailed examples there :D , http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

有一个名为MSDN:D的网站,你可以在那里找到所有与语法相关的基本内容,并提供详细的示例:D, http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

#3


1  

The array you declared has one row and two columns and you are trying to access two rows and three columns. With your code you will get exception. You also need a comma for two dimension on left side. You can read more about using multi-dimension here.

您声明的数组有一行和两列,您正在尝试访问两行和三列。对于您的代码,您将得到异常。左边的二维也需要一个逗号。您可以在这里阅读更多关于使用多维的信息。

string[,] arr1 = new string[1, 2];
arr1[0, 0] = "One";
arr1[0, 1] = "two";