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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xwh1230 中级黑马   /  2014-7-23 21:46  /  1016 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

因为技术半吊子所以好多东西写不出来啊,写出来了也不知道对不对。。。真心有点郁闷
/*
* rehtbwegrtrewfwxf  统计这个字符串中每个字符出现多少次
*/
package cn.itcast.map.test;
import java.util.*;
public class Test {
public static void main(String[] args) {
  String s = "rehtbwegrtrewfwxf";
  TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
  s = s.trim();
  //转换成为字符数组
  char[] ch = s.toCharArray();
//  定义循环,比较并存入treemap
  for(int i = 0;i<ch.length;i++){
   Integer in = tm.get(ch[i]);
   if (in==null) {
    tm.put(ch[i], 1);
   }
   else
    tm.put(ch[i], in+1);
  }
  System.out.println(tm);
  Iterator<Character,Integer> it = set.iterator();
  while(it.hasNext()){
   Character ch1 = it.next();
   Integer in = tm.get(ch1);
   System.out.println(ch1+"="+in);
  }
  
}
}
这个问题怎么会出错呢?没搞懂。。

10 个回复

倒序浏览
这个题建议你从新做吧,ch[i]可能会重复,不能作为键
Map集合的遍历方式有两种,一种是通过键获取值,一种是通过键值对的对象获取值,也就是tm.enty(),没有Iterator迭代器这种遍历方式。
建议你用ArrayList集合来做会好做一些。

点评

这道题key有重复完全没问题,因为需求就是不断更新value。ArrayList没法知道出现次数和字符的对应关系  发表于 2014-7-25 11:39
回复 使用道具 举报
张盼 发表于 2014-7-23 22:53
这个题建议你从新做吧,ch可能会重复,不能作为键
Map集合的遍历方式有两种,一种是通过键获取值,一种是通 ...

我觉得用HashMap好一点,保证唯一性。
回复 使用道具 举报
  1. import java.util.*;
  2. class Count
  3. {
  4.         public Map countStr(String str)
  5.         {
  6.                 Map<String,Integer> map = new HashMap<String,Integer>();
  7.                 String temp = "";
  8.                 for (int i=0;i<str.length();i++)
  9.                 {
  10.                         temp = str.substring(i,i+1);
  11.                         if(map.size()==0)
  12.                         {
  13.                                 map.put(temp,1);//map集合中没数据,添加第一个进去。
  14.                         }
  15.                         else
  16.                         {
  17.                                 if(map.get(temp)==null)
  18.                                 {
  19.                                         map.put(temp,1);//map集合没有,将其加入
  20.                                 }
  21.                                 else
  22.                                 {
  23.                                         int n = map.get(temp);
  24.                                         int m = n+1;
  25.                                         map.put(temp,m);
  26.                                 }
  27.                         }
  28.                 }
  29.                 return map;
  30.         }

  31.         public static void main(String[] args)
  32.         {
  33.                 Count count = new Count();
  34.                 Map map = count.countStr("rehtbwegrtrewfwxf");

  35.                 //迭代输出
  36.                 Set set        = map.keySet();
  37.                 Iterator it = set.iterator();
  38.                 while(it.hasNext())
  39.                 {
  40.                         String key = (String)it.next();
  41.                         int number = (Integer)map.get(key);
  42.                         System.out.println(key+"出现:"+number+"次。");
  43.                 }
  44.         }
  45. }
复制代码
回复 使用道具 举报
mark一下,这个好像是毕老师视频里的面试题,用的应该是TreeMap,自动排序……
回复 使用道具 举报
花花公子 发表于 2014-7-23 23:56
我觉得用HashMap好一点,保证唯一性。

TreeMap不也能保证唯一性么?
回复 使用道具 举报
xwh1230 发表于 2014-7-24 20:14
TreeMap不也能保证唯一性么?

嘿嘿不都一样么,都是解决问题
回复 使用道具 举报
你的set变量在哪里定义的
回复 使用道具 举报
ahuhxl 中级黑马 2014-7-25 11:46:29
9#
楼主错在这:Iterator<Character,Integer> it = set.iterator();
首先,set是变量?但没有定义过。是Set集合?那S应该大写,再说也没见过直接用集合名.iterator的。
其次,Iterator的泛型能接收两个?
所以这个地方应该这样改一下:
Set<Character> keyset = tm.keySet();//获取键的Set集合
Iterator<Character> it = keyset.iterator();//取得Set集合的迭代器
这样你的代码就应该没问题了
回复 使用道具 举报
看看,学习下
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马