• 201521123079《java程序设计》第8周学习总结


    1. 本周学习总结

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

    2. 书面作业

    本次作业题集集合

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

    1.1 实验总结

    • 这题主要是对函数remove和covnertStringToList的编写, convertStringToList中,主要就是Scanner sc=new Scanner(nextLine),以及使用sc.next()添加字符串
    • remove函数中删除list中元素时要考虑删除后的位置。

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

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

    • 用while循环将不重复的单词加入集合strSet
    if (strSet.size() <= 10)
                for (String s : strSet) {
                    System.out.println(s);
                }
            else{
                            int m=10;
                for (String s : strSet) {
                    if (m-- <= 0)
                        break;
                    System.out.println(s);
                }
                    }
    

    2.2 实验总结

    在判断一个元素是否要加入strSet时应该判断这个元素在其中是否存在。

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

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

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

     while (in.hasNextLine()) {
                    String str2 = in.nextLine();
                    String[] str2list = str2.split(" ");
                    ArrayList<Integer> arr = new ArrayList<Integer>();
                    for (int i = 0; i < strset.size(); i++) {
                        for (int j = 0; j < str2list.length; j++) {
                                                   //找出共有行数 
                        }
                    }
                    if (!arr.isEmpty()) {
                        System.out.println(arr);
                                            //输出所在行的字符串
                    }
                    else
                        System.out.println("found 0 results");
                }
    

    3.3 实验总结

    利用split()分割字符串,使之以空格为分界将字符串分为一个个单词。

    4.Stream与Lambda

    编写一个Student类,属性为:

    private Long id;
    private String name;
    private int age;
    private Gender gender;//枚举类型
    private boolean joinsACM; //是否参加过ACM比赛

    4.创建一集合对象,如List,内有若干Student对象用于后面的测试。

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

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

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

    5.泛型类:GeneralStack(题目5-5)

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

    5.2 GeneralStack接口的代码

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

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

    泛型允许指定集合中元素的类型,这就可以得到强类型,在编译时进行类型检查。从此无需使用有风险的强制类型转换;正如此题,存在Integer型,String型,Car型,如果不使用泛型,要么要进行强制转换,有可能运行时出现错误,使用了泛型接口,错误在编译阶段就能发现,不用等到运行时才发现出错。

    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 (int i = 0; i < list.size(); i++) {
            if(list.get(i).compareTo(max)>0){
                max=list.get(i);
            }
        }
        return max;
    }
    

    运行结果如下:

    3.码云提交记录

  • 相关阅读:
    请求转发和请求重定向的区别
    查看电脑连过的WiFi密码
    linux mysql不能远程登录
    map的遍历方法
    ________________springbootのMybatis
    ________________springbootのTest
    ________________springbootの自定义starter
    ________________springbootのAOP
    ________________springbootのjdbc、事物
    ________________初学springboot14
  • 原文地址:https://www.cnblogs.com/xjn12138/p/6715563.html
Copyright © 2020-2023  润新知