黑马程序员技术交流社区

标题: 求解一个集合中练习的问题 [打印本页]

作者: xwh1230    时间: 2014-7-23 21:46
标题: 求解一个集合中练习的问题
因为技术半吊子所以好多东西写不出来啊,写出来了也不知道对不对。。。真心有点郁闷
/*
* 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);
  }
  
}
}
这个问题怎么会出错呢?没搞懂。。

作者: 张盼    时间: 2014-7-23 22:53
这个题建议你从新做吧,ch[i]可能会重复,不能作为键
Map集合的遍历方式有两种,一种是通过键获取值,一种是通过键值对的对象获取值,也就是tm.enty(),没有Iterator迭代器这种遍历方式。
建议你用ArrayList集合来做会好做一些。
作者: 花花公子    时间: 2014-7-23 23:56
张盼 发表于 2014-7-23 22:53
这个题建议你从新做吧,ch可能会重复,不能作为键
Map集合的遍历方式有两种,一种是通过键获取值,一种是通 ...

我觉得用HashMap好一点,保证唯一性。
作者: 花花公子    时间: 2014-7-23 23:57
  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. }
复制代码

作者: wisely    时间: 2014-7-24 00:40
mark一下,这个好像是毕老师视频里的面试题,用的应该是TreeMap,自动排序……
作者: xwh1230    时间: 2014-7-24 20:14
花花公子 发表于 2014-7-23 23:56
我觉得用HashMap好一点,保证唯一性。

TreeMap不也能保证唯一性么?
作者: 花花公子    时间: 2014-7-25 11:21
xwh1230 发表于 2014-7-24 20:14
TreeMap不也能保证唯一性么?

嘿嘿不都一样么,都是解决问题
作者: fantacyleo    时间: 2014-7-25 11:43
你的set变量在哪里定义的
作者: ahuhxl    时间: 2014-7-25 11:46
楼主错在这:Iterator<Character,Integer> it = set.iterator();
首先,set是变量?但没有定义过。是Set集合?那S应该大写,再说也没见过直接用集合名.iterator的。
其次,Iterator的泛型能接收两个?
所以这个地方应该这样改一下:
Set<Character> keyset = tm.keySet();//获取键的Set集合
Iterator<Character> it = keyset.iterator();//取得Set集合的迭代器
这样你的代码就应该没问题了
作者: shen7518    时间: 2014-7-25 11:48
看看,学习下




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2