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


    1. 本周学习总结

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

    2. 书面作业

    本次作业题集集合

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

    1.1 实验总结
    答:实现convertStringToList函数主要是建立个Scanner扫描器。实现remove函数可以用迭代器进行遍历,通过next()方法将String根据空格断开。

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

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

    1.用while循环遍历每个单词,在map中建立键值对应。
    2.将map对象转化为list对象。
    3.对list的对象进行排序,然后输出。
    

    2.2 实验总结
    答:map对象转化为list对象时,用entrySet()可以同时获得map的键和值,运用Collection.sort方法排序会很方便。

    3.倒排索引(题目5-4)

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

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

    1.在map中建立键值对应;
    2.向集合逐个加入元素;
    3.if(新元素为!!!!!)break;
    4.遍历TreeMap,if(map.containsKey(key))return map中所有的value值;
    

    3.3 实验总结
    答:对map的理解、使用不够透彻,在同学的指导下才完成,还需要进一步透析。

    4.Stream与Lambda

    编写一个Student类,属性为:

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

    List<Student> list = new ArrayList<Student>();
    list.add(new Student(21L,"zheng",21,getGender(2),true));
    list.add(new Student(20L,"ling",21,getGender(1),false));
    list.add(new Student(1L,"wang",20,getGender(1),true));
    list.add(new Student(25L,"zhang",25,getGender(2),true));
    list.add(new Student(20L,"zhang",20,getGender(1),false));
    

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

    for(i=0;i<list.size();i++){
        if((list.get(i).getId()>10)&&(list.get(i).getName().equals("zhang"))&&(list.get(i).getAge()>20)&&(list.get(i).getGender().equals(getGender(2)))&&(list.get(i).isJoinsACM()==true)){
            list2.add(list.get(i));
        }
    }
    

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

     ArrayList<Student> list = (ArrayList<Student>) list.stream().filter(e -> e.getId() > 10)
                    .filter(e -> e.getAge() > 20).filter(e -> e.getGender().equals(Gender.2)).filter(e -> e.isJoinsACM())
                    .collect(Collectors.toList());
    
            list.forEach(System.out::println);
    

    运行结果相同

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

    ArrayList<Student> list = (ArrayList<Student>) list.stream().filter(e -> e != null)
                    .filter(e -> e.getId() > 10).filter(e -> e.getAge() > 20).filter(e -> e.getGender().equals(Gender.2))
                    .filter(e -> e.isJoinsACM()).collect(Collectors.toList());
    
            list.forEach(System.out::println);
    

    未出现异常

    5.泛型类: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 结合本题,说明泛型有什么好处
    答:本题中Stack需要实现Interger、Double和Car三种类型,编写一个对任何引用类型的数据都适用的GeneralStack接口,就不需要一个个去定义,大大提高了效率。

    6.泛型方法

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

    public static <T extends Comparable<T>> T max(List<T> list)
       {                     
          T max = list.get(0);
          for (T t : list) {
            if ( t.compareTo( max ) > 0 ){
             max = t; 
          }
        }
          return max; 
       }```
    
    #3. 码云上代码提交记录及PTA实验总结
    
    题目集:jmu-Java-05-集合
    
    ##3.1. 码云代码提交记录
    
    在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图
    ![](http://images2015.cnblogs.com/blog/1109849/201704/1109849-20170415180459658-513527908.png)
  • 相关阅读:
    Luogu P1396 营救
    Luogu P1339 热浪Heat Wave
    哈夫曼树学习笔记
    题解 CF1372C
    题解 CF 1372 B
    题解 CF 1372A
    题解 UVA1193 Radar Installation
    题解 洛谷 P2287 [USACO07NOV]Sunscreen G
    洛谷 P1080 国王游戏 题解
    牛客练习赛 66C公因子 题解
  • 原文地址:https://www.cnblogs.com/ltykm/p/6715451.html
Copyright © 2020-2023  润新知