如何在没有 linq 的情况下获取 List<> 中元素的计数

我想计算数组中的元素但没有 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


Smart猫小萌
浏览 193回答 3
3回答

qq_遁去的一_1

这是我可以考虑使用的一种方法Dictionary<string, int>:public static Dictionary<string, int> GetObjectCount(List<string> items){&nbsp; &nbsp; // Dictionary object to return&nbsp; &nbsp; Dictionary<string, int> keysAndCount = new Dictionary<string, int>();&nbsp; &nbsp; // Iterate your string values&nbsp; &nbsp; foreach(string s in items)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Check if dictionary contains the key, if so, add to count&nbsp; &nbsp; &nbsp; &nbsp; if (keysAndCount.ContainsKey(s))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keysAndCount[s]++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Add key to dictionary with initial count of 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keysAndCount.Add(s, 1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; &nbsp;return keysAndCount;}然后取回结果并打印到控制台:Dictionary<string, int> dic = GetObjectCount(list);//Print to Consoleforeach(string s in dic.Keys){&nbsp; &nbsp;Console.WriteLine(s + " has a count of: " + dic[s]);}

犯罪嫌疑人X

我不知道你为什么要为此寻找 LINQ less 的解决方案,因为这可以很容易和有效地完成。我强烈建议您使用它并按照以下方式操作:var _group = list.GroupBy(i => i);string result = "";foreach (var grp in _group)&nbsp; &nbsp;result += grp.Key + ": " + grp.Count() + Environment.NewLine;MessageBox.Show(result);否则,如果你真的无法使用 LINQ,你可以像下面那样做:Dictionary<string, int> listCount = new Dictionary<string, int>();foreach (string item in list)&nbsp; &nbsp; if (!listCount.ContainsKey(item))&nbsp; &nbsp; &nbsp; &nbsp; listCount.Add(item, 1);&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; listCount[item]++;string result2 = "";foreach (KeyValuePair<string, int> item in listCount)&nbsp; &nbsp; result2 += item.Key + ": " + item.Value + Environment.NewLine;MessageBox.Show(result2);

九州编程

你可以尝试这样的事情:public int countOccurances(List<string> inputList, string countFor){&nbsp; &nbsp; // Identifiers used are:&nbsp; &nbsp; int countSoFar = 0;&nbsp; &nbsp; // Go through your list to count&nbsp; &nbsp; foreach (string listItem in inputList)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;// Check your condition&nbsp; &nbsp; &nbsp; &nbsp;if (listItem == countFor)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;countSoFar++;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp; // Return the results&nbsp; &nbsp; return countSoFar;}这将为您提供任何刺痛的计数。一如既往,有更好的方法,但这是一个好的开始。或者如果你想要:public string countOccurances(List<string> inputList, string countFor){&nbsp; &nbsp; // Identifiers used are:&nbsp; &nbsp; int countSoFar = 0;&nbsp;&nbsp; &nbsp; string result = countFor;&nbsp; &nbsp; // Go through your list to count&nbsp; &nbsp; foreach (string listItem in inputList)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;// Check your condition&nbsp; &nbsp; &nbsp; &nbsp;if (listItem == countFor)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;countSoFar++;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp; // Return the results&nbsp; &nbsp; return countFor + " = " countSoFar;}或者更好的选择:private static void CountOccurances(List<string> inputList, string countFor)&nbsp;{&nbsp; &nbsp; int result = 0;&nbsp; &nbsp; foreach (string s in inputList)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (s == countFor)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; Console.WriteLine($"There are {result} occurrances of {countFor}.");}
打开App,查看更多内容
随时随地看视频慕课网APP