简体   繁体   中英

2D array to table in c#?

I need to put the data from this table into an array and then make the array print as a formatted table in the console. Here's the table that I got my data from http://puu.sh/oqV8f/7d982f2665.jpg ; I just need to make the array output rows and columns instead of a list. I have this so far:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zumba1
{
    class Zumba
    {
        static void Main(string[] args)
        { //Recreated the data in the table for the zumba section, added each row, and each column.
            string[,] schedule = new string [8, 6] { { "1:00", "3:00", "5:00", "7:00", "TOTAL", "", },
                                 {"Monday", "12", "10", "17", "22", "244",   },
                                 {"Tuesday", "11", "13", "17", "22", "252",},
                                 {"Wednesday", "12", "10", "22", "22", "264",},
                                 {"Thursday", "9", "14", "17", "22", "248",},
                                 {"Friday", "12", "10", "21", "12", "220",},
                                 {"Saturday", "12", "10", "5", "10", "148"},
                                 {" ", " ", " ", " ", " ","1376",}};
            foreach (string i in schedule)
            {
                Console.WriteLine(i.ToString());
            }
            Console.ReadKey();
        }
    }
}

Any Ideas?

Foreach on a [,] array gives you all elements as a list, as you noticed. In this case you need to output as follow:

for (int x0 = 0; x0 < schedule.GetLength(0); x0++)
{
    for (int x1 = 0; x1 < schedule.GetLength(1); x1++)
    {
        Console.Write("{0}\t", schedule[x0, x1]);
    }
    Console.WriteLine();
}
Console.ReadKey();

If you want to use foreach for any reason, you can also declare your table as [][] array. But in both ways you have to create 2 loops:

string[][] schedule = new string[][] {
                                    new string[] { "1:00", "3:00", "5:00", "7:00", "TOTAL", "", },
                                    new string[] {"Monday", "12", "10", "17", "22", "244",   },
                                    new string[] {"Tuesday", "11", "13", "17", "22", "252",},
                                    new string[] {"Wednesday", "12", "10", "22", "22", "264",},
                                    new string[] {"Thursday", "9", "14", "17", "22", "248",},
                                    new string[] {"Friday", "12", "10", "21", "12", "220",},
                                    new string[] {"Saturday", "12", "10", "5", "10", "148"},
                                    new string[] {" ", " ", " ", " ", " ","1376",}
        };
foreach (string[] line in schedule)
{
    foreach (string i in line)
        Console.Write("{0}\t", i);
    Console.WriteLine();
}

If you use a monospaced font in the console you can adjust where each thing displays by putting more spaces if necessary

For example for the members corresponding to the the 1st and 2nd row and 1st of second column this would be the thing to calculate:

Largest word is Wednesday that is 9 letters, on first row first column I should put 9 espaces as there would be a blank. Then you might put four spaces as a separator between columns, then for the second column you calculate that 1:00 is the largest string so for 12 you would add 2 extra spaces, and so on.

Using a tab instead of some spaces is also likely to work but if the table ends up having some string that is much larger than one in another column it won't work.

Hope it helps.

Got it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zumba1
{
    class Zumba
    {
        static void Main(string[] args)
        { //Recreated the data in the table for the zumba section, added each row, and each column. Worked on formatting.
            string[,] schedule = new string[8, 6] { { "\t\t1:00", "3:00", "5:00", "7:00", "TOTAL", "", },
                                 {"Monday", "\t12", "10", "17", "22", "$244",   },
                                 {"Tuesday", "\t11", "13", "17", "22", "$252",},
                                 {"Wednesday", "12", "10", "22", "22", "$264",},
                                 {"Thursday", "9", "14", "17", "22", "$248",},
                                 {"Friday", "\t12", "10", "21", "12", "$220",},
                                 {"Saturday", "12", "10", "5", "10", "$148"},
                                 {" ", " ", " ", " ", " ","\t$1376",}};
            //Nested for loops to print in a table-style format.
            for (int i = 0; i < schedule.GetLength(0); i++)

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

            }
     Console.ReadLine();
            }
        }
    }
}

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