简体   繁体   English

如何声明二维字符串数组?

[英]How can I declare a two dimensional string array?

string[][] Tablero = new string[3][3];

I need to have a 3x3 array arrangement to save information to. 我需要一个3x3阵列安排来保存信息。 How do I declare this in C#? 我如何在C#中声明这个?

string[,] Tablero = new string[3,3];

You can also instantiate it in the same line with array initializer syntax as follows: 您还可以使用数组初始化程序语法在同一行中实例化它,如下所示:

string[,] Tablero = new string[3, 3] {{"a","b","c"},
                                      {"d","e","f"}, 
                                      {"g","h","i"} };

You probably want this: 你可能想要这个:

string[,] Tablero = new string[3,3];

This will create you a matrix-like array where all rows have the same length. 这将创建一个类似矩阵的数组,其中所有行具有相同的长度。

The array in your sample is a so-called jagged array , ie an array of arrays where the elements can be of different size. 样本中的数组是一个所谓的锯齿状数组 ,即一个数组数组,其中元素的大小可以不同。 A jagged array would have to be created in a different way: 必须以不同的方式创建锯齿状数组:

string[][] Tablero = new string[3][];
for (int i = 0; i < Tablero.GetLength(0); i++)
{
    Tablero[i] = new string[3];
}

You can also use initializers to fill the array elements with data: 您还可以使用初始化程序用数据填充数组元素:

string[,] Tablero = new string[,]
{
    {"1.1","1.2", "1.3"},
    {"2.1","2.2", "2.3"},
    {"3.1", "3.2", "3.3"}
};

And in case of a jagged array: 如果是锯齿状阵列:

string[][] Tablero = new string[][]
{
    new string[] {"1.1","1.2", "1.3"},
    new string[] {"2.1","2.2", "2.3"},
    new string[] {"3.1", "3.2", "3.3"}
};

You just declared a jagged array. 你刚刚声明了一个锯齿状的数组。 Such kind of arrays can have different sizes for all dimensions. 这种类型的阵列可以具有不同尺寸的所有尺寸。 For example: 例如:

string[][] jaggedStrings =  {
new string[] {"x","y","z"},
new string[] {"x","y"},
new string[] {"x"}
};

In your case you need regular array. 在你的情况下,你需要常规阵列。 See answers above. 见上面的答案。 More about jagged arrays 更多关于锯齿状阵列的信息

I assume you're looking for this: 我假设你正在寻找这个:

        string[,] Tablero = new string[3,3];

The syntax for a jagged array is: 锯齿状数组的语法是:

        string[][] Tablero = new string[3][];
        for (int ix = 0; ix < 3; ++ix) {
            Tablero[ix] = new string[3];
        }

There are 2 types of multidimensional arrays in C#, called Multidimensional and Jagged . C#中有两种类型的多维数组,称为MultidimensionalJagged

For multidimensional you can by: 对于多维,您可以通过:

string[,] multi = new string[3, 3];

For jagged array you have to write a bit more code: 对于锯齿状数组,您必须编写更多代码:

string[][] jagged = new string[3][];
            for (int i = 0; i < jagged.Length; i++)
            {
                jagged[i] = new string[3];
            }

In short jagged array is both faster and has intuitive syntax. 简而言之,锯齿状阵列既快又具有直观的语法。 For more information see: this Stackoverflow question 有关更多信息,请参阅: 此Stackoverflow问题

try this : 试试这个 :

string[,] myArray = new string[3,3];

have a look on http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx 看看http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

A 3x3 (multidimensional) array can also be initialized (you have already declared it) like this: 也可以像这样初始化一个3x3(多维)数组(你已经声明了它):

string[,] Tablero =  {
                        { "a", "b", "c" },
                        { "d", "e", "f" }, 
                        { "g", "h", "i"} 
                     };
string[,] Tablero = new string[3,3];

当您尝试创建一个多维数组时,您需要做的就是在声明中添加一个逗号,如下所示:

string[,] tablero = new string[3,3].

string[][] is not a two-dimensional array, it's an array of arrays (a jagged array ). string[][] 不是二维数组,它是一个数组数组(一个锯齿状数组 )。 That's something different. 那是不同的。

To declare a two-dimensional array, use this syntax: 要声明二维数组,请使用以下语法:

string[,] tablero = new string[3, 3];

If you really want a jagged array , you need to initialize it like this: 如果你真的想要一个锯齿状数组 ,你需要像这样初始化它:

string[][] tablero = new string[][] { new string[3], 
                                      new string[3], 
                                      new string[3] };

you can also write the code below. 你也可以写下面的代码。

Array lbl_array = Array.CreateInstance(typeof(string), i, j);

where 'i' is the number of rows and 'j' is the number of columns. 其中'i'是行数,'j'是列数。 using the 'typeof(..)' method you can choose the type of your array ie int, string, double 使用'typeof(..)'方法,您可以选择数组的类型,即int,string,double

There are many examples on working with arrays in C# here . 有在C#中的数组工作的许多例子在这里

I hope this helps. 我希望这有帮助。

Thanks, Damian 谢谢,达米安

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM