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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 赵茹艳 中级黑马   /  2012-5-24 21:47  /  2562 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 赵茹艳 于 2012-5-24 21:49 编辑

public class Test06 {

        /*
                   *取出一个字符串中字母出现的次数。如:"abcdekka27qoq"  a(2)b(1)k(2)...
        */
        
        public static void main(String[] args) {
                count("abcdekka27qoq");
        }

        public static void count(String string) {
                String s1 = "";
               
                char[] arr = string.toCharArray();
                int i = 1;
               
                for (char c : arr) {
                        if (Character.isLetter(c)){
                                i = countTime(c, arr.toString());
                                
                                s1 = s1 + c+"("+ i +")";
                                System.out.println(s1);
                        }
                }
               
        }

        private static int countTime(char key, String string) {
                char[] arr = string.toCharArray();
                int times = 0;
                for (int i = 0; i < arr.length; i++ ) {
                        if (arr== key){
                                times++;
                        }
                }
                return times;
        }

}

求教,思路是对的吗?哪些地方错了?真是脑子乱成浆糊了!求指教?

7 个回复

倒序浏览
思路:看着道题打印的结果是首先 a(2)b(1)k(2)...一个字母后面对应着出现的次数具有映射关系,这样我们会想到TreeMap集合,因为我们可以把字母作为键,次数座位值存入TreeMap集合。
要把字符串转换成字符数组,然后取出数组中的每一个元素进行判断,把排序好的字符数StringBuilder中然后在输出数组即可
具体代码实现如下:
import java.util.*;

public class TreeSet1 {


        public static void main(String[] args) {

                String s=charCount("abcdekka27qoq");
                System.out.println(s);
        }
        public static String charCount(String str)
        {
                int count=0;
                char[] chs=str.toCharArray();
                TreeMap<Character,Integer>ts=new TreeMap<Character,Integer>();
                for(int i=0;i<chs.length;i++)
                {
                        if(!(chs[i]>='a'&& chs[i]<='z' || chs[i]>='A' && chs[i]<='Z'))
                                continue;
                        Integer value=ts.get(chs[i]);
                        if(value==null)
                        {
                                ts.put(chs[i], 1);
                        }
                        else
                        {
                                value=value+1;
                                ts.put(chs[i], value);
                        }
                       
                               
                       
                }
                StringBuilder sb=new StringBuilder();
                Set<Character> se=ts.keySet();
                Iterator<Character> it=se.iterator();
                while(it.hasNext())
                {
                        Character ch=it.next();
                        Integer value=ts.get(ch);
                        sb.append(ch+"("+value+")");
                }
                return sb.toString();
        }

}
回复 使用道具 举报
本帖最后由 罗文杰 于 2012-5-25 01:13 编辑

楼主总体的上的思路是可以达到要求的。就是用在把字符串转化为字符数组,在遍历数组的过程中再和字符串做比较是可以达到目的的。不过楼主有几个地方有写考虑不周到。比如:没有去除重复元素,还有楼主把字符串转来转去的好麻烦,关键就是在这里,你在传的过程中已经不是在操作原有的字符串,详情见对你的代码注释。可以把函数直接写成对数组操作的。

复制代码
public class Test5 {

        /*
                   *取出一个字符串中字母出现的次数。如:"abcdekka27qoq"  a(2)b(1)k(2)...
        */

        public static void main(String[] args) {
                count("abcdekka27qoq");
        }

        public static void count(String string) {
                String s1 = "";

                char[] arr = string.toCharArray();
                int i = 1;

                for (char c : arr) {
                        if (Character.isLetter(c)){
                          i = countTime(c, arr.toString()); //重点!!这里 arr.toString()方法返回的值不是你原有的字符串,而是arr.toString方法返回一个字符串,该字符串由类名(对象是该类的一个实例)、at 标记符“<code>@</code>”和此对象哈希码的无符号十六进制表示组成。  这个函数写成对数组arr操作的。
                         //此模块内应添加去除重复元素的动作。

                                                               
                                s1 = s1 + c+"("+ i +")";   //这里导致你每次输出都带着上次的字符串
                                System.out.println(s1);    //这里不用换行
                        }
                }

        }

        private static int countTime(char key, String string) {
                char[] arr = string.toCharArray();
                                
                int times = 0;
                for (int i = 0; i < arr.length; i++ ) {
                                       
                        if (arr== key){      // 这句 应该用arr【i】做比较,我相信楼主一定加了。  ps:我这句话编辑了快一个小时,终于知道为啥不出【i】了    我估计楼主也是因为这个原因贴 不出来【i】         
                                times++;
                        }
                }
                return times;
        }

}


以下是我根据楼主的思路优化的代码:
  1. import java.util.*;
  2. class  Test4
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 count("abcdekka27qoq");
  7.         }

  8.         public static void count(String string)
  9.         {
  10.                 String s1 = "";
  11.                
  12.                 char[] arr = string.toCharArray();
  13.                 int i = 1;
  14.                 ArrayList<Character> al = new ArrayList<Character>(); //创建一个集合用于存储遍历过的元素
  15.                
  16.                 for (char c : arr)
  17.                 {               
  18.                         if (Character.isLetter(c))
  19.                         {
  20.                                 if(!al.contains(c))              //判断集合中是否有正在遍历的字符
  21.                                 {
  22.                                         al.add(c);
  23.                                         i = countTime(c, arr);
  24.                                 
  25.                                         s1 = c+"("+ i +")";
  26.                                         System.out.print(s1);
  27.                                 }
  28.                 }
  29.         }
  30.                
  31.   }

  32.     private static int countTime(char key, char[] arr)     //这里直接操作数组就好了,省得转来转去的。
  33.     {
  34.                
  35.         int times = 0;
  36.         for (int i = 0; i < arr.length; i++ )
  37.                 {
  38.                         if (arr[i] == key)
  39.                         {
  40.                                 times++;
  41.             }
  42.         }
  43.         return times;
  44.     }
  45. }
复制代码
运行结果:
回复 使用道具 举报
本帖最后由 罗文杰 于 2012-5-25 01:11 编辑

{:soso_e109:}上面那个代码块发了好多次 每次发出去都有问题 终于让我找到原因了 原来中括号i不显示的……,没看HTML方面知识的后果,得马上恶补了,加油~~~
回复 使用道具 举报
  1. public class Test06 {

  2. /*
  3. *取出一个字符串中字母出现的次数。如:"abcdekka27qoq" a(2)b(1)k(2)...
  4. */
  5. /*
  6. public static void main(String[] args) {
  7. count("abcdekka27qoq");
  8. }

  9. public static void count(String string) {
  10. String s1 = "";

  11. char[] arr = string.toCharArray();
  12. int i = 0;

  13. for (char c : arr) {
  14. if (Character.isLetter(c)){

  15. char[] arr1 = s1.toCharArray();          //这里是把s1转成数组
  16. int sign=1;                                         //标记
  17. for (int j = 0; j < arr1.length;j++ )    //
  18. {来信                            //
  19. if (c==(arr1[j]))                                 //是否已经计算过
  20. {                                                        //
  21. sign=0;                                              //是已经计算过就把sign置0
  22. break;                                               //
  23. }
  24. else                                                   //不是就把sign置1;
  25. sign=1;
  26. }                                                     //如果sign==1就表示这个字符没计算过,如果sign==0就表示这个字符已经计算过了
  27. if(sign==1)
  28. {
  29. i = countTime(c, arr);
  30. s1 = s1 + c+"("+ i +")";
  31. // System.out.println(s1);
  32. }
  33. }

  34. }
  35. System.out.println(s1);
  36. }

  37. private static int countTime(char key, char[] arr) {
  38. //char[] arr = string.toCharArray();
  39. int times = 0;
  40. for (int i = 0; i < arr.length; i++ ) {
  41. if (key==(arr[i])){
  42. times++;
  43. }
  44. }
  45. return times;
  46. }
  47. }
复制代码
这样的题用集合来做是非常方便的
思路是先要有一个查找的字符,然后又有一个这个字符对应的出现的次数,于是我们就可以想到使用TreeMap集合,key为字符,出现次数为value;
实现代码如下:
class Test06
{
        public static void main(String[] args)
     {
                String str = new String("abaaacdefgabcd");
               TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
               char[] arr = str.toCharArray();
                int times=0;
              for(char c: arr)
                  {
                         for(int i = 0;i<arr.length;i++)
                           {
                              if(c==arr)
                              times++;
                           }
                         tm.put(Character.valueOf(c),Integer.valueOf(times));
                       times=0;
               }
            StringBuilder strb = new StringBuilder();
              Set<Character> ts = tm.keySet();
             Iterator<Character>  it = ts.iterator();
     while(it.hasNext())
           {
                Character key = (Character)it.next();
                Integer value = (Integer)tm.get(key);
               strb.append(key+"("+value+")");
          }
                System.out.println(strb.toString());
    }
}







回复 使用道具 举报
各位都很强大啊!感谢万分啊!那个arr的确是自己写少了!罗同学深知我心啊!多谢各位,学到了很多,大家一起加油吧!
回复 使用道具 举报
赞{:soso__6235880048239246314_3:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马