统计一个字符串中出现最多的字符个数
程序员文章站
2024-01-24 13:47:46
...
[code="java"]
package date0513;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
* @author tonyJ
2011-5-13 下午02:05:00 */ public class Test01 { public static void main(String[] args) { countMaxStr("111dfasfdddd"); } @SuppressWarnings("unchecked") public static void countMaxStr(String str) { char[] c = str.toCharArray(); Map<character integer> map = new HashMap<character integer>(); for (int i = 0; i iter = map.entrySet().iterator(); int max = 0; char ch = ' '; while (iter.hasNext()) { Map.Entry entry = (Entry) iter.next(); if ((Integer) entry.getValue() > max) { max = (Integer) entry.getValue(); ch = (Character) entry.getKey(); } } System.out.println("最多的字符是:" + ch); System.out.println("出现的次数是:" + max); } } [/code]</character></character>
2011-5-13 下午02:05:00 */ public class Test01 { public static void main(String[] args) { countMaxStr("111dfasfdddd"); } @SuppressWarnings("unchecked") public static void countMaxStr(String str) { char[] c = str.toCharArray(); Map<character integer> map = new HashMap<character integer>(); for (int i = 0; i iter = map.entrySet().iterator(); int max = 0; char ch = ' '; while (iter.hasNext()) { Map.Entry entry = (Entry) iter.next(); if ((Integer) entry.getValue() > max) { max = (Integer) entry.getValue(); ch = (Character) entry.getKey(); } } System.out.println("最多的字符是:" + ch); System.out.println("出现的次数是:" + max); } } [/code]</character></character>