沒什么難度,只是需要把統(tǒng)計(jì)的temp(暫存需要統(tǒng)計(jì)的字符)和其對(duì)應(yīng)的count存到map中,這樣就能一目了然的查看結(jié)果。 但是如果字符多了,又懶得自己去看,而又要選出出現(xiàn)次數(shù)最多的字符,也只需要操作這個(gè)map就行,這就涉及到使用合適的算法來(lái)實(shí)現(xiàn),等復(fù)習(xí)了算法了再寫實(shí)現(xiàn)。
public class CountChar { public static void main(String[] args) { //待統(tǒng)計(jì)字符 String target = "aaaassdddddd"; //字符數(shù)組 String[] strings = target.split(""); String temp = null; //計(jì)數(shù)器 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.新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注