package auto;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class GetSum {
public static void main(String[] args){
String a="abc我你你你你他";
GetSum gs=new GetSum();
Map<Character ,VO> m = new HashMap<Character ,VO>();
for (int i=0;i<a.length();i++){
char temp=a.charAt(i);
if(m.containsKey(temp)){
VO v=(VO)m.get(temp);
v.value+=1; //得到对象,可以修改其中的值
}else{
VO vtemp=gs.new VO(temp, 1); //内部类的新建方法,必须这样新建
m.put(temp,vtemp);
}
}
Collection<VO> c=m.values();
for(VO vo:c){
System.out.print(vo.getKey()+":");
System.out.print(vo.getValue()+";");
}
//结果 他:1;我:1;b:1;c:1;a:1;你:4; Collection Map等集合一般都是范型
}
class VO{
private char key;
private int value;
public VO(char key, int value) {
super();
this.key = key;
this.value = value;
}
public int getValue() {
return value;
}
public char getKey() {
return key;
}
}
}
统计字符串中各个字符的个数
最新推荐文章于 2021-02-10 22:59:40 发布