简体   繁体   中英

How do I properly initialize and populate my multidimensional array?

How do I properly initialize and populate my multidimensional array?

string[] thisCanVaryInLength = new string[3] {"col1,nam1","col2,nam2","col3,nam3"};

string[,] columnsAndTheirNames = ?? //Unsure how to initialize

for (int i = 0; i < thisCanVaryInLength.Length; i++)
{
    columnsAndTheirNames[i, 0] = thisCanVaryInLength[0];
    columnsAndTheirNames[i, 1] = thisCanVaryInLength[1];
}

How about:

string[,] columnsAndTheirNames = new string[thisCanVaryInLength.Length, 2];

or with values:

string[,] columnsAndTheirNames = new string[,] { 
                 {"col1,nam1", "col2,nam2"},
                 {"col1,nam1", "col2,nam2"},
                 {"col1,nam1", "col2,nam2"}};

This is what your current code does, but maybe you wanted this:

string[,] columnsAndTheirNames = new string[,] { 
                 {"col1", "nam1"},
                 {"col2", "nam2"},
                 {"col3", "nam3"}};

There are 2 ways to allocate a 2 dimensional array

Method1:

Here you initialized it with null strings

string[,] columnsAndTheirNames1 = new string[2, 3];

Method2:

Here you initialized it with string literals.

string[,] columnsAndTheirNames = {
                                    { "row1-col1", "row1-col2"},
                                    { "row2-col1", "row2-col2"},
                                    { "row3-col1", "row3-col2"}
                                };

Here you can see how to access it:

for (int i = 0; i < columnsAndTheirNames.GetLength(0); ++i) {
    for (int j = 0; j < columnsAndTheirNames.GetLength(1); ++j) {
        Console.Write(columnsAndTheirNames[i, j] + "\t");
    }
    Console.WriteLine();
}

The output would like like below

row1-col1       row1-col2
row2-col1       row2-col2
row3-col1       row3-col2

So your code should be something like below

string[] thisCanVaryInLength = new string[3] { "col1,nam1", "col2,nam2", "col3,nam3" };

string[,] columnsAndTheirNames = new string[2, thisCanVaryInLength.Length];

for (int i = 0; i < thisCanVaryInLength.Length; i++) {
    var items = thisCanVaryInLength[i].Split(',');
    columnsAndTheirNames[0, i] = items[0];
    columnsAndTheirNames[1, i] = items[1];
}

for (int i = 0; i < columnsAndTheirNames.GetLength(0); ++i) {
    for (int j = 0; j < columnsAndTheirNames.GetLength(1); ++j) {
        Console.Write(columnsAndTheirNames[i, j] + "\t");
    }
    Console.WriteLine();
}

And the output is

col1    col2    col3
nam1    nam2    nam3

What about this?

private static string[,] columnsAndTheirNames = new string[,]
{
    { "col1", "nam1" },
    { "col2", "nam2" },
    { "col3", "nam3" }
};

private void foo() {
    Console.WriteLine(columnsAndTheirNames [0, 0]); // col1
    Console.WriteLine(columnsAndTheirNames [0, 1]); // nam1
    Console.WriteLine(columnsAndTheirNames [1, 0]); // col2
    Console.WriteLine(columnsAndTheirNames [1, 1]); // nam2
}
string[,] values =
{
  {"col1","val1"},
  {"col2","val2"},
  {"col3","val3"},
};

Look in debugger how it's looking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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