zoukankan      html  css  js  c++  java
  • 201521123002《Java程序设计》第8周学习总结

    1. 本周学习总结

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

    2. 书面作业

    本次作业题集集合

    1.List中指定元素的删除(题目4-1)
    1.1 实验总结

    1.提交函数实验题时只要提交编写的函数即可,不然会出现编译错误
    2.出现了一次答案错误,点击查看当时的代码,发现错误点为误用"=="比较字符串。

    2.统计文字中的单词数量并按出现次数排序(题目5-3)
    2.1 伪代码(简单写出大体步骤)

    while(has next word)
     	if(the map doesn't contains the word)
     		put the word into the map
     	else
     		get the value in the map to add 1
    sort the map
    printout the result 
    

    1.用HashMap先建立一个映射关系,

    关键处

                if(cnt==null){
                    map.put(key, 1);
                }else{
                    map.put(key, cnt+1);
                }
    

    2.然后转化为集合用sort排序编写compare

    关键处

    public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
                    // TODO Auto-generated method stub
                    int x=o2.getValue()-o1.getValue();
                    if(x!=0)
                        return x;
                    else
                        return o1.getKey().compareTo(o2.getKey());
                }
    

    3.最后输出

    2.2 实验总结

    1.可以看到出现了编译错误用来是pta识别不了jdk8的语法,所以之后提交的时候不能用jdk8的语法。

    2.在控制台输入的时候不能用line

    3.倒排索引(题目5-4)
    3.1 截图你的提交结果(出现学号)

    3.2 伪代码(简单写出大体步骤)
    1.建索引 Map<String,Set<Integer>> map=new TreeMap<String,Set<Integer>>();
    2.分为只有一个单词和多个单词来查找

    create a map to keep the reference
    printout the reference
    search the keywords
    create an Collection to keep the result
    if(the Collection  is empty)
    	output "found 0 results"
    else 
    	output Collection
    

    3.3 实验总结

    这题还是比较难的,用到了很多的种集合类型,做完这题收获还是比较大的,学会用s.split(" "),但是我对自己查找单词的算法不是很满意,感觉有点冗长。算法啊算法。

    4.Stream与Lambda
    编写一个Student类,属性为:

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

    创建一集合对象,如List,内有若干Student对象用于后面的测试。
    4.1 使用传统方法编写一个方法,将id>10,name为zhang, age>20, gender为女,参加过ACM比赛的学生筛选出来,放入新的集合。在main中调用,然后输出结果。


    结果:

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

    public static List<Student> Search(List<Student> list){
    		List<Student> t= list.stream().filter(e->e.getId()>10l&&e.getName().equals("zhang")
    				&&e.getGender().equals(Gender.女)&&e.isJoinsACM()&&e.getAge()>20).collect(Collectors.toList());
    		return t;
    	}
    

    功能一样,结果一样。

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

    public static List<Student> Search(List<Student> list){
    		List<Student> t= list.stream().filter(e->e != null&&e.getId()>10l&&e.getName().equals("zhang")
    				&&e.getGender().equals(Gender.女)&&e.isJoinsACM()&&e.getAge()>20).collect(Collectors.toList());
    		return t;
    	}
    

    5.泛型类:GeneralStack(题目5-5)
    5.1 截图你的提交结果(出现学号)

    5.2 GeneralStack接口的代码

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

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

    从本题来看,我们知道以前所定义IntegerStack接口,只能用于存放Integer类型的数据。然而对于栈来说,不管内部存放的是什么类型的数据,基本操作与元素的具体类型无关。这时就需要用到泛型,这样就不用因为类的不同而去重复写一样的代码。

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

    public static <T extends Comparable> T Max(List<T> list){
            T max= list.get(0);
            for(T e:list){
            	if(e.compareTo(max)>0)
            		max=e;
            }
            return max;
        }
    

    3. 使用码云管理Java代码

  • 相关阅读:
    中产阶级的特点
    数字化营销的7个增长点
    前沿显微成像技术专题——电压成像
    前沿显微成像技术专题之钙离子成像
    前沿显微成像技术专题
    前沿显微成像技术专题
    单分子荧光成像概述
    前沿显微成像技术专题之:光片荧光显微镜(3)
    前沿显微成像技术专题之:光片荧光显微镜(2)
    前沿显微成像技术专题之:光片荧光显微镜(1)
  • 原文地址:https://www.cnblogs.com/lch9/p/6703591.html
Copyright © 2011-2022 走看看