【问题标题】:Creating Rows and Columns to List Array创建行和列以列出数组
【发布时间】:2016-02-11 02:33:28
【问题描述】:

我有一个包含 30 行和 5 列的 csv 文件。我使用流阅读器将其读入我的应用程序,然后放入一个数组,排序,现在被写出到文件中。我有 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.

谢谢, 乔

【问题讨论】:

    标签: c# arrays for-loop console-application


    【解决方案1】:

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

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

    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

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

    【讨论】:

    • 这对订购非常有意义。谢谢!
    【解决方案2】:

    你的数组索引从 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);
      }
    

    【讨论】:

    • 我跳过 0,从 1 开始。有时很难记住从 0 开始,自然,所以我总是将位置 0 归零。
    • @Joe 我会说,如果其他人会看到/使用/编辑您的代码,这可能不是一个好习惯,因为大多数程序员认为index start from 0 很直观 :),但对每个人来说都是他自己的...我的答案对你想要的有用吗?除了索引问题。
    • 这可能会在最后丢失一些记录。
    • @Joe 是的。解决方法是对其进行不同的排序。
    • 是和不是。我通过将 scorArray[n] 更改为 [n + 1] 来修复数组调用,因为我没有使用 0 索引。所以一切都打印出了很好的数字。它只是没有按照我想要的方式打印出来。我有 5 列分数。第一列从 97.05 开始,然后垂直下降到 96.52 和 93.16 等。此代码打印每个水平下降的数字。那就是我需要修复的地方。
    【解决方案3】:

    不使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-11
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      相关资源
      最近更新 更多