美文网首页
找出字符串中出现最多的字符和次数

找出字符串中出现最多的字符和次数

作者: fuel | 来源:发表于2016-08-14 22:03 被阅读0次

可以将字符作为数组的下标,通过遍历数组,得到最多的字符和次数

public static void showMostCharAndNum(String str){
  int asciis = new int[256];
  int maxIndexAndChar = 0;
  for(int i=0;i<str.lengyh();++i){
    assciis[str.charAt(i)]++;
    if(ascoos[str.charAt(i)] > maxIndexAndChar)
        maxIndexAndChar=str.charAt(i);
    }
System.out.println((char)maxIndexAndChar + " "+asciis[maxIndexAndChar]);
}  

相关文章

网友评论

      本文标题:找出字符串中出现最多的字符和次数

      本文链接:https://www.haomeiwen.com/subject/bkzysttx.html