100
100
110
110
100
115
110
100
120
110
100
请问如何统计100有多个少,110有多少个等等.
谢谢.
7 个解决方案
#1
我的意思是不用for,list中有这个方法吗?
#2
MSDN中一查 什么都知道了
#3
List.FindAll
#4
List<int> list = new List<int>() { 12 , 12 , 13, 13, 14, 15, 15, 15 };
var g = list.GroupBy(i => i);
foreach (var item in g)
{
Console.WriteLine("Value:{0} , Count:{1}", item.Key, item.Count());
}
#5
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Oviraptor");
dinosaurs.Add("Velociraptor");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Dilophosaurus");
dinosaurs.Add("Gallimimus");
dinosaurs.Add("Triceratops");
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nTrueForAll(EndsWithSaurus): {0}",
dinosaurs.TrueForAll(EndsWithSaurus));
Console.WriteLine("\nFind(EndsWithSaurus): {0}",
dinosaurs.Find(EndsWithSaurus));
Console.WriteLine("\nFindLast(EndsWithSaurus): {0}",
dinosaurs.FindLast(EndsWithSaurus));
Console.WriteLine("\nFindAll(EndsWithSaurus):");
List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);
foreach(string dinosaur in sublist)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\n{0} elements removed by RemoveAll(EndsWithSaurus).",
dinosaurs.RemoveAll(EndsWithSaurus));
Console.WriteLine("\nList now contains:");
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nExists(EndsWithSaurus): {0}",
dinosaurs.Exists(EndsWithSaurus));
}
// Search predicate returns true if a string ends in "saurus".
private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}
}
#6
list.FindAll(delegate(int n) { return n == 100; }).Count
#7
List<int> list = new List<int>();
list.Add(100);
list.Add(100);
list.Add(100);
list.Add(100);
list.Add(100);
MessageBox.Show(list.FindAll((ex) => { return ex == 100; }).Count.ToString());
嘿嘿,可行
list.Add(100);
list.Add(100);
list.Add(100);
list.Add(100);
list.Add(100);
MessageBox.Show(list.FindAll((ex) => { return ex == 100; }).Count.ToString());
嘿嘿,可行
#1
我的意思是不用for,list中有这个方法吗?
#2
MSDN中一查 什么都知道了
#3
List.FindAll
#4
List<int> list = new List<int>() { 12 , 12 , 13, 13, 14, 15, 15, 15 };
var g = list.GroupBy(i => i);
foreach (var item in g)
{
Console.WriteLine("Value:{0} , Count:{1}", item.Key, item.Count());
}
#5
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Oviraptor");
dinosaurs.Add("Velociraptor");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Dilophosaurus");
dinosaurs.Add("Gallimimus");
dinosaurs.Add("Triceratops");
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nTrueForAll(EndsWithSaurus): {0}",
dinosaurs.TrueForAll(EndsWithSaurus));
Console.WriteLine("\nFind(EndsWithSaurus): {0}",
dinosaurs.Find(EndsWithSaurus));
Console.WriteLine("\nFindLast(EndsWithSaurus): {0}",
dinosaurs.FindLast(EndsWithSaurus));
Console.WriteLine("\nFindAll(EndsWithSaurus):");
List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);
foreach(string dinosaur in sublist)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\n{0} elements removed by RemoveAll(EndsWithSaurus).",
dinosaurs.RemoveAll(EndsWithSaurus));
Console.WriteLine("\nList now contains:");
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nExists(EndsWithSaurus): {0}",
dinosaurs.Exists(EndsWithSaurus));
}
// Search predicate returns true if a string ends in "saurus".
private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}
}
#6
list.FindAll(delegate(int n) { return n == 100; }).Count
#7
List<int> list = new List<int>();
list.Add(100);
list.Add(100);
list.Add(100);
list.Add(100);
list.Add(100);
MessageBox.Show(list.FindAll((ex) => { return ex == 100; }).Count.ToString());
嘿嘿,可行
list.Add(100);
list.Add(100);
list.Add(100);
list.Add(100);
list.Add(100);
MessageBox.Show(list.FindAll((ex) => { return ex == 100; }).Count.ToString());
嘿嘿,可行