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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© masai158 高级黑马   /  2014-7-28 15:57  /  1299 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

上次看到看到测试题。然后 想到了 键值队 Map.....
取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)...
今天抱着试一下的心态: 将Map集合 转成 set 集合。然后在通过 TreeSet集合排序。然后在 遍历 TreeSet集合。。(这是我目前自己想到的方法
你们有更简单的方法吗???求分享!!!!!!

我的部分代码如下:

7 个回复

倒序浏览
感觉,好迂回。
回复 使用道具 举报

{:2_39:}{:2_39:}{:2_39:}!原谅我是新手!求大神指明一下思想
回复 使用道具 举报
masai158 发表于 2014-7-28 16:07
!原谅我是新手!求大神指明一下思想

Set的底层本来就是利用Map来实现的,你又用Set来模拟Map,感觉没啥意义。
我写了个用数组实现的,不过最后没排序就是了。
回复 使用道具 举报
有,直接TreeMap<Character, Integer>就把排序搞定了
回复 使用道具 举报
fantacyleo 发表于 2014-7-28 16:12
有,直接TreeMap就把排序搞定了

{:2_34:}{:2_34:}{:2_34:}{:2_34:}{:2_34:}{:2_34:}{:2_34:}{:2_34:}{:2_34:}{:2_34:}{:2_34:}!!!!!我,,,,,,想扇我自己一耳光
回复 使用道具 举报
刚刚把排序弄进去了。数组版。
  1. package test;

  2. public class Test12 {
  3.         public static void main(String[] args) {
  4.                 countCharTimes("abcdekka27qoq");
  5.         }

  6.         public static void countCharTimes(String string) {
  7.                 char[] stringChars = string.toCharArray();
  8.                 char[] chars = new char[stringChars.length];// 用于存储不同的字符
  9.                 int[] times = new int[stringChars.length];// 用于存储各个字符的出现的次数
  10.                 int size = 0;// 不同字符的实际种数
  11.                 // 外循环遍历原字符串的各个字符
  12.                 out: for (int i = 0; i < stringChars.length; i++) {
  13.                         // 内循环遍历不同字符的数组,若字符已经出现则次数加1,且跳出内循环,继续外循环
  14.                         for (int j = 0; j < size; j++) {
  15.                                 if (chars[j] == stringChars[i]) {
  16.                                         times[j]++;
  17.                                         continue out;
  18.                                 }
  19.                         }
  20.                         chars[size] = stringChars[i];// 加入不同的字符
  21.                         times[size] = 1;// 新加入的字符次数设置为1
  22.                         size++;// 不同字符数组的实际种数加1
  23.                 }
  24.                 //排序
  25.                 for (int i = 0; i < size - 1; i++) {
  26.                         for (int j = i + 1; j < size; j++) {
  27.                                 if (chars[i] > chars[j]) {
  28.                                         // 排不同字符那个数组
  29.                                         char tempChar = chars[i];
  30.                                         chars[i] = chars[j];
  31.                                         chars[j] = tempChar;
  32.                                         // 排对应的计数数组
  33.                                         int tempInt = times[i];
  34.                                         times[i] = times[j];
  35.                                         times[j] = tempInt;
  36.                                 }
  37.                         }
  38.                 }
  39.                
  40.                 System.out.println("字符串\"" + string + "\"中,有" + size + "种字符,各个字符的个数为:");
  41.                 for (int i = 0; i < size; i++) {
  42.                         System.out.print(chars[i] + "(" + times[i]+")");
  43.                 }

  44.         }
  45. }
复制代码



回复 使用道具 举报
黎志勇 发表于 2014-7-28 16:20
刚刚把排序弄进去了。数组版。

3Q 学习了。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马