C# 如何在没有linq的情况下获取列表中元素的计数
我想计算数组中的元素数,但不需要linq 例如:C# 如何在没有linq的情况下获取列表中元素的计数,c#,C#,我想计算数组中的元素数,但不需要linq 例如: string a = "cat"; string b = "dog"; string c = "cat"; string d = "horse"; var list = new List<string>(); list.Add(a); list.Add(b); list.Add(c); list.Add(d); 及 理想的结果是:猫=2,狗=1,马=1。您可以尝试以下方法: public int countOccurances(L
string a = "cat";
string b = "dog";
string c = "cat";
string d = "horse";
var list = new List<string>();
list.Add(a);
list.Add(b);
list.Add(c);
list.Add(d);
及
理想的结果是:猫=2,狗=1,马=1。您可以尝试以下方法:
public int countOccurances(List<string> inputList, string countFor)
{
// Identifiers used are:
int countSoFar = 0;
// Go through your list to count
foreach (string listItem in inputList)
{
// Check your condition
if (listItem == countFor)
{
countSoFar++;
}
}
// Return the results
return countSoFar;
}
这会让你知道你给它刺的次数。总是有更好的方法,但这是一个好的开始
或者,如果您愿意:
public string countOccurances(List<string> inputList, string countFor)
{
// Identifiers used are:
int countSoFar = 0;
string result = countFor;
// Go through your list to count
foreach (string listItem in inputList)
{
// Check your condition
if (listItem == countFor)
{
countSoFar++;
}
}
// Return the results
return countFor + " = " countSoFar;
}
或者更好的选择:
private static void CountOccurances(List<string> inputList, string countFor)
{
int result = 0;
foreach (string s in inputList)
{
if (s == countFor)
{
result++;
}
}
Console.WriteLine($"There are {result} occurrances of {countFor}.");
}
以下是我可以想到的使用字典的一种方法: 然后返回结果并打印到控制台:
Dictionary<string, int> dic = GetObjectCount(list);
//Print to Console
foreach(string s in dic.Keys)
{
Console.WriteLine(s + " has a count of: " + dic[s]);
}
解决您的问题的简单方法是 林克在引擎盖下做这件事。然而,除非这是为了优化大型列表或学习经验,否则如果您知道如何使用Linq,那么Linq应该是您的首选。它的存在是为了让你的生活更轻松 这种方法的另一个实现是简化所需的调用数量,只需在方法中写入输出:
string[] myStrings = new string[] { "Cat", "Dog", "Horse", "CaT", "cat", "DOG" };
CountTerms(myStrings, "cat", "dog");
Console.ReadKey();
static void CountTerms(string[] strings, params string[] terms) {
foreach (string term in terms) {
int result = 0;
foreach (string s in strings)
if (s == term)
result++;
Console.WriteLine($"There are {result} instances of {term}");
}
}
话虽如此,我强烈推荐Ryan Wilson的答案。他的版本简化了手头的任务。他的实现的唯一缺点是,如果您以一种独特的方式实现它,那么List.Countc=>c==cat将以这种方式实现。我不确定您为什么要为此寻找LINQ-less解决方案,因为它可以非常轻松有效地完成这项工作。我强烈建议您使用它,并按如下方式操作:
var _group = list.GroupBy(i => i);
string result = "";
foreach (var grp in _group)
result += grp.Key + ": " + grp.Count() + Environment.NewLine;
MessageBox.Show(result);
否则,如果您确实无法使用LINQ,您可以按以下方式执行:
Dictionary<string, int> listCount = new Dictionary<string, int>();
foreach (string item in list)
if (!listCount.ContainsKey(item))
listCount.Add(item, 1);
else
listCount[item]++;
string result2 = "";
foreach (KeyValuePair<string, int> item in listCount)
result2 += item.Key + ": " + item.Value + Environment.NewLine;
MessageBox.Show(result2);
Linq应该使开发人员的生活更轻松。无论如何,你可以做这样的事情:
string a = "cat";
string b = "dog";
string c = "cat";
string d = "horse";
var list = new List<string>();
list.Add(a);
list.Add(b);
list.Add(c);
list.Add(d);
var result = GetCount(list);
Console.WriteLine(result);
Console.ReadLine();
static string GetCount(List<string> obj)
{
string result = string.Empty;
int cat = 0;
int dog = 0;
int horse = 0;
foreach (var item in obj)
{
switch (item)
{
case "dog":
dog++;
break;
case "cat":
cat++;
break;
case "horse":
horse++;
break;
}
}
result = "cat = " + cat.ToString() + " dog = " + dog.ToString() + " horse = " + horse.ToString();
return result;
}
到目前为止你试过什么?为什么没有linq?Linq旨在让您的生活更轻松:为什么要让它更难?list.Count属性,否则,循环三次并在一次迭代中计数。我投票将这个问题作为离题题来结束,因为简单地询问代码而不显示出OP解决它的任何努力。@FrankerZ Linq也很慢,这取决于它必须迭代的数据的大小和调用的操作。你为什么要使用字典?只是好奇?Linq只计算引擎盖下的每个对象,并返回结果。@davisj1691他需要一个结果集,这样他就可以打印每个对象类型和对象计数,因此通过使用字典,它将每个项目作为键返回,并将计数作为其值。也可以通过键快速查找,但是考虑较大列表的开销吗?使用计数系统不是更简单吗?我可以告诉你,一个数组比一个拥有大数据集的字典要快。我不同意,这一点也不过分。这是编写此练习的自然方式,除非您只计算列表中的一项。@davisj1691没问题。试着让事情更有效率没什么错,我想我是在看到OP的预期结果时才想到的。不要因为问题而使用toLower或toUpper。特别是当s.EqualSearchTerm、StringComparison.OrdinalIgnoreCase在所有情况下都有效时。@ErikPhilips您包含的链接引入了一个问题,如果我没记错的话,这个问题已经在.NET中解决了。请随意查看string.toupper不变量。我相信托洛尔也有不变的版本。此外,我将从我的帖子中删除ToUpper,因为它会大大降低比较速度。这根本不是问题。这就是.Net以前、现在和将来的工作方式。.ToUpperInvariant方法只解决了一些问题,如要正确比较,应使用String.ComparestrA.ToUpperInvariant、strB.ToUpperInvariant、StringComparison.Ordinal;但这仍然比以前慢,所以真的没有理由使用这种方法进行比较。@ErikPhilips在我诚实的观点中,比较应该在没有修改的情况下进行平等性测试。我知道有时修改测试字符串和搜索词进行比较是有用的,例如;要确定字符串是否包含某一组字符,但在大多数情况下,如果可以执行abc==abc,则执行该操作,因为它总是更准确、更快。确定字符串是否包含某一组字符仍然不需要Toupper或ToLower,这只会在可以使用strA.IndexOfstrB时增加额外开销,StringComparison.OrdinalIgnoreCase>=0。我看没有理由使用ToUpper/ToLower。谢谢我的兄弟,这项工作
Dictionary<string, int> listCount = new Dictionary<string, int>();
foreach (string item in list)
if (!listCount.ContainsKey(item))
listCount.Add(item, 1);
else
listCount[item]++;
string result2 = "";
foreach (KeyValuePair<string, int> item in listCount)
result2 += item.Key + ": " + item.Value + Environment.NewLine;
MessageBox.Show(result2);
string a = "cat";
string b = "dog";
string c = "cat";
string d = "horse";
var list = new List<string>();
list.Add(a);
list.Add(b);
list.Add(c);
list.Add(d);
var result = GetCount(list);
Console.WriteLine(result);
Console.ReadLine();
static string GetCount(List<string> obj)
{
string result = string.Empty;
int cat = 0;
int dog = 0;
int horse = 0;
foreach (var item in obj)
{
switch (item)
{
case "dog":
dog++;
break;
case "cat":
cat++;
break;
case "horse":
horse++;
break;
}
}
result = "cat = " + cat.ToString() + " dog = " + dog.ToString() + " horse = " + horse.ToString();
return result;
}