为输入生成类似 - m3g2k3c2的输出mmmggkkkcc

时间:2015-08-24 19:05:19

标签: java

所以基本上我想表明一个角色在一个字符串中重复多少次,所以如果我有hheerrrtt,我应该把它输出为h2e2r3t2,这是我试过的:

import java.util.ArrayList;
import java.util.List;

public class HelloWorld{
static void process(String s){

int[] count = new int[10];

char[] array = s.toCharArray();

System.out.println(s.length());

     int j=0;
     int m=0;
     int i=0;
     char c;
     ArrayList<Character> list = new ArrayList<Character>();
     for(j=0;j<s.length();j++){

         m=0;
         for(i=0;i<s.length();i++){

                 if(array[j]==array[i]){
                     m++;
                 }

            }
            System.out.println("char "+ array[j] + " is " + m);
            list.add(array[j]);
            //c=char(m);
            list.add(Character(m)); // gives an error, can't be donethis way

     }


 }
     public static void main(String []args){
        System.out.println("Hello World");
          process("hhhelloc");
     }
}

我能够循环遍历每个字符并查看它重复了多少次,但我无法生成像h3e1l2o1c1这样的字符串。我选择数组的字符列表,并尝试添加每个字符,然后重复它的次数,但我无法向arraylist添加一个整数。任何帮助表示赞赏

0 个答案:

没有答案