繁体   English   中英

读取数组2D并输出为表C#

[英]Read array 2D and output as table C#

我是C#编程的新手,我试图以表格的形式显示矩阵的全部内容,但是到目前为止,我得到的是读取枚举并从矩阵中读取一行。 相反,我需要从矩阵中读取多行并将其输出为表格。

当我运行程序时,我会在行中插入两次数据,并且输出应该是表中的该数据,但是只显示了一行。 这是代码:

static int getInsertIndex(string[,] matrix)
        {
            for (int j = 0; j < matrix.GetLength(0); j++)
            {
                if (string.IsNullOrEmpty(matrix[j, 0])) return j;
            }

            return -1;
        }
        private static void InsertData<T>(string[,] matrix)
        {

            // int newId = generateId(ref id);
            int n = getInsertIndex(matrix), id = 1;

            matrix[n, 0] = Convert.ToString(id++);
            int x = matrix.GetLength(1) - 1;
            matrix[n, x] = "true";

            for (var j = 1; j < matrix.GetLength(1); j++)
            {
                do
                {
                    Console.Write($"\nInsert {GetHeader<T>(j)}: ");
                    matrix[0, j] = Console.ReadLine();
                } while (string.IsNullOrEmpty(matrix[0, j]));
            }
        }

        private static void ListData<T>(string[,] matrix)
        {
            var array = new string[matrix.GetUpperBound(1)];

            for (var l = 0; l < matrix.GetLength(0); l++)
            {
                for (var i = 0; i < array.Length; i++)
                {
                    array[i] = matrix[0, i];
                }
            }

            PrintRow(array);
            PrintLine();
        }

        private static string GetHeader<T>(int i) => Enum.GetName(typeof(T), i);

        private static void ShowHeader<T>(string[,] matrix)
        {
            var array = new string[matrix.GetUpperBound(1)];

            for (var i = 0; i < array.Length; i++)
            {
                array[i] = GetHeader<T>(i);
            }

            PrintLine();
            PrintRow(array);
            PrintLine();
        }

        private static void PrintLine()
        {
            Console.WriteLine(new string('-', Console.WindowWidth - 1));
        }

        private static void PrintRow(IReadOnlyCollection<string> columns)
        {
            var width = (Console.WindowWidth - 1 - columns.Count) / columns.Count;
            var row = columns.Aggregate("|", (current, column) => current + AlignCentre(column, width) + "|");
            Console.WriteLine(row);
        }

        static string AlignCentre(string text, int width)
        {
            text = text.Length > width ? text.Substring(0, width - 3) + "..." : text;

            return string.IsNullOrEmpty(text)
                ? new string(' ', width)
                : text.PadRight(width - (width - text.Length) / 2).PadLeft(width);
        }

        enum ClientHeader { Id, Name, Surname, Addres, CodPostal, Telephone, Email, State };

        private static void Main()
        {
            var client = new string[4, 7];
            InsertData<ClientHeader>(client);

            Console.Clear();
            InsertData<ClientHeader>(client);

            ShowHeader<ClientHeader>(client);
            ListData<ClientHeader>(client);

        }
    }
}

有两个问题:

1)在InsertData()函数中,您要更新第n行。

更换

matrix[0, j] = Console.ReadLine();

通过

matrix[n, j] = Console.ReadLine();

2)在ListData()函数中要显示每一行,因此需要将数组变量移到第一个for循环中。 array[i] = matrix[l, i]替换array[i] = matrix[0, i] ,因为您正在显示第l行。

private static void ListData<T>(string[,] matrix)
{
    for (var l = 0; l < matrix.GetLength(0); l++)
    {
        var array = new string[matrix.GetUpperBound(1)];

        for (var i = 0; i < array.Length; i++)
        {
            array[i] = matrix[l, i];
        }

        PrintRow(array);
    }

    PrintLine();
}

尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication1
{
    public class Program
    {
        private static void Main()
        {
            List<List<string>> data = new List<List<string>>() {
                new List<string>() { "Name", "Age", "Weight"},
                new List<string>() { "John", "33", "180"},
                new List<string>() { "Mary", "32", "125"},
                new List<string>() { "Harry", "40", "200"}
            };

            DataTable dt = new DataTable();
            for (int i = 0; i < data.Count; i++)
            {
                if (i == 0)
                {
                    foreach (string col in data[i])
                    {
                        dt.Columns.Add(col);
                    }
                }
                else
                {
                    dt.Rows.Add(data[i].ToArray());
                }
            }

        }
    }

}

暂无
暂无

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

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