仅当数组列表不存在时才添加字符

时间:2018-11-30 00:38:50

标签: c# arraylist element infinite-loop

下面的代码应该读取一个文本文件并计算该文件中的所有ASCII字符并累加频率。然后,它必须将字符,ASCII值和频率写入输出文件。代码如下:

class CharacterFrequency
{
    char ch;
    int frequency;
    public char getCharacter()
    {
        return ch;
    }
    public void setCharacter(char ch)
    {
        this.ch = ch;
    }
    public int getfrequency()
    {
        return frequency;
    }
    public void setfrequency(int frequency)
    {
        this.frequency = frequency;
    }

    static void Main()
    {
        Console.WriteLine("Enter the file path");
        var InputFileName = Console.ReadLine();

        Console.WriteLine("Enter the outputfile name");
        var OutputFileName = Console.ReadLine();

        StreamWriter streamWriter = new StreamWriter(OutputFileName);
        string data = File.ReadAllText(InputFileName);
        ArrayList al = new ArrayList();

        //create two for loops to traverse through the arraylist and compare
        for (int i = 0; i < data.Length; i++)
        {
            int k = 0;
            int f = 0;

            for (int j = 0; j < data.Length; j++)
            {
                if (data[i].Equals(data[j]))
                {
                    f++;
                }
            }

            if (!al.Contains(data[i]))
            {
                al.Add(data[i] + "(" + (int)data[i] + ")" + f + " ");
            }
            else
            {
                k++;
            }

            //i added the below if statement but it did not fix the issue
            foreach (var item in al)
            {
                streamWriter.WriteLine(item);
            }
        }

        streamWriter.Close();
    }
}

代码可以编译并正常运行,但是输出文件不正确。它添加了已经审查过的信件。我添加了一个带有输出文件的图像,该图像显示了它正在创建的错误输出。 -> enter image description here

如何检查数组列表中是否已存在字符?我使用的方式无法正常工作,并且我已经为此工作了几周,但没有成功。我尝试使用调试器,但是由于代码仍在运行并正确编译,因此不会出现该问题。

2 个答案:

答案 0 :(得分:0)

您的算法有效,但是您在循环中写入文件时正在复制输出,这就是为什么在结果中看到重复的原因。如果将代码移到循环外,应该没问题。

                foreach (var item in al)
                {
                    streamWriter.WriteLine(item);
                }

我建议您正确执行算法时,性能会很差,您进行了过多不必要的比较,也许您应该阅读/检查更多有关使用字典存储结果的信息。

答案 1 :(得分:0)

ArrayList不太适合此任务,实际上ArrayLists不再使用。如果有人告诉您您必须使用ArrayList进行此操作

字典将是此数据的更好容器。您可以将字符用作键,将计数用作值。

这是执行此操作的一种方法:

var inputPath = @"c:\temp\temp.txt";
var outputPath = @"c:\temp\results.txt";
var data = new Dictionary<char, int>();

// For each character in the file, add it to the dictionary
// or increment the count if it already exists
foreach (var character in File.ReadAllText(inputPath))
{
    if (data.ContainsKey(character)) data[character]++;
    else data.Add(character, 1);
}

// Create our results summary
var results = data.ToList()
    .Select(item => $"{item.Key} ({(int) item.Key}) {item.Value}");

// Write results to output file
File.WriteAllLines(outputPath, results);

如果您必须使用ArrayList(没有人再使用过,但是由于某些原因您说要这样做),那么它仅对存储结果有用,但是没有跟踪计数。

使用ArrayList的一种方法是与LinqDistinct扩展方法结合使用(首先查找所有不同的字符,然后获取计数):

Count
相关问题