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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© upx 中级黑马   /  2015-11-10 18:45  /  3436 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

24黑马币
取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,
* 输出格式为:a(2)b(1)k(2)...

7 个回复

倒序浏览

  1. public class Text01 {

  2. /*
  3. * 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,
  4. * 输出格式为:a(2)b(1)k(2)...
  5. */
  6.         public static void main(String[] args) {
  7.                 // TODO Auto-generated method stub
  8.                 String str = new String("abcdekka27qoq");    //自定义的一个字符串
  9.        
  10.         char[] ch1 = str.toCharArray();                  //将字符串转变为字符串数组,为了方便后续进行比对
  11.         sum(ch1);                                        //将ch1放进方法sum当中;求出现的个数
  12.        
  13.         }
  14.         public static void sum(char[] arr){
  15.                 String array =new String();                  //定义一个null 的字符串,为了存ch1中不重复的元素
  16.                 for (int i = 0; i < arr.length; i++) {
  17.                         if ((array.indexOf(arr[i]) < 0) ) {      //比对字符串array中ch1【i】的索引值,若返回-1,则字符串中没有对应的字符,则将该字符添加到字符串的最后面
  18.                                 array = array + arr[i];
  19.                         }
  20.                 }
  21.                 char[] ch2 = array.toCharArray();                        //将array字符串变为字符串数组,用来比ch2中每个元素在ch1中出现的次数。
  22.                
  23.                
  24.                 for (int i = 0; i < ch2.length; i++) {      //遍历之前array转为ch2的字符串数组
  25.                         int x = 0;
  26.                         for (int j = 0; j < arr.length; j++) {    //遍历之前str转为ch1的字符串数组
  27.                                
  28.                                 if (ch2[i] == arr[j]) {                                        //比对ch2中每一个元素在ch1中出现的次数,若相等,则数字加1.
  29.                                         x++;
  30.                                 }                               
  31.                         }
  32.                         System.out.print(ch2[i]+"("+x+")");      //打印ch2中元素在ch1中出现的次数,则就是ch1中元素在ch1中出现的次数。
  33.                 }
  34.         }
  35. }
复制代码
回复 使用道具 举报
可以看一下,目前我还只学到string,所以后面的大神的方法还不清楚,但是我就是用的string to char【】,然后通过char[] arr转回到string字符串中,每加一个字符的索引,若索引为负数,这这个字符串中还没有改字符,这样再用去重的字符串转字符数组,用这个字符数组的元素比对之前最原始的字符串所转的字符数组,有点绕,可以看看代码。上午才做的。应该可以帮到你。



public class Text01 {

/*
* 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,
* 输出格式为:a(2)b(1)k(2)...
*/
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                String str = new String("abcdekka27qoq");    //自定义的一个字符串
       
        char[] ch1 = str.toCharArray();                  //将字符串转变为字符串数组,为了方便后续进行比对
        sum(ch1);                                        //将ch1放进方法sum当中;求出现的个数
       
        }
        public static void sum(char[] arr){
                String array =new String();                  //定义一个null 的字符串,为了存ch1中不重复的元素
                for (int i = 0; i < arr.length; i++) {
                        if ((array.indexOf(arr[i]) < 0) ) {      //比对字符串array中ch1【i】的索引值,若返回-1,则字符串中没有对应的字符,则将该字符添加到字符串的最后面
                                array = array + arr[i];
                        }
                }
                char[] ch2 = array.toCharArray();                        //将array字符串变为字符串数组,用来比ch2中每个元素在ch1中出现的次数。
               
               
                for (int i = 0; i < ch2.length; i++) {      //遍历之前array转为ch2的字符串数组
                        int x = 0;
                        for (int j = 0; j < arr.length; j++) {    //遍历之前str转为ch1的字符串数组
                               
                                if (ch2[i] == arr[j]) {                                        //比对ch2中每一个元素在ch1中出现的次数,若相等,则数字加1.
                                        x++;
                                }                               
                        }
                        System.out.print(ch2[i]+"("+x+")");      //打印ch2中元素在ch1中出现的次数,则就是ch1中元素在ch1中出现的次数。
                }
        }
}
回复 使用道具 举报
学习一下
回复 使用道具 举报
厉害啊!!
回复 使用道具 举报
public class Demo_2 {

        /**
         * 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,
         * 输出格式为:a(2)b(1)k(2)...
         */
        public static void main(String[] args) {
                String s="abcdekka27qoq"; //
                int numa=0;
                int numb=0;
                int numk=0;
                for (int i = 0; i < s.length(); i++) {
                        char c=s.charAt(i);
                        if(c=='a'){
                                numa++;
                        }else if(c=='b'){
                                numb++;
                        }else if (c=='k'){
                                numk++;
                        }else{
                               
                        }
                }
                System.out.println("a("+numa+")b("+numb+")k("+numk+").......");
               
        }

}
回复 使用道具 举报
过来学习一下!
回复 使用道具 举报
/*取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,
                * 输出格式为:a(2)b(1)k(2)...*/
               
                String str = "abcdekka27qoq";
                char[] ch1 = str.toCharArray();
                for (int i = 0; i < ch1.length - 1; i++) {
                        for (int j = i + 1; j < ch1.length; j++) {
                                if(ch1[i] > ch1[j]){
                                        int temp = ch1[i];
                                        ch1[i] = ch1[j];
                                        ch1[j] = (char) temp;
                                }
                        }
                }
                System.out.println(Arrays.toString(ch1));
               
                for (int i = 0; i < ch1.length; ) {
                        int count = 0;
                        for (int j = i + 1; j < ch1.length; j++) {
                                if(ch1[i] == ch1[j]) {
                                        count++;
                                }
                        }
                        System.out.print(ch1[i]+"("+(count + 1)+")");
                        i = i + count;
                        i++;
                }

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马