沒什么難度,只是需要把統計的temp(暫存需要統計的字符)和其對應的count存到map中,這樣就能一目了然的查看結果。 但是如果字符多了,又懶得自己去看,而又要選出出現次數最多的字符,也只需要操作這個map就行,這就涉及到使用合適的算法來實現,等復習了算法了再寫實現。
public class CountChar { public static void main(String[] args) { //待統計字符 String target = "aaaassdddddd"; //字符數組 String[] strings = target.split(""); String temp = null; //計數器 int count; Map<String, Integer> map = new HashMap<>(); for (int i = 0; i < strings.length; i++) { temp = strings[i]; count = 0; for (int j = 0; j < strings.length; j++) { if (temp.equals(strings[j])) { count++; } } if(!map.containsKey(temp)) { map.put(temp, count); } } System.out.新聞熱點
疑難解答