仅在数组列表不存在时才将字符添加到数组列表中

Adding characters to an array list only if it does not already exist

下面的代码应该读取一个文本文件并计算文件中的所有 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

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

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

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

我建议您的算法虽然正确但性能不佳,您进行了太多不必要的比较,也许您应该 read/check 更多关于使用字典存储结果的信息。

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 的一种方法是结合 Linq 扩展方法 DistinctCount(首先找到所有不同的字符,然后得到每一个的计数):

foreach (var chr in data.Distinct())
{
    al.Add($"{chr} ({(int) chr}) {data.Count(c => c == chr)}");
}