简体   繁体   中英

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

1923 february 21.0

instead of this

1920

1923

january

february

20.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. If so, just allocate the size statically

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