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];
}

      

+3


source to share


4 answers


What 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 is doing, but maybe you wanted it:

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

      

+2


source


There are two ways to select a 2-dimensional array

Method1:

Here you have initialized it with null strings

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

      

Method2:

Here you have initialized it with string literals.

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

      

You can see how to access it here:



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 result should look like below

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

      

So your code should look something like

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 conclusion

col1    col2    col3
nam1    nam2    nam3

      

+1


source


How 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
}

      

0


source


string[,] values =
{
  {"col1","val1"},
  {"col2","val2"},
  {"col3","val3"},
};

      

Check out how it looks in the debugger.

0


source







All Articles