将数组代码转换为arraylist C#

时间:2013-09-27 20:02:04

标签: c# arrays arraylist

我需要把它变成一个数组列表而不是一个数组,我不知道如何开始。程序接受一个文件并逐行读取文件,并检查每个unque字符出现的次数。

       class Program
     {
    static void Main(string[] args)
    {
        // Parse arguments.
        if (args.Length != 2)
        {
            Console.WriteLine("Two arguments are needed.");
            return;
        }
        var inputFile = args[0];
        var outputFile = args[1];
        var asciiTable = new charfrequency[255];
        var input = File.ReadAllLines(inputFile);
        var output = "";

        Console.WriteLine(string.Format("The input file is:  <{0}>.", inputFile));
        Console.WriteLine(string.Format("The output file is: <{0}>.", outputFile));


       // creates the asci table.
         for (var i = 0; i < asciiTable.Length; i++)
        {
            asciiTable[i] = new charfrequency();
           asciiTable[i].setCharacter((char)i);
        }

        // Parse the file line by line.
        foreach (var line in input)
        {
            foreach (var character in line)
            {
                var key = (int)character;

                if (key < 255 && key >= 0)
                {
                    asciiTable[key].increment();
                }
            }
        }

        // Write to the output string.
        foreach (var charfrequency in asciiTable)
        {
            if (charfrequency.getFrequency() > 0)
            {
                output += charfrequency.toString();
                //string.Format("{0}[{1}]: {2}{3}", charfrequency.getCharacter(),            (int)charfrequency.getCharacter(), charfrequency.getFrequency(), Environment.NewLine);
            }
        }

        // Write the output string to the outputFile.
        File.WriteAllText(outputFile, output);
    }

1 个答案:

答案 0 :(得分:0)

如果使用LINQ命令,这可能会更容易。 Here是一篇解释如何将LINQ与文件一起使用的文章。这不是完整的答案,但可能会让您更接近它。