本文實(shí)例講述了java計(jì)算給定字符串中出現(xiàn)次數(shù)最多的字母和該字母出現(xiàn)次數(shù)的方法。分享給大家供大家參考,具體如下:
import Java.util.Collections;import java.util.Map;import java.util.TreeMap;public class TestStringSplict { public static void main(String[] args){ String str = "aaaaaaaccccccccccccccccccccccaaaabb";// 用map實(shí)現(xiàn)// TreeMap<Character,Integer> map = new TreeMap<Character,Integer>();// for(Character ch : str.toCharArray()){// if((ch>='a' && ch<'z')||(ch>'A' && ch<'Z')){// Integer count = map.get(ch);// map.put(ch, null==count?1:count+1);// }// }//// System.out.println(Collections.max(map.values())); //用普通數(shù)組實(shí)現(xiàn) int[] aa = new int[60]; for(char temp:str.toCharArray()){ if((temp>=65 && temp<=90)||(temp>=97 && temp<=122)){ temp -= 65; aa[temp]++; } } int max = aa[0]; int position = 0; for(int i=0;i<aa.length;i++){ if(aa[i]>max){ max = aa[i]; position = i; } } System.out.println(max); System.out.println("字母"+(char)(position+65) + "出現(xiàn)" + max + "次"); }}PS:這里再為大家推薦幾款在線字符統(tǒng)計(jì)工具供大家參考:
在線字?jǐn)?shù)統(tǒng)計(jì)工具:
http://tools.VeVB.COm/code/zishutongji
在線字符統(tǒng)計(jì)與編輯工具:
http://tools.VeVB.COm/code/char_tongji
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選