• 201521123095 《Java程序设计》第8周学习总结


    1. 本周学习总结##

    1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容。

    2. 书面作业##

    本次作业题集集合##

    Q1 List中指定元素的删除(题目4-1)##

    1.1 实验总结

    对于删除函数,用contains方法查看是否存在,如果找到了,删除后,list的长度就减一了,此时循环计数i也要减一。利用charAt得到的字符类型数据如果变为字符串以及注意到在删除元素后遍历下标要减1即可。
    

    Q2 统计文字中的单词数量并按出现次数排序(题目5-3)##

    2.1 伪代码(简单写出大体步骤)

    while (input.hasNextLine()) {
                String str = input.next();
                if (str.equals("!!!!!")){
                    break;}
                else if (!str.contains(str))
                    str.add(str);
            }
    

    2.2 实验总结

    这题关键就是使用了Set的自然排序实现类TreeSet,此类可以对添加的对象进行默认排序。
    

    Q3 倒排索引(题目5-4)##

    3.1 截图你的提交结果(出现学号)

    3.2 伪代码(简单写出大体步骤)

    while (!lineword.equals("!!!!!")) {
        String[] words = lineword.split(" ");
        for (String word : words) {
            Set<Integer> subIndex = new TreeSet<Integer>();
            if (!index.containsKey(word)) {
                subIndex.add(line);
                index.put(word, subIndex);
            } 
                    else {
                subIndex = index.get(word);
                if (!subIndex.contains(line)) {
                subIndex.add(line);
                index.put(word, subIndex);
                 }
         }
    }
    

    3.3 实验总结

    要正确地处理Map内部的键值对的各类操作,在读每一行时候,用一个map映射,存放这一整行的信息和行数,读取每一行的每个单词,放入key为String, value为TreeSet<Integer>,即出现的行数。输出部分也要注意考虑。
    

    Q4 Stream与Lambda##

    编写一个Student类,属性为:
    private Long id;
    private int age;
    private Gender gender;//枚举类型
    private boolean joinsACM; //是否参加过ACM比赛
    创建一集合对象,如List,内有若干Student对象用于后面的测试。

    4.1 使用传统方法编写一个方法,将id>10,name为zhang, age>20, gender为女,参加过ACM比赛的学生筛选出来,放入新的集合。在main中调用,然后输出结果。
    关键代码:

    ArrayList<Student> list =new ArrayList<Student>();
    for(Student a:s){
        if(a.getId()>10&&a.getName().equals("zhang")&&a.getAge()>20&&a.getGender().equals(Gender.female)&&a.isJoinsACM()){
        list.add(a);        
    }
    

    结果为

    [Student{id=11, name='zhang', age=21, gender=female, joinsAcm=true}]
    [Student{id=12, name='zhang', age=25, gender=female, joinsAcm=true}]
    

    4.2 使用java8中的stream(), filter(), collect()编写功能同4.1的函数,并测试。
    关键代码

    ArrayList<Student> arrayList2 = (ArrayList<Student>) arrayList.parallelStream()
            .filter(student -> (student.getId() > 10L && student.getName().equals("zhang")
                    && student.getAge() > 20 && 
                    student.getGender().equals(Gender.female)
                    && student.isJoinsACM()))
            .collect(Collectors.toList());
    

    4.3 构建测试集合的时候,除了正常的Student对象,再往集合中添加一些null,然后重新改写4.2,使其不出现异常。

        List<Student> result=student.stream().filter(stu-> stu!=null&&stu.getId() > 10L && stu.getName().equals("zhang")
                && stu.getAge() > 20 &&
                stu.getGender().equals(Gender.FEMALE)
                && stu.isJoinsAcm())
                .collect(Collectors.toList());
    

    Q5 泛型类:GeneralStack(题目5-5)##

    5.1 截图你的提交结果(出现学号)

    5.2 GeneralStack接口的代码

    interface GeneralStack<E>
    {
        E push(E item);
        E pop();
        E peek();
        public boolean empty();
        public int size();
    }
    

    5.3 结合本题,说明泛型有什么好处

    泛型将操作的数据类型进行了参数化,使之和一个参数一样可变动,这道题中所编写的GeneralStack接口对于任何引用类型都适用,我们可以根据实际定义所需的数据类型,使得编写的代码可以被不同类型的对象所使用。如输入为"Integer"时,就将T替换为Integer,而无需定义多个接口
    

    Q6 基础参考文件GenericMain,在此文件上进行修改。##

    6.1 编写方法max,该方法可以返回List中所有元素的最大值。List中的元素必须实现Comparable接口。编写的max方法需使得String max = max(strList)可以运行成功,其中strList为List类型。也能使得Integer maxInt = max(intList);运行成功,其中intList为List类型。

     public static void main(String[] args) {
    
    List<String> list1 = new ArrayList<String>();
    list1.add("abc");
    list1.add("cba");
    list1.add("acb");
    String min=Collections.max(list1);
    System.out.println(min);
    }
    
    public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)
    {
        return Collections.max(coll);
    }
    

    3. 码云上代码提交记录及PTA实验总结##

    题目集:jmu-Java-05-集合

    3.1. 码云代码提交记录

    在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图

    3.2. PTA实验

    函数(4-1),编程(5-3,5-4,5-5)
    实验总结已经在作业中体现,不用写。

  • 相关阅读:
    针对数据库索引的优化
    acd
    HDOJ 5045 Contest
    《计算机时代》2015年第7期刊登出《基于数据仓库星形模式的广东省快速公路一张网资金结算情况分析系统》
    为什么大多数编程语言中的数组都从0開始
    十年,青春就是一转眼的事
    电子商务系统的设计与实现(十四):菜单高亮
    最近1个月的财务计划没有做好,囧啊
    最近1个月的财务计划没有做好,囧啊
    雷观(十九):我的人生观
  • 原文地址:https://www.cnblogs.com/YYYYYYY/p/6715543.html
Copyright © 2020-2023  润新知