获取该字符串中每一个字母出现的个数 “GZITCASTADVANCEDTRAININGJAVASEJAVAEE ANDROID” import java.util.TreeMap; public class Test5 { public static void main(String[] args) { String str = null; try { str = "GZITCASTADVANCEDTRAININGJAVASEJAVAEE ANDROID"; } catch (Exception e) { e.printStackTrace(); } Map tree = new TreeMap(); for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { if (!tree.containsKey(ch)) { tree.put(ch, new Integer(1)); } else { Integer in = (Integer) tree.get(ch) + 1; tree.put(ch, in); } } } Iterator tit = tree.keySet().iterator(); while (tit.hasNext()) { Object temp = tit.next(); System.out.print(temp.toString() + "(" + tree.get(temp) + ")"); } } } 输出:A(9)C(2)D(4)E(4)G(2)I(4)J(2)N(4)O(1)R(2)S(2)T(3)V(3)Z(1) |