请问,怎么样做数组元素统计? 如string[] str={"1","1","2","5","2"};统计的结果为 1,2个;2,2个;5,1个 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private System.Collections.Hashtable dt = new System.Collections.Hashtable(); private System.Collections.ArrayList al = new System.Collections.ArrayList(); public string TotalCount(string[] strs) { foreach(string str in strs) { bool isSearched = false; for(int i=0;i<al.Count ;i++) { if(al[i].ToString()==str) { int temp = (int) dt[str]; dt[str] = temp+1; isSearched = true; break; } } if(!isSearched) { dt.Add(str,1); al.Add(str); } } string returnStr = string.Empty; al.Sort(); for(int i=0;i<al.Count ;i++) { returnStr+=al[i].ToString()+","+dt[al[i]]+"个;"; } return returnStr; } 这是个最傻冒的方法,有没有人有更好的方法,比如不仅仅局限于string集合。 string[] str=new string[]{"1","1","2","5","2"};ArrayList a=new ArrayList();ArrayList b=new ArrayList(); for(int i=0;i<str.Length;i++){ string c=str[i]; if(!b.Contains(c)) { int m=0; for(int j=0;j<str.Length;j++) { if(c==str[j]) { m++; } } a.Add(m); b.Add(c); }}for(int m=0;m<a.Count;m++){ Response.Write(b[m]+","+a[m]+"个;");} 来一个只用一个集合类的,算法复杂度为 str.length-1,接龙!!!!!!string returnStr = string.Empty; string[] str=new string[]{"1","1","2","5","2"};System.Collections.ArrayList al = new System.Collections.ArrayList();al.AddRange(strs);al.Sort();string tempStr = al[0].ToString();int index = 1;for(int i=1;i<al.Count;i++){ if(al[i].ToString()==tempStr) { ++index; continue;}else{ returnStr += tempStr+","+index.ToString()+"个;"; tempStr = al[i].ToString(); index = 1;}}returnStr += tempStr+","+index.ToString()+"个;"; 请教!mysql时间字段用了int。 gridview显示问题 Microsoft.Win32的问题 frmWaiting,这个到底在哪个头文件下??? 求csdn内容框的的插件(UBB) datagridview更改数据源的问题 一个非常手的问题 myString.Substring(0, 3);我要中文算2个字,英文算一个字,怎么做? 谁能跟我讲讲odbcDataAdapte和dataSet。顶贴有分 请教一个关于水晶报表取数据的问题 请教关于winform连接数据库语句 DBGIRD的问题新手的菜鸟问题,请高手们进来回答一下,先谢了!!!!
private System.Collections.ArrayList al = new System.Collections.ArrayList();
public string TotalCount(string[] strs)
{
foreach(string str in strs)
{
bool isSearched = false;
for(int i=0;i<al.Count ;i++)
{
if(al[i].ToString()==str)
{
int temp = (int) dt[str];
dt[str] = temp+1;
isSearched = true;
break;
}
}
if(!isSearched)
{
dt.Add(str,1);
al.Add(str);
}
}
string returnStr = string.Empty;
al.Sort();
for(int i=0;i<al.Count ;i++)
{
returnStr+=al[i].ToString()+","+dt[al[i]]+"个;";
}
return returnStr; }
ArrayList a=new ArrayList();
ArrayList b=new ArrayList();
for(int i=0;i<str.Length;i++)
{
string c=str[i];
if(!b.Contains(c))
{
int m=0;
for(int j=0;j<str.Length;j++)
{
if(c==str[j])
{
m++;
}
}
a.Add(m);
b.Add(c);
}
}
for(int m=0;m<a.Count;m++)
{
Response.Write(b[m]+","+a[m]+"个;");
}
string returnStr = string.Empty;
string[] str=new string[]{"1","1","2","5","2"};
System.Collections.ArrayList al = new System.Collections.ArrayList();
al.AddRange(strs);
al.Sort();
string tempStr = al[0].ToString();
int index = 1;
for(int i=1;i<al.Count;i++)
{
if(al[i].ToString()==tempStr)
{
++index;
continue;
}
else
{
returnStr += tempStr+","+index.ToString()+"个;";
tempStr = al[i].ToString();
index = 1;
}
}
returnStr += tempStr+","+index.ToString()+"个;";