比如字符串 "b23chabaccscc"   怎样得出字符c是出现次数最多的

解决方案 »

  1.   

    设一个数组
    a[n]
    a[0]代表 'a'
    b[1]代表 'b'
    。a[25]代表'z'a[26]代表 ‘0’
    a[27]代表 ‘1’
    遍历字符串,将相应的+1。
    最大值为出现最多的。
      

  2.   

    public class test{ 
        public static void main(String args[]) {
         String str="b23chabaccscc";
         boolean[] isCount=new boolean[str.length()];
         char c=(char)0;
         int max=0;
         for(int i=0;i<str.length();i++){
         if(!isCount[i]){
         char temp=str.charAt(i);
         int tempCount=1;
         int tempIndex=i+1;
         isCount[i]=true;
         while(tempIndex<str.length()&&(tempIndex=str.indexOf(temp,tempIndex))>0){
         tempCount++;
         isCount[tempIndex]=true;
         tempIndex++;
         }
         if(tempCount>max){
         c=temp;
         max=tempCount;
         }
            }
         }
         System.out.println("出现次数最多的字符是:"+c);
         System.out.println("出现的次数是:"+max);
        
        }
    }
      

  3.   

      假定这个字符串只包含a-z,0-9之间的字符,那么,我可以用一个Map的容器来把字符和它出现的次数对应起来,通过Stirng类的charAt()方法,以第一个数为标准,然后一次比较,相等的话Map中的(Values)增加,依此类推,最后,这个Map容器中放的就是char字符(键)和出现次数int(值)的对应了,出现次数最多的字符c就得到了。
      

  4.   

    如果只是英文和数字包括其他键盘字符 
    可以用asi码做下标。
    如果包含中文 就用 Hash吧。最好考虑下Hash冲突 
      

  5.   

    这个不用正则可惜了:
    import java.util.Arrays;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class MyRegex {

    public static void main(String[] args) { String a = "b23chabaccscc";
    String[] arr = a.split("(?<=\\G.{1})");
    String s2 = "";
    Arrays.sort(arr);
    for (String i : arr) {
    s2 += i + "-";
    }
    Matcher m = Pattern.compile("(\\w*-)\\1*").matcher(s2);
    while (m.find()) {
    System.out.println(m.group().substring(0, 1) + "的个数:"
    + m.group().split("-").length);
    }
    }
    }
      

  6.   

    打印:
    2的个数:1
    3的个数:1
    a的个数:2
    b的个数:2
    c的个数:5
    h的个数:1
    s的个数:1
      

  7.   


            public static void main(String[] args)
    {
    Map<String, Integer> map = new HashMap<String,Integer>();
    String str="b23chabaccscc";
    String[] strArray = str.split("(?<=\\G.{1})");
    for(int i = 0 ; i < strArray.length; i++)
    {
    if(map.keySet().contains(strArray[i]))
    map.put(strArray[i], map.get(strArray[i])+1);
    else
    map.put(strArray[i], 1);
    } for(String strKey : map.keySet())
    {
    System.out.println(strKey + " number is : " + map.get(strKey));
    }
    }其中的正则表达式是参考 thc1987同志的。
      

  8.   

    呵呵,偶也是用正则实现的,不过和楼上的各位方法不同,呵呵
    互相学习了
    import java.util.Hashtable;
    import java.util.regex.*;
    public class Test1 { /**
     * @param args
     */
    String result_char;
    int result_times;

        public static void main(String[] args){
         Test1 t = new Test1();
         String test = "b23chabaccscc";
         //String test = "12seseeeeeeee11";
         t.getMax(test);
        
         System.out.println(t.result_char);
         System.out.println(t.result_times);         
        }
        
        void getMax(String input){
         char[] data = input.toCharArray();     for(int i=0;i<data.length;i++){
         int temp = 0;
         Pattern  p=Pattern.compile(String.valueOf(data[i]));
         Matcher m=p.matcher(input);
         while(m.find()){
         ++temp;
         }
        
         if(temp>result_times){
         result_times = temp;
         result_char = String.valueOf(data[i]);
         }
         }
        }
    }
      

  9.   

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;public class TestString {
    public static void main(String[] args) {
    TestString ts = new TestString();
    ts.getMaxChar("fsdfxc#@$###vsdfrewgxvxcdsfase");
    }

    void getMaxChar(String s){

    Map<String, Integer> map = new HashMap<String, Integer>();
    for(int i = 0,len = s.length(); i < len; i++){
    String falg = s.substring(i, i + 1);
    if(!map.containsKey(falg)){
    map.put(falg, 1);
    }else{
    Integer l = map.get(falg);
    l ++;
    map.put(falg, l);
    }
    }
    Integer falg = 0;
    Iterator it = map.keySet().iterator();
    while(it.hasNext()){
    String key = it.next().toString();
    Integer i = map.get(key);
    if(i > falg){
    falg = i;
    }
    }
    it = map.keySet().iterator();
    while(it.hasNext()){
    String key = it.next().toString();
    Integer i = map.get(key);
    if(i == falg){
    System.out.print(key + " ");
    }
    }
    System.out.print("出现了次数最多,等于" + falg + "次");
    }
    }
    你运行一下上面那代码,看是不是你想要的结果,不过我想这方法不太好!~不过你那字符串中字符可以为其它一些字符!~
      

  10.   

    public static void main(String[] args) {
    String temp = "b23chabaccscc";
    Map map = new HashMap();
    int length = temp.length();
    List list = new ArrayList();
    int max = 0;
    int count = 0;
    char maxC = 0;
    for(int i=0;i<length;i++) {
    char c = temp.charAt(i);
    if(map.containsKey(c)) {
    count = (Integer)map.get(c)+1;
    if(max < count){
    max = count;
    maxC = c;
    }
    map.put(c, count);
    }else {
    map.put(c, 1);
    }
    }
    System.out.print("最大次数:"+max+"  出现的字符:"+ maxC);
    }
      

  11.   

     public static void main(String[] args){    
         Map<Character,Integer> sMap=new HashMap<Character,Integer>();
         char[] temp="askdjfkssssdfggdieo".toCharArray();
         int max=0;
         char maxc=' ';
         for (int i = 0; i < temp.length; i++) {
          Integer s=sMap.get(temp[i]);
          if(s!=null&&s>max){
         max=s; 
         maxc=temp[i];
          }
          sMap.put(temp[i], s!=null?s+1:1);
         }
         System.out.println(maxc+"最多"+(max+1)+"次");       
        }
      

  12.   

    如果是不带中文的字符串的话.直接用ASCII码做是最简单的public static Character FirstNonRepeated(String string) {
            //因为是ASCII码,所以直接创建ASCII码的数组
            int[] counter = new int[128];
            //记录每个字符出现的次数
            for (int i = 0; i < string.length(); i++) {
                //记录字符的位置,数组相应位置值+1;
                char ch = string.charAt(i);
                counter[ch]++;;
            }
            int max = 0;
            char ch = '';
            //循环找出数组最大值,最大值对应的那个数组下标的字符即是出现次数最多的字符
            for (int i = 0; i < counter.length; i++) {
                    if(counter[i] > max)
                    {
                        ch = string.charAt(i);
                        max = counter[i];                }
            }
            return ch;
        }
      

  13.   

    记得加上对参数string的判断,为null和为""的情况直接return null;
      

  14.   

    import java.util.HashMap;public class TestHash { public static void main(String[] args) {

        String s = "b23chabaccscc"; HashMap<Character, Integer> h = new HashMap<Character, Integer>();
    int max = 0;
    char x = ' ';
    for (int i = 0; i < s.length(); i++) { if (!h.containsKey(s.charAt(i))) { h.put(s.charAt(i), 1); } else { h.put(s.charAt(i), h.get(s.charAt(i)) + 1);
    }
    if (h.get(s.charAt(i)) > max) {
    max = h.get(s.charAt(i));
    x = s.charAt(i);
    } }

    System.out.println("出现最多的字符是" + x);
    System.out.println("出现的次数为" + max);
    }}
      

  15.   

    import java.util.*;
    public class Aa 
    {  

    public static void main(String[] args) 
    {    
     Scanner input=new Scanner(System.in);
     System.out.println("请输入字符串:");
     String s=input.next();
     char []a=s.toCharArray();
     ArrayList<Integer> aa=new ArrayList<Integer>();
     ArrayList<Integer> bb=new ArrayList<Integer>();
     ArrayList<Integer> cc=new ArrayList<Integer>();

     for (int i=0;i<s.length() ;i++ )
     {  int number=0;
         
     for (int j=0;j<s.length() ;j++ )
     {
     if (a[i]==a[j])
     {
     number++;
     }
     
     }
     aa.add(number);
     bb.add(number);
     }
      Comparator<Integer> comp = Collections.reverseOrder();
             Collections.sort(aa, comp);

     for (int i=0;i<s.length() ;i++  )
     {
     if(bb.get(i)==aa.get(0))
     cc.add(i);

     }
     int ccc=cc.get(0);

    System.out.println("字符串中出现次数最多的是"+a[ccc]+"他们总共出现"+cc.size()+"次依次在下列几个位置");

    for (int i=0;i<cc.size() ;i++ )
    {

       System.out.print(cc.get(i)+" ");
    }
    }
    }
      

  16.   

    用HashMap存储每个字符出现的次数。
    step1.如果该字符在HashMap中没有,就创建该键,值为1,如果已存在,值就加一。
    step2.遍历HashMap,寻找最大值。
      

  17.   


    public static void main(String[] args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, IOException {
        String aa = "zuitla我我love我朝tt";
        
        char[] cs = aa.toCharArray();
        char maxStr = 0;
        int maxNumber = 0;
        Map<Character,Integer> m = new HashMap();
        for (char c : cs) {
    if(m.get(c) == null){
        m.put(c, 0);
    }else{
        if(maxNumber < (m.get(c) + 1)){
    maxNumber = (m.get(c) + 1);
    maxStr = c;
        }
        m.put(c, m.get(c) + 1);
    }
        }
        
      System.out.println(maxStr);  
    }