C#中的问题,对从文件中读取的数组进行排序 [英] Problem in C#, sorting an array that has been read from a file

查看:173
本文介绍了C#中的问题,对从文件中读取的数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用C#将文本文件中的名称列表读入数组,然后按字母顺序对数组进行排序。从文件中读取数组有效但使用Array.Sort对数组进行排序不起作用。



硬编码数组的值然后排序确实有效,但是从如下所示的文件中读取后,数组没有排序。



代码和输出如下所示。



我尝试过:



I have to read a list of names from a text file into an array using C#, then sort the array alphabetically. Reading the array from the file works but sorting the array using Array.Sort doesn't work.

Hard coding the values of the array and then sorting does work, but after reading from the file as shown below, the array is not sorted.

Code and output is shown below.

What I have tried:

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

namespace Files_Q3
{
    class Program
    {
        static void Main(string[] args)
        {

            // Data is read from Files_Q3.txt and stored in a String Array called dataFromFile[]........

            string fileName = @"Files_Q3.txt";
            Console.WriteLine("The contents of the file {0} is:", fileName);
            Console.WriteLine();

            string[] dataFromFile = new string[10];
            int index = 0;

            StreamReader streamReader = new StreamReader(fileName);
            using (streamReader)
            {
                string fileContents = streamReader.ReadToEnd();

                dataFromFile[index] = fileContents;

                Console.WriteLine(dataFromFile[index]);
                index++;
            }

            Console.WriteLine();
            Console.WriteLine();

            foreach(string name in dataFromFile)
            {
                Console.WriteLine(name);
            }

            // Sort the String array of names

            // Call Array.Sort method.
            Array.Sort(dataFromFile);

            foreach (string name in dataFromFile)
            {
                Console.WriteLine(name);
            }

            Console.WriteLine();
            Console.WriteLine();

        }
    }
}



输出为:



Files_Q3.txt文件的内容是:




Output is:

The contents of the file Files_Q3.txt is:

fred
bill
trevor
chris


fred
bill
trevor
chris


















fred
bill
trevor
chris

Press any key to continue . . .

推荐答案

string fileContents = streamReader.ReadToEnd(); // reads all the file's content into a single string

dataFromFile[index] = fileContents; // sets element 0 to the file content read above.
// so you only have one entry in the array.



什么你应该做的是使用一个循环来逐行读取文件,并在你去的时候递增 index 的值,从而将每一行存储在数组的单独元素中。



[edit]

更简单的替代方案是 File.ReadAllLines Method(String)(System.IO) [ ^ ]。

[/ edit]


What you should be doing is using a loop to read the file line by line, and incrementing the value of index as you go, thus storing each line in a separate element of the array.

[edit]
An easier alternative would be File.ReadAllLines Method (String) (System.IO)[^].
[/edit]


正如理查德所发现的那样,你在1中读取fie并将整个文件存储在1个字符串中,因此无需排序。

其余的代码是错误的,因为 dataFromFile 很大如果需要并且包含空元素,则必须更改代码以调整 dataFromFile 或跟踪要排序和显示的元素数量。

-----

您的代码没有按照您的预期行事,您不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
调试器中没有魔法,它不知道你应该做什么,它没有找到错误,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

在Visual Studio中调试C#代码 - YouTube [ ^ ]

调试器只是向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。
As spotted by Richard, you a reading the fie in 1 go and storing the whole file in 1 string, thus nothing to sort.
Rest of your code is wrong because dataFromFile is bigger than necessary and contain empty elements, you have to change your code to resize dataFromFile or keep track of number of elements to sort and display.
-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于C#中的问题,对从文件中读取的数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆