A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 827093172 中级黑马   /  2016-4-9 12:22  /  2375 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

6黑马币
1、 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,
  输出格式为:a(2)b(1)k(2)...

最佳答案

查看完整内容

public class Demo01 { public static void main(String[] args) { String s = "fdskai2353425hfdsakhfdska"; Map map = StringList(s); //接受一个字符串,返回一个map集合. ListMap(map); //接受一个map集合,遍历并打印. } public static void ListMap(Map map) {//遍历map Set se = new HashSet(); se = map.entrySet(); Iterator it = se.iterator(); while(it.hasNext()) { Entry en = (Entry)it.next ...

3 个回复

倒序浏览
public class Demo01 {
        public static void main(String[] args) {
                String s = "fdskai2353425hfdsakhfdska";
                Map<Character, Integer> map = StringList(s); //接受一个字符串,返回一个map集合.
                ListMap(map); //接受一个map集合,遍历并打印.
        }

        public static void ListMap(Map<Character, Integer> map) {//遍历map
                Set se = new HashSet();
                se = map.entrySet();
                Iterator it = se.iterator();
                while(it.hasNext())
                {
                        Entry en = (Entry)it.next();
                        System.out.print(en.getKey() + "(" + en.getValue() + ")");
                }
        }

        public static Map<Character, Integer> StringList(String s) {  //将字符串遍历
                char[] ch = s.toCharArray();
                Map<Character, Integer> map = new HashMap<Character, Integer>();
                for(int x = 0; x < ch.length ; x ++)
                {
                        int a = 0;  //定义一个变量,每次都是从x角标在y循环中获取次数.
                        for(int y = 0; y < ch.length; y ++)
                        {
                                if(ch[x]== ch[y])
                                {
                                        a ++;  //如果相同了,就让变量加一.
                                }
                        }
                        if(!map.containsKey(ch[x])) //如果不存在才装进来.保证这个value是最大的次数值.
                        {
                                map.put(ch[x], a);
                        }
                }
                return map;
        }
}
回复 使用道具 举报
public class Demo01 {
        public static void main(String[] args) {
                String s = "fdskai2353425hfdsakhfdska";
                Map<Character, Integer> map = StringList(s); //接受一个字符串,返回一个map集合.
                ListMap(map); //接受一个map集合,遍历并打印.
        }

        public static void ListMap(Map<Character, Integer> map) {//遍历map
                Set se = new HashSet();
                se = map.entrySet();
                Iterator it = se.iterator();
                while(it.hasNext())
                {
                        Entry en = (Entry)it.next();
                        System.out.print(en.getKey() + "(" + en.getValue() + ")");
                }
        }

        public static Map<Character, Integer> StringList(String s) {  //将字符串遍历
                char[] ch = s.toCharArray();
                Map<Character, Integer> map = new HashMap<Character, Integer>();
                for(int x = 0; x < ch.length ; x ++)
                {
                        int a = 0;  //定义一个变量,每次都是从x角标在y循环中获取次数.
                        for(int y = 0; y < ch.length; y ++)
                        {
                                if(ch[x]== ch[y])
                                {
                                        a ++;  //如果相同了,就让变量加一.
                                }
                        }
                        if(!map.containsKey(ch[x])) //如果不存在才装进来.保证这个value是最大的次数值.
                        {
                                map.put(ch[x], a);
                        }
                }
                return map;
        }
}
回复 使用道具 举报
public class Demo01 {
        public static void main(String[] args) {
                String s = "fdskai2353425hfdsakhfdska";
                Map<Character, Integer> map = StringList(s); //接受一个字符串,返回一个map集合.
                ListMap(map); //接受一个map集合,遍历并打印.
        }

        public static void ListMap(Map<Character, Integer> map) {//遍历map
                Set se = new HashSet();
                se = map.entrySet();
                Iterator it = se.iterator();
                while(it.hasNext())
                {
                        Entry en = (Entry)it.next();
                        System.out.print(en.getKey() + "(" + en.getValue() + ")");
                }
        }

        public static Map<Character, Integer> StringList(String s) {  //将字符串遍历
                char[] ch = s.toCharArray();
                Map<Character, Integer> map = new HashMap<Character, Integer>();
                for(int x = 0; x < ch.length ; x ++)
                {
                        int a = 0;  //定义一个变量,每次都是从x角标在y循环中获取次数.
                        for(int y = 0; y < ch.length; y ++)
                        {
                                if(ch[x]== ch[y])
                                {
                                        a ++;  //如果相同了,就让变量加一.
                                }
                        }
                        if(!map.containsKey(ch[x])) //如果不存在才装进来.保证这个value是最大的次数值.
                        {
                                map.put(ch[x], a);
                        }
                }
                return map;
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马