package cn.com.lianxi;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class demo {
public static void main(String[] args) {
Map<String,Integer> map=getcount("abcdbcd");
for(String s:map.keySet()){
System.out.println(s+":"+map.get(s));
}
}
public static Map<String,Integer> getcount(String str){
String[] ch=str.split("");//分隔字符串,获得字符串数组
List<String> list=new ArrayList<String>();//用于记录出现的字符
list.add("");
int count;
Map<String,Integer> map=new HashMap<String, Integer>();
outer:
for(int i=0;i<ch.length;i++){
count=0;//记录字符出现的个数,每次循环前需要重置为0
String s=ch[i];//获得字符
//判断字符是不是前面出现过,如果是的话,就跳过这次循环
for(int j=0;j<list.size();j++){
if(s.equals(list.get(j))){
continue outer;//跳出最外面的循环,继续执行下一个
}
}
//匹配字符出现的个数,count是记录个数的
for(int m=0;m<ch.length;m++){
if(s.equals(ch[m])){
count++;
}
}
map.put(s, count);//存储数据
list.add(s);//获取每次的字符,用于过滤掉后面出现的相同字符
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class demo {
public static void main(String[] args) {
Map<String,Integer> map=getcount("abcdbcd");
for(String s:map.keySet()){
System.out.println(s+":"+map.get(s));
}
}
public static Map<String,Integer> getcount(String str){
String[] ch=str.split("");//分隔字符串,获得字符串数组
List<String> list=new ArrayList<String>();//用于记录出现的字符
list.add("");
int count;
Map<String,Integer> map=new HashMap<String, Integer>();
outer:
for(int i=0;i<ch.length;i++){
count=0;//记录字符出现的个数,每次循环前需要重置为0
String s=ch[i];//获得字符
//判断字符是不是前面出现过,如果是的话,就跳过这次循环
for(int j=0;j<list.size();j++){
if(s.equals(list.get(j))){
continue outer;//跳出最外面的循环,继续执行下一个
}
}
//匹配字符出现的个数,count是记录个数的
for(int m=0;m<ch.length;m++){
if(s.equals(ch[m])){
count++;
}
}
map.put(s, count);//存储数据
list.add(s);//获取每次的字符,用于过滤掉后面出现的相同字符
}
return map;}
}