java中,输入字符串,把重复的输出,只能输出一次,并且输出重复了几次。
展开全部
public static void main(String[] args) {
String str ="aaabbccc";
Map<String,Integer> counterMap = new HashMap<String,Integer>();
for(int index = 0,len=str.length();index<len;index++){
String temp = str.substring(index,index+1);
if(counterMap.get(temp)==null){
counterMap.put(temp, 1);
}else{
Integer count = counterMap.get(temp);
counterMap.put(temp,count+1);
}
}
for(String key :counterMap.keySet()){
System.out.println("key:"+key+" value:"+counterMap.get(key));
}
}
-----结果------
key:b value:2
key:c value:3
key:a value:3
String str ="aaabbccc";
Map<String,Integer> counterMap = new HashMap<String,Integer>();
for(int index = 0,len=str.length();index<len;index++){
String temp = str.substring(index,index+1);
if(counterMap.get(temp)==null){
counterMap.put(temp, 1);
}else{
Integer count = counterMap.get(temp);
counterMap.put(temp,count+1);
}
}
for(String key :counterMap.keySet()){
System.out.println("key:"+key+" value:"+counterMap.get(key));
}
}
-----结果------
key:b value:2
key:c value:3
key:a value:3
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |