求出该字符串中每个字符出现的次数

public class Demo05 {
     public static void main(String[] args) {
          String maxString = “hellword”;
          HashMap<String,Integer> map = new HashMap<String,Integer> ();
          for(int i = 0 ;i < maxString.length()-1;i++) {
               int count = 0;
               char charAt = maxString.charAt(i);
               String minString = “” + charAt;
               int index = maxString.indexOf(minString);
               while (index != -1) {
                    count++;
                    int startIndex = index + 1;
                    index = maxString.indexOf(charAt, startIndex);
               }
               if(!map.containsKey(minString)) {
                    map.put(minString, count);
               }
          }

          for (Map.Entry<String, Integer> entry : map.entrySet()) {
               System.out.println(entry.getKey() + “出现的次数为” + entry.getValue());
          }
     }
}

点赞