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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杨道红 中级黑马   /  2013-12-31 14:36  /  1257 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杨道红 于 2013-12-31 16:49 编辑

复制代码
  1. import java.util.TreeMap;
  2. /*
  3. * 一个字符串“asdcasdcascasdasdfa”
  4. * 存储字符和字符出现的次数,如a(4)。b(2)
  5. * 对于字符串,定义索引index,确定每次的字符是哪个,在集合中是否存在
  6. * 如果不存在,则添加
  7. * 如果存在,则取出原来字符对应的值+1再重新赋值到集合里面
  8. */
  9. public class TreeMapTest {
  10.         public static void main(String[] args) {
  11.                 String str = "asdaasdfsdacac";
  12.                 TreeMap<Character,Integer> tm = createCollection(str);
  13.                 System.out.println(tm);
  14.         }
  15.         public static TreeMap<Character,Integer> createCollection(String str){
  16.                 TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
  17.                 for(int index=0;index<str.length();index++){
  18.                         char key = str.charAt(index);
  19.                         int value = tm.get(key);
  20.                         if(value==0)
  21.                                 value = 1;
  22.                         else
  23.                                 value++;
  24.                         tm.put(key, value);
  25.                 }
  26.                 return tm;
  27.         }
  28. }
复制代码

抛出java.lang.NullPointerException异常,在哪修改?

评分

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

查看全部评分

6 个回复

倒序浏览
int value = tm.get(key);我敲了一下 但我不会上传  截图    先 给你说下具体思路:把字符串变成字符数组。 定义一个TreeMap集合。Character做键,Integer做值。 遍历字符数组,获取到每一个字符。拿到该字符,到TreeMap集合中找对应的值

评分

参与人数 1黑马币 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
  1. public class TreeMapTest {
  2.         public static void main(String[] args) {
  3.                 String s = "cbxzbvavdvgd";
  4.                 // 把字符串变成字符数组。
  5.                 char[] chs = s.toCharArray();
  6.                 // 定义一个TreeMap集合。Character做键,Integer做值。
  7.                 TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
  8.                 // 遍历字符数组,获取到每一个字符。
  9.                 for (char ch : chs) {
  10.                         // 拿到该字符,到TreeMap集合中找对应的值
  11.                         Integer i = tm.get(ch);
  12. // 根据返回值是否为null 是:存储,把次数记录为1 否:把数据++,然后重写存储
  13.                         if (i == null) {
  14.                                 tm.put(ch, 1);
  15.                         } else {
  16.                                 i++;
  17.                                 tm.put(ch, i);
  18.                         }
  19.                 }
  20.                 // 把TreeMap的数据拼接成一个字符串。
  21.                 StringBuilder sb = new StringBuilder();
  22.                 Set<Character> set = tm.keySet();
  23.                 for (Character ch : set) {
  24.                         Integer i = tm.get(ch);
  25.                         sb.append(ch).append("(").append(i).append(")");
  26.                 }
  27.                 String result = sb.toString();
  28.                 System.out.println(result);
  29.         }
  30. }
复制代码



看下效果怎么样

评分

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

查看全部评分

回复 使用道具 举报
public static TreeMap<Character,Integer> createCollection(String str){
                         TreeMap<Character,Integer> tm=new TreeMap<Character, Integer>();
                char[] chs=str.toCharArray();
         for(int index=0;index<chs.length;index++){
                 
                char key=chs[index];
                Integer value=tm.get(key);
                if(value==null)     //判断是否为空,
                {
                        tm.put(key, 1);
                }
                if(value!=null)    //若不为空加1;赋值
                {
                        tm.put(key,value+1);
                        value=0;      //清空value值
                }
         }
         System.out.println(tm);
         
         
         return tm;

}

评分

参与人数 1黑马币 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 776699 于 2013-12-31 16:25 编辑

空指针异常,你的Integer value = tm.get(key);  应该是Integer 类型,而 value为一个对象,而不能用value==0来判断,所以,抛出空指针,你修改一下看看

评分

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

查看全部评分

回复 使用道具 举报
776699 发表于 2013-12-31 16:20
空指针异常,你的Integer value = tm.get(key);  应该是Integer 类型,而 value为一个对象,而不能用value= ...

谢谢,我试试
回复 使用道具 举报
776699 发表于 2013-12-31 16:20
空指针异常,你的Integer value = tm.get(key);  应该是Integer 类型,而 value为一个对象,而不能用value= ...

就是那个错误,我把int改成Integer,==0换成==null就行了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马