- <p>package question;
- import java.util.TreeMap;
- /**
- * 利用map知识点,获取字符串“kalskga”中每一个字母出现的次数
- *
- * 例:a(2)b(4)f(1)
- *
- * @author Administrator
- *
- */
- public class Qtest19 {
- public static void main(String[] args) {
- String str = "kalskga";
- // 将字符串转换成char数组
- char[] ch = str.toCharArray();
- //调用charToTreeMap()
- TreeMap<Character, Integer> tm = charToTreeMap(ch);
- System.out.println(tm);
- }
- public static TreeMap<Character, Integer> charToTreeMap(char[] ch) {
- // 创建TreeMap对象
- TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
- for (int i = 0; i < ch.length - 1; i++) {
- int n = 1;
- for (int j = 1 + i; j < ch.length; j++) {
- if(ch[i] == ch[j]){
- n++;
- }
- }
- tm.put(ch[i], n);
- }
- return tm;
- }
- }</p><p>
- </p><p>
- </p><p>
- </p><p>
- </p><p>
- </p><p>哪里出问题了?求指教啊……
- </p>
复制代码
|
|