A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 耿渊博 中级黑马   /  2014-4-3 22:22  /  1078 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

打印结果对了,格式不太对,怎么改啊?
  1. package com.Map;
  2. import java.util.*;
  3. /*
  4. 练习:
  5. "sdfgzxcvasdfxcvdf"获取该字符串中的字母出现的次数。

  6. 希望打印结果:a(1)c(2).....
  7. */
  8. public class MapTest3 {

  9.         public static void main(String[] args) {
  10.                 // TODO Auto-generated method stub
  11.                 charCount("sdfgzxcvasdfxcvdf");
  12.         }
  13.        
  14.         public static String charCount(String str){
  15.                 char[] chs = str.toCharArray();
  16.                
  17.                 TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
  18.                
  19.                 for(int x=0;x<chs.length;x++){
  20.                         Integer value = tm.get(chs[x]);
  21.                        
  22.                         if(value ==null){
  23.                                 tm.put(chs[x], 1);
  24.                         }
  25.                         else{
  26.                                 value = value+1;
  27.                                 tm.put(chs[x], value);
  28.                         }
  29.                 }
  30.                
  31.                 System.out.println(tm);
  32.                
  33.                 return null;
  34.         }

  35. }

  36. //打印结果{a=1, c=2, d=3, f=3, g=1, s=2, v=2, x=2, z=1}
复制代码


3 个回复

倒序浏览
public static void charCount(String str) {
                char[] chs = str.toCharArray();

                TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();

                for (int x = 0; x < chs.length; x++) {
                        Integer value = tm.get(chs[x]);

                        if (value == null) {
                                tm.put(chs[x], 1);
                        } else {
                                value = value + 1;
                                tm.put(chs[x], value);
                        }
                }

                Set<Character> s = tm.keySet();
                Iterator<Character> it = s.iterator();
                while (it.hasNext()) {
                        Character key = it.next();
                        Integer valu = tm.get(key);
                        System.out.print(key + "(" + valu + ")");
                }

        }
这样就可以了,TreeMap集合要遍历一下,得到它的key和value,然后再用要求的形式打印,其中TreeMap有两种遍历方法,一种是.keySet(),得到键的集合,然后遍历,另一种是xxx.entrySet()得到Map.entry(K,V)类型的集合,然后遍历得到key和value,我这里用的第一种

评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1

查看全部评分

回复 使用道具 举报
  1. //package com.Map;
  2. import java.util.*;
  3. /*
  4. 练习:
  5. "sdfgzxcvasdfxcvdf"获取该字符串中的字母出现的次数。

  6. 希望打印结果:a(1)c(2).....
  7. */
  8. public class MapTest3
  9.         {

  10.         public static void main(String[] args) {
  11.                 // TODO Auto-generated method stub
  12.                String s = charCount("sdfgzxcvasdfxcvdf");
  13.                                            System.out.println(s);

  14.     }
  15.         
  16.         public static String charCount(String str){
  17.                 char[] chs = str.toCharArray();
  18.                
  19.                 TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
  20.                
  21.                 for(int x=0;x<chs.length;x++){
  22.                         Integer value = tm.get(chs[x]);
  23.                         
  24.                         if(value ==null){
  25.                                 tm.put(chs[x], 1);
  26.                         }
  27.                         else{
  28.                                 value = value+1;
  29.                                 tm.put(chs[x], value);
  30.                         }
  31.                 }
  32.         StringBuilder sb = new StringBuilder();

  33.                 Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();
  34.                 Iterator<Map.Entry<Character,Integer>>  it = entrySet.iterator();

  35.                 while(it.hasNext())
  36.                 {
  37.                         Map.Entry<Character,Integer> me = it.next();
  38.                         Character ch = me.getKey();
  39.                         Integer value = me.getValue();
  40.                         sb.append(ch+"("+value+")");
  41.                 }

  42.                         return sb.toString();
  43.                 //System.out.println(tm);
  44.                
  45.                 //return null;
  46.         }

  47. }

  48. //打印结果{a=1, c=2, d=3, f=3, g=1, s=2, v=2, x=2, z=1}
复制代码
改好
回复 使用道具 举报
  1. public static void charCount(String str) {
  2.                  char[] chs = str.toCharArray();

  3.                 TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();

  4.                 for (int x = 0; x < chs.length; x++) {
  5.                          Integer value = tm.get(chs[x]);

  6.                         if (value == null) {
  7.                                  tm.put(chs[x], 1);
  8.                          } else {
  9.                                  value = value + 1;
  10.                                  tm.put(chs[x], value);
  11.                          }
  12.                  }

  13.                 Set<Character> s = tm.keySet();
  14.                  Iterator<Character> it = s.iterator();
  15.                  while (it.hasNext()) {
  16.                          Character key = it.next();
  17.                          Integer valu = tm.get(key);
  18.                          System.out.print(key + "(" + valu + ")");
  19.                  }

  20.         }
  21. 这样就可以了,TreeMap集合要遍历一下,得到它的key和value,然后再用要求的形式打印,其中TreeMap有两种遍历方法,一种是.keySet(),得到键的集合,然后遍历,另一种是xxx.entrySet()得到Map.entry(K,V)类型的集合,然后遍历得到key和value,我这里用的第一种
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马