标签:pac first 问题: port integer sys map lin length
解决这个问题,首先得解决2个小问题:
1、因为是任意给定的任意字符串,你怎样知道需要打印的次数?
2、用循环打印 怎样不重复打印
上个栗子:
1 package kimi; 2 3 import java.util.LinkedHashMap; 4 import java.util.Map; 5 import java.util.Set; 6 7 public class Text$$$$ { 8 public static Map<String, Integer> map = new LinkedHashMap<>(); 9 10 public static Map<String, Integer> getLetterCountByString(String string) { 11 int length = string.length(); 12 String first = string.substring(0, 1); 13 string = string.replace(first, ""); 14 map.put(first, length - string.length()); 15 return string.length() > 0 ? getLetterCountByString(string) : map; 16 } 17 18 public static void main(String[] args) { 19 Map<String, Integer> map = getLetterCountByString("sdsdas s2334@@ 我睡觉哦 sda3 "); 20 Set<String> set = map.keySet(); 21 for (String key : set) { 22 System.out.println("key:" + key + " value:" + map.get(key)); 23 } 24 } 25 }
第15行代码运用递归的思想,不需要知道具体打印的次数,只要给定一个目标,利用三目运算符,比普通for循环减少了代码量,
运行结果如下:
key:s value:5 key:d value:3 key:a value:2 key: value:4 key:2 value:1 key:3 value:3 key:4 value:1 key:@ value:2 key:我 value:1 key:睡 value:1 key:觉 value:1 key:哦 value:1
标签:pac first 问题: port integer sys map lin length
原文地址:http://www.cnblogs.com/flash3/p/7123031.html