简体   繁体   English

矩阵形式的锯齿状二维数组C#

[英]Jagged 2d array in matrix form c#

case 8:
                    string[][] Ws1Data = new[]
{
File.ReadAllLines(@"\university\Assignment2Alg\files\YearSorted.txt"),
File.ReadAllLines(@"\university\Assignment2Alg\files\MonthSorted.txt"),
File.ReadAllLines(@"\university\Assignment2Alg\files\WS1_AFSorted.txt"),
File.ReadAllLines(@"\university\Assignment2Alg\files\WS1_RainSorted.txt"),
File.ReadAllLines(@"\university\Assignment2Alg\files\WS1_SunSorted.txt"),
File.ReadAllLines(@"\university\Assignment2Alg\files\WS1_TMaxSorted.txt"),
File.ReadAllLines(@"\university\Assignment2Alg\files\WS1_TMinSorted.txt"),



};
                    for (int i = 0; i < Ws1Data.Length; i++)
                    {
                        string[] innerArray = Ws1Data[i];
                        for (int a = 0; a < innerArray.Length; a++)
                        {
                            Console.WriteLine(innerArray[a] + " ");
                        }
                        Console.WriteLine();
                    }


                    break;

As you can see I have an array of text files which all have data inside them, I want the data to be showed in the format of columns and not below each other. 如您所见,我有一个文本文件数组,里面都有数据,我希望数据以列的格式而不是彼此之间的格式显示。

So it shows like this. 如此显示。

1920 january 20.2 1920年1月20.2日

1923 february 21.0 1923年2月21.0日

instead of this 代替这个

1920 1920年

1923 1923年

january 一月

february 二月

20.2 20.2

24.2 24.2

Im stuck and cant find out how to do it, Probably really easy once its pointed out, but you dont learn if you dont ask. 我被困住了,找不到解决方法,一旦指出,可能真的很容易,但是如果您不问,您就不会学。

Do your files have the same number of lines? 您的文件行数是否相同?

If so, something like this should work: 如果是这样,类似这样的事情应该起作用:

        int nRows = Ws1Data[0].Length;
        int nColumns = Ws1Data.Length;
        string tempString = "";
        for (int i = 0; i < nRows; i++)
        {
            tempString = "";
            for (int j = 0; j < nColumns; j++)
            {
                tempString = tempString + Ws1Data[j][i];
                tempString = tempString + " ";
            }
            Console.WriteLine(tempString);
        }

Depending on the number of columns, you can have a stack overflow due to tempString. 根据列数的不同,由于tempString可能会导致堆栈溢出。 If so, just allocate the size statically 如果是这样,只需静态分配大小

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

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