例如 :{张三, 李四, 张三, 李四, 张三}
张三 3次
李四 2次
感谢大家的帮助,最终我是这样解决的, 贴出来供大家指正。主要代码如下
DataTable dt = e.Result as DataTable;
DataRow [] dr = dt.Select("","xftime asc");
List<myelem> al = new List<myelem>();
List<myelem_result> al_result = new List<myelem_result>();
foreach (DataRow r in dr)
{
myelem me = new myelem();
me.pub_name = r[5].ToString().Trim();
me.pub_riqi = Convert.ToDateTime(r[2]).ToString("yyyy-MM-dd");
me.pub_int = 1;
dataitem d = new dataitem();
d.pub_time = Convert.ToDateTime(r[2]).ToString("HH:mm");
d.pub_station = r[7].ToString();
me.pub_item = d;
al.Add(me);
}
int flag = 1;
for (int i = 0; i < al.Count; i++)
{
List<dataitem> ld = new List<dataitem>();
myelem_result me_result = new myelem_result();
me_result.pub_riqi = al[i].pub_riqi;
me_result.pub_name = al[i].pub_name;
dataitem d2 = new dataitem();
d2.pub_time = al[i].pub_item.pub_time;
d2.pub_station = al[i].pub_item.pub_station;
ld.Add(d2);
d2 = null;
me_result.pub_list_item = ld;
for (int j = i + 1; j < al.Count; j++)
{
if (i + 1 < al.Count)
{
if (al[i].pub_riqi == al[j].pub_riqi)
{
flag++;
dataitem d1 = new dataitem();
d1.pub_time = al[j].pub_item.pub_time;
d1.pub_station = al[j].pub_item.pub_station;
ld.Add(d1);
d1 = null;
al.RemoveAt(j);
j--;
}
}
}
me_result.pub_int = flag;
al_result.Add(me_result);
me_result = null;
flag = 1;
}
桶排序
先找出,list中所有不一样的元素,再统计每个元素出现的次数。
group by
Google arrarylist linq count,你会回来谢我的。
var list=new{张三, 李四, 张三, 李四, 张三}
list.groupby(m=>m).select(m=>{m,m.count})
java中的Collections.frequency(Collection Object o):返回指定集合中指定对象出现的次数
import java.util.*;
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
int[] array = {1, 12, 43, 12, 13, 12, 43};
for (int i : array) {
list.add(new Integer(i));
}
System.out.println(Collections.frequency(list, new Integer(1)));
System.out.println(Collections.frequency(list, new Integer(12)));
System.out.println(Collections.frequency(list, new Integer(43)));
System.out.println(Collections.frequency(list, new Integer(13)));
}
}