本帖最后由 黄秋 于 2012-3-6 08:05 编辑
用HashMap更靠谱:- public static void main(String[] args) {
- HashMap<Character,Integer> hm = new HashMap<Character,Integer>();
- String s="ABCGoag-afg+++";
- Character ch;
- for(int i=0;i<s.length();i++){
- ch=s.charAt(i);
- if(hm.containsKey(ch)) //若是重复,则+1
- hm.put(ch,hm.get(ch)+1);
- else
- hm.put(ch,1);
- }
- System.out.println("-: "+hm.get('-')+" a:"+hm.get('a')+" +:"+hm.get('+'));
- }
复制代码 |