- package com.cn.reviev;
- import java.util.*;
- /**
- * 获取字符串中字母的个数并输入
- * @author Administrator
- *
- */
- public class TreeSetSortString {
- public static void main(String[] args) {
- String s="azxcAXCCxaea rtsdfbb";
- countNum(s);
- }
- static void countNum(String s){
- char [] ch=s.toCharArray();//字符串转成数组
- Map<Character,Integer> st=new TreeMap<Character,Integer>();
- for(int i=0;i<ch.length;i++){
- if(!((ch[i]<='z'&&ch[i]>='a')||(ch[i]<='Z'&&ch[i]>='A'))) continue;
- int count=0;//计数器
- if(st.containsKey(ch[i])){//st中包含有元素就取出该值
- count=st.get(ch[i]);
- }
- count++;//计数器加一
- st.put(ch[i],count);//存入
- }
- System.out.println(st);
- }
- }
复制代码 |
|