比较/计数 System.Collections.ArrayList 中的值
我正在清理 5 个文件以获得特定值。我预计不会有任何不同的值,但由于这是出于我自己的教育目的,我希望应用程序能够计算、比较和打印最流行的值。
例如:
ArrayList arrName = new ArrayList();
arrName.Add("BOB")
arrName.Add("JOHN")
arrName.Add("TOM")
arrName.Add("TOM")
arrName.Add("TOM")
我想要的结果是TOM,但作为新手,我真的不知道如何前进。
任何想法、建议或例子都将不胜感激。 谢谢。
I'm scrubbing 5 files for a specific value. I dont anticipate any different values, BUT since this is for my own educational purposes, I would like the application to count, compare and print the most popular value.
for example:
ArrayList arrName = new ArrayList();
arrName.Add("BOB")
arrName.Add("JOHN")
arrName.Add("TOM")
arrName.Add("TOM")
arrName.Add("TOM")
The result I would like is going to be TOM but being a novice, I really dont know how to move forward.
Any thoughts, suggestions or examples are greatly appreciated.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您可以使用 LINQ,则可以轻松地使用 LINQ 来完成此操作,使用类似于“
它将返回计数最高的值”或
default(Type)
的查询。如果计数相等,它将返回第一个计数最高的计数。您可以将该方法与泛型一起放入扩展中以供一般使用。如果您需要计数最高的所有值,例如您的列表是“BOB”、“JOHN”、“JOHN”、“TOM”、“TOM”,我想您可以改用此版本为了返回 JOHN 和 TOM:
You can easily do this with LINQ if you can use it, with a query similar to
It will return the value with the highest count, or
default(Type)
. In cases of equivalent counts, it will return the first one with he highest count. You can put that method in your extensions with generics for general usage.If you need all values that have the highest count, like if your list was
"BOB", "JOHN", "JOHN", "TOM", "TOM"
I guess you could use this version instead in order to return both JOHN and TOM:您没有指定您正在使用的 .Net / C# 版本,因此我将针对 C# 的每个版本进行处理:v1、v2 和 v3。
C# V1:
C# V2:
C# V3:
You didn't specify the version of .Net / C# you are using, so I'll treat this for each version of C#: v1, v2 and v3.
C# V1:
C# V2:
C# V3:
LINQ 非常适合这种任务。
首先,让我们定义我们正在做的事情:
此查询实现了上述内容:
该实现读起来非常类似于高级描述,这是一个很好的副作用声明性语法。这是每个部分的快速解释:
就像一个循环声明;
item
指的是循环变量。这将根据每个
item
的值将其放入一个组中。这会对每个组进行计数并对它们进行排序,以使最常见的位于第一个。如果有两个组的计数相同,则选择较小的值。 (由于每个组都包含所有相同的值,因此关键是计数的项目。)
这表示对于每个组,我们只需要计数的项目。
这将获取有序列表中的第一项(计数最高的项)。如果原始序列为空,则返回 null。
用法:
This is the kind of task for which LINQ is well-suited.
First, let's define what we're doing:
This query implements the above:
The implementation reads very much like the high-level description, a nice side-effect of the declarative syntax. Here is a quick explanation of each part:
is like a loop declaration;
item
refers to the loop variable.This puts each
item
in a group based on its value.This counts each group and sorts them such that the most frequent is first. If there are two groups with the same count, the lesser value is chosen. (As each group contains all the same values, the key is the counted item.)
This says that for each group, we just want the counted item.
This grabs the first item in the ordered list (the one with the highest count). If the original sequence is empty, null is returned.
Usage:
您可以使用字典 (.NET 2.0+) 来保存每个值的重复计数:
如果您使用的是 C# 3.0 (.NET 3.5 +),则使用:
You can use a Dictionary (.NET 2.0+) to hold the repeated count of each value:
If you're using C# 3.0 (.NET 3.5 +) then use:
要遍历循环,您可以使用
foreach
:要对值进行计数,您可以使用
Hashtable
,它将键映射到值。键可以是名称,值可以是您在列表中看到该名称的次数。To move through the loop, you can use a
foreach
:And to count the values, you can use a
Hashtable
, which maps keys to values. The key can be a name, and the value can be how many times you've seen that name in the list.