zoukankan      html  css  js  c++  java
  • 框架习题

    1、有如下Student对象,属性如图所示:

    其中classNum表示学生的班级编号,比如:class05

    有如下List:

    List list = new ArrayList();
           list.add(new Student(“Tom”, 18, 100, “class05”));
           list.add(new Student(“Jerry”, 22, 70, “class04”));
           list.add(new Student(“Owen”, 25, 90, “class05”));
           list.add(new Student(“Jim”, 30,80 , “class05”));
           list.add(new Student(“Steve”, 28, 66, “class06”));
           list.add(new Student(“Kevin”, 24, 100, “class04”));

    在这个list 的基础上,完成下列要求:
           1) 计算所有学生的平均年龄
           2) 计算各个班级的平均分

    package com.newer.cn;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    public class TestStudent {
    
        public static void main(String[] args) {
            List<Student> list = new ArrayList<Student>();
            list.add(new Student("Tom", 18, 100, "class05"));
            list.add(new Student("Jerry", 22, 70, "class04"));
            list.add(new Student("Owen", 25, 90, "class05"));
            list.add(new Student("Jim", 30, 80, "class05"));
            list.add(new Student("Steve", 28, 66, "class06"));
            list.add(new Student("Kevin", 24, 100, "class04"));
            list.add(new Student("Jim", 30, 80, "class07"));
            list.add(new Student("Steve", 28, 66, "class06"));
            list.add(new Student("Kevin", 24, 100, "class07"));
            // 求平均年龄
            double sumAge = 0;
            for (Student stu : list) {
                sumAge += stu.getAge();
            }
            /*for (int i = 0; i < list.size(); i++) {
                Student stu =list.get(i);
                sumAge +=stu.getAge();            
            }*/
            System.out.println("平均年龄是:" + sumAge / list.size());
            // 求每个班的平均分
            Map<String, List<Student>> map = new HashMap<String, List<Student>>();
            for (int i = 0; i < list.size(); i++) {
                Student s = list.get(i);
                String classNum = s.getClassNum();
                if (map.containsKey(classNum)) {
                    map.get(classNum).add(s);
                } else {
                    List<Student> l = new ArrayList<Student>();
                    l.add(s);
                    map.put(classNum, l);
                }
            }
            // [class06,class05,class04]
            Set<String> cls = map.keySet();
            for (String s : cls) {
                double sumScore = 0;
                List<Student> ls = map.get(s);
                for (Student student : ls) {
                    sumScore += student.getScore();
                }
                System.out.println(s + "班的平均分是:" + sumScore / ls.size());
            }
        }
    
    }

    2、已知有16支足球队参加2008北京奥运会。写一个程序,随机把16支球队分成四组

    package com.newer.cn;
    
    import java.sql.Array;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Map;
    
    public class Test1 {
    
        public static void main(String[] args) {
            List<String> list = new ArrayList<String>();
            list.add("A");
            list.add("B");
            list.add("C");
            list.add("D");
            list.add("E");
            list.add("F");
            list.add("G");
            list.add("H");
            list.add("I");
            list.add("J");
            list.add("K");
            list.add("L");
            list.add("M");
            list.add("N");
            list.add("O");
            list.add("P");
            System.out.println(list);
            Collections.shuffle(list);
            System.out.println(list);
            List<List<String>> group = new ArrayList<List<String>>();
            group.add(new ArrayList<String>());
            group.add(new ArrayList<String>());
            group.add(new ArrayList<String>());
            group.add(new ArrayList<String>());
            int a=0;
            for (int i = 0; i < list.size(); i++) {
                group.get(a).add(list.get(i));
                if(group.get(a).size()==4)
                    a++;
            }
            for (List k : group)
                System.out.println(k);
    
        }
    
    }
  • 相关阅读:
    【剑指Offer】21、栈的压入、弹出序列
    【剑指Offer】20、包含min函数的栈
    【剑指Offer】19、顺时针打印矩阵
    【Shell编程】Shell基本语法
    【Shell编程】Shell程序设计
    linux 大中括号变量解读
    Python 二进制,十进制,十六进制转换
    Python3.x和Python2.x的区别
    python通过SSH登陆linux并操作
    PEP8特性
  • 原文地址:https://www.cnblogs.com/lujing-newer/p/6567671.html
Copyright © 2011-2022 走看看