二维数组到C#中的表? [英] 2D array to table in c#?

查看:62
本文介绍了二维数组到C#中的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将来自此表的数据放入一个数组,然后在控制台中将该数组打印为格式化的表.这是我从 http://puu.sh/oqV8f/7d982f2665.jpg;我只需要使数组输出行和列而不是列表即可.到目前为止,我已经知道了:

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();
        }
    }
}

有什么想法吗?

推荐答案

Foreach 将所有元素显示为列表.在这种情况下,您需要输出如下:

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();

如果出于任何原因要使用 foreach ,也可以将表声明为[] []数组.但是在两种方式中,您都必须创建2个循环:

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();
}

这篇关于二维数组到C#中的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆