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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 9527个人 中级黑马   /  2016-9-20 00:09  /  1909 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

1.分析以下需求,并用代码实现:
        (1)定义一个学生类Student,包含属性:姓名(String name)、年龄(int age)
        (2)定义Map集合,用Student对象作为key,用字符串(此表示表示学生的住址)作为value
        (3)利用四种方式遍历Map集合中的内容,格式:key::value

/*
public class Demo1_Test1 {

        /**
         (1)定义一个学生类Student,包含属性:姓名(String name)、年龄(int age)
         (2)定义Map集合,用Student对象作为key,用字符串(此表示表示学生的住址)作为value
         (3)利用四种方式遍历Map集合中的内容,格式:key::value
         
        public static void main(String[] args) {
                Map<Student, String> hmp = new HashMap<Student, String>();                //定义一个map集合
                hmp.put(new Student("张三", 23), "北京");                                                        //添加元素
                hmp.put(new Student("李四", 24), "上海");
                hmp.put(new Student("武王", 25), "广州");
                Set<Student> set = hmp.keySet();                                                                //获取所有键的结合
       
                Iterator<Student> it = set.iterator();                                                        //后去学生的迭代器
                Iterator it1 = hmp.entrySet().iterator();                                                //
                method4(hmp);
               
//                method3(hmp);
//                method2(map, set);
//                System.out.println("-------------------------------------");
//                method1(map, it);
       
               
               
        }

        public static void method4(Map<Student, String> hmp) {
                Iterator<Map.Entry<Student, String>> it11 = hmp.entrySet().iterator();
                  while (it11.hasNext()) {
                   Map.Entry<Student, String> en = it11.next();
                   System.out.println(en.getKey().getName() + en.getKey().getAge() + en.getValue());
                  }
        }

        public static void method3(HashMap<Student, String> hmp) {
                for (Map.Entry<Student, String> stu : hmp.entrySet()) {
                        System.out.println( stu.getKey().getName() + stu.getKey().getAge() + stu.getValue() );
                }
        }

        public static void method2(Map<Student, String> map, Set<Student> set) {
                for (Student s : set) {
                        System.out.println(s.getName() + s.getAge() + map.get(s));
                }
        }

        public static void method1(Map<Student, String> map, Iterator<Student> it) {
                while (it.hasNext()) {
                        Student s = it.next();
                        System.out.println(s.getName() + s.getAge() + map.get(s));
                }
        }
*/
       
2.分析以下需求,并用代码实现:
        (1)利用键盘录入,输入一个字符串
        (2)统计该字符串中各个字符的数量
        (3)如:
                用户输入字符串"If~you-want~to~change-your_fate_I_think~you~must~come-to-the-dark-horse-to-learn-java"
                程序输出结果:-(9)I(2)_(3)a(7)c(2)d(1)e(6)f(2)g(1)h(4)i(1)j(1)k(2)l(1)m(2)n(4)o(8)r(4)s(2)t(8)u(4)v(1)w(1)y(3)~(6)

       
        /*
                public static void main(String[] args) {
                Scanner  sc1 = new Scanner(System.in);                                                        //创建键盘录入对象
                System.out.println("请输入一个字符串:");
                String s1 = sc1.nextLine();                                                                                //接收录入的字符串
                char [] c1 = s1.toCharArray();                                                                        //将字符串转换成字符数组
                LinkedHashMap<Character, Integer> lhm = new LinkedHashMap<>();        //创建map集合
                Arrays.sort(c1);                                                                                                //调用sort方法,将字符数组排序
                for (char d : c1) {
                        lhm.put(d, lhm.containsKey(d) ? lhm.get(d) + 1 : 1);                //将字符存进集合的键,如果有重复的,值加1
                }
                for (Character c : lhm.keySet()) {
               
                        System.out.print(c + "(" + lhm.get(c) + ")");                                //遍历并打印数组
                }
               
       
        }
        */
3.分析以下需求,并用代码实现:
        (1)统计每个单词出现的次数
        (2)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
        (3)打印格式:
                to=3
                  think=1
                  you=2
                  //........
        /*
                public static void main(String[] args) {
                String s1 = "If you want to change your fate I think you must come to the dark horse to learn java";
                String[] s2 = s1.split(" ");                                                                //将字符串切割字符数组
               
                HashMap<String, Integer> hm= new HashMap<>();                                //创建map集合
                for (String st : s2) {                               
                        hm.put(st, hm.containsKey(st) ? hm.get(st) + 1 : 1);        //遍历字符串数组,将字符添加到集合的键中,如果有重复,值加1
                }
               
                for (String s : hm.keySet()) {
                        System.out.println(s + " = " + hm.get(s));                                //遍历集合
                }
        }
        */
               
                       
4.练习今天的课堂代码


3 个回复

倒序浏览
hping 来自手机 初级黑马 2016-9-20 00:14:04
沙发
赞一个!!!
回复 使用道具 举报
可以的,顶一顶!
回复 使用道具 举报
厉害,正好是今天的题目
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马