创建行和列以列出数组

本文关键字:数组 创建 | 更新日期: 2023-09-27 18:34:04

>我有一个 csv 文件,有 30 行和 5 列。我使用流读取器将其读取到我的应用程序中,然后放入数组中,排序,现在被写出到文件中。我有 5 列显示"分数",后面跟着破折号。我的问题是我需要在排序后以 5 列输出我的数组。我有一个 for 循环,它遍历数组的长度。我真的需要它来迭代 30 行和 5 列,但我不确定该怎么做。很抱歉,我是 C# 和 linq 等功能的新手。这是我的输出和 for 循环的简短版本。如果需要进一步澄清,请告诉我。

int n;
  fileOut.WriteLine();
  fileOut.WriteLine("Score    Score    Score    Score    Score");
  fileOut.WriteLine("-----    -----    -----    -----    -----");
  for (n = 1; n <= numOfScores; n++)
  fileOut.WriteLine("{0,4:f}", scoreArray[n]);
  fileOut.WriteLine();

我知道必须有一个简单的方法来做到这一点,只是不确定。

当前输出如下所示:

Score    Score    Score    Score    Score
-----    -----    -----    -----    -----
97.05
96.52
93.16
92.44
91.05
90.66
90.59
//etc, etc, through entire array, only one line when it needs to be in each column.

谢谢乔

创建行和列以列出数组

要正确地执行此操作,我首先需要处理您的数据。您有一个排序的分数数组。您希望将其输出到列,其中数据在填充之前先填充,通过输出流(控制台(,该输出流强烈偏向于在写下之前写入

这意味着为了避免输出流中的回溯(缓慢且棘手(,我们需要在将数据写入控制台之前处理内存中的数据:

double[] scores = ...; //sorted data in here
// now some constants
const int cols = 5;
const int colWidth = 5;
const int colSpace = 4;
const string Header = "Score";
//figure out how many rows we need
int remainder = scores.Length % cols;
int rows = scores.Length / cols + (remainder > 0?1:0);
//organize the data into a 2d structure that matches the output
// I chose an array of arrays rather than 2d array so I can pass individual
//   arrays to format function later on.
var data = new string[rows][];
int i = 0; //score index
for (int c = 0;c < cols;c++)
{
    for (int r = 0;r < rows && i < scores.Length; r++)
    {
        //make sure nested array exists and is pre-populated with empty strings (string.Format() will choke later if we leave these as nulls)
        data[r] = data[r] ?? Enumerable.Repeat("", cols).ToArray();
        //skip this cell if it's at the bottom row of a later column in an unbalanced array
        if (remainder > 0 && r == rows - 1 && c >= remainder) continue;
        data[r][c] = scores[i].ToString();
        i++;
    }
}
//write the header
var format = string.Join("", Enumerable.Repeat("{0,-" + (colWidth + colSpace) + "}", cols));
Console.WriteLine(format, Header);
Console.WriteLine(format, new string('-', colWidth));
//write the data
format = string.Join("", Enumerable.Range(0,cols).Select(i => string.Format("{{{0},-{1}}}{2}", i, colWidth, new string(' ',colSpace))).ToArray());
for (int i = 0; i < rows; i++)
    Console.WriteLine(format, data[i]);

使用此示例数据运行代码:

double[] scores = { 97.05,96.52,93.16,92.44,91.05,90.66,90.59,19.1, 18.4, 16.8, 11.1, 13.8, 12.2, 7.9, 8.1, 11.0, 14.5, 16.6, 21.3, 16, 17.9};
scores = scores.OrderBy(s => s*-1).ToArray();

我得到这个结果:

分数分数分数分数   -----    -----    -----    -----    -----   97.05    90.66    18.4     16       11.1    96.52    90.59    17.9     14.5     11      93.16    21.3     16.8     13.8     8.1     92.44    19.1     16.6     12.2     7.9     91.05                                                  

这里很酷的事情是,此代码可让您轻松调整所需的列数。

数组索引以 1 开头有点奇怪。它应该以 0 开头(除非您跳过第一个索引上的某些内容?

这可能就是您要找的。确保索引没有越界(数组可被 5 整除(。

  fileOut.WriteLine();
  fileOut.WriteLine("Score    Score    Score    Score    Score");
  fileOut.WriteLine("-----    -----    -----    -----    -----");
  for (int n = 0; n < numOfScores; n+=5) // why do you need to declare n outside?
  {
            fileOut.WriteLine("{0,4:f} {1,4:f} {2,4:f} {3,4:f} {4,4:f}{5}", scoreArray[n],scoreArray[n+1],scoreArray[n+2],scoreArray[n+3],scoreArray[n+4],Environment.NewLine);
  }
无需使用

Console.WriteLine,只需使用 Console.Write。 每次调用 WriteLine 时,都会在输入后打印一个新行字符。

  fileOut.WriteLine();
  fileOut.WriteLine("Score    Score    Score    Score    Score");
  fileOut.WriteLine("-----    -----    -----    -----    -----");
  for (n = 1; n <= numOfScores; n++)
  fileOut.Write("{0,4:f}", scoreArray[n]);
  fileOut.WriteLine(); //do this so there is a new line after all the data