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

    1. 本周学习总结

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

    1.2 选做:收集你认为有用的代码片段

    答:主要以泛型为主

    //简单的泛型类的定义,T为类型参数
    public class Pair<T> {
     public Pair(T first, T second) {
           this.first = first;  
          this.second = second; 
     }
     public T getFirst() { return first; }
     public void setFirst(T newValue) { first = newValue; }
     private T first; 
     private T second;
     ....
    }
    
    
    //在普通的类中也可以定义一个泛型方法
    class ArrayAlg{
        public static <T> T getMiddle(T[] arr){
            return arr[arr.length/2];
        }
    }
    
    //使用
    Integer[] ints = {0,1,2,3,4,5,6,7,8,9};
    Integer x = ArrayAlg.getMiddle(ints);
    
    //获得数组的最小值
    public static <T extends Comparable> Pair<T> minmax(T[] a){
         ......
    }
    `T extends Comparable`表示T是绑定类型(Comparable)的子类型,可有多个绑定类型:`T extends Comparable & Serializable`
    
    //Pair<T>的原始类型(raw type)如下
    public class Pair{
        public Pair(Object first, Object second){
         .....
        }
    }
    
    
    //这里的Pair声明,它的类型参数是Employee或者其子类,如Pair<Manager>或Pair< Employee>
    Pair<? extends Employee> x = new Pair<Manager>(); //合法
    
    
    //举例正确的声明
    List list = new ArrayList<String>();
    List<String> list = new ArrayList<>();
    List<? extends Object> list = new ArrayList<String>();
    
    //一般来说,带有子类型限定的通配符可以从泛型对象读取,比如,源代码中的getFirst方法
    printBuddies(Pair<? extends Employee> p){
        Employee first = p.getFirst(); //从p中读
        System.out.println(first.getName());
    } 
    //p中存放的一定是Employee或其子类对象,这就保证该对象已经具有Employee拥有的方法与属性,如getName()
    
    //无限定通配符,不知道具体类型
    boolean  hasNulls(Pair<?> p){
        return p.getFirst() == null || p.getSecond() == null;
     }
    

    2. 书面作业:本次作业题集集合

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

    1.1 实验总结

    答:
    1.以空格为分隔符,将line转换为List,最主要的代码就是list.add(in.next());调用next方法,将空格之间有意义的字符串提取出来,add到list中。

    public static List<String> convertStringToList(String line){
    	List<String> list=new ArrayList<String>();
    	Scanner in=new Scanner(line);
    	while(in.hasNext()){
                list.add(in.next());
    	}
    	in.close();
    	return list;
    }
    

    2.这边如果没有搞清楚remove的方法就很容易出错,如果list.remove(i);之后没有i--;,那么list中的个别元素会被漏掉而没有办法将str全部删除干净;
    至于remove的方法,举个例子,假设list中依次放入0,1,2,3,4,5,即元素0地址为0,1地址为1,类推,当我们要删除小于散的元素时,就需要从0开始比较,此时i=0,list.get(i)=0,小于3,remove;此时i=1,而当元素0被删除之后,地址为0的元素变成1,地址位1的元素变成2,remove第i个元素后,2就被删掉了,与此同时,本该被删除的元素1却被忽略过去,那么结果就会出错,因此必须进行i--

    public static void remove(List<String> list, String str){
    	for(int i=0;i<list.size();i++){
    		if(list.get(i).equals(str)){
    			list.remove(i);
    			i--;
    		}
    	}
    }
    

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

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

    Map<String,Integer>map=new HashMap<String,Integer>();//定义HashMap存放单词(key:String)与出现次数(value:Integer)
    String str=in.next();//str寄存最近输入的单词
        while(!str.equals("!!!!!")){//循环结束标志判断
        if(map.get(str)==null)map.put(str, 1);//Map中没有str,加入,value=1
    else map.put(str, map.get(str)+1);//Map中有str,替换,value+1
    str=in.next();//循环
    }
    //以下是重难点
    ArrayList<Map.Entry<String,Integer>>arr=new ArrayList<Map.Entry<String,Integer>>(map.entrySet());//用ArrayList保存Map中的键值对,以便排序
    Collections.sort(arr, new Comparator<Map.Entry<String,Integer>>(){//调用sort方法,进行定义
    public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
    if(o2.getValue()-o1.getValue()!=0)
    return o2.getValue()-o1.getValue();//次数按照降序排序
    return o1.getKey().compareTo(o2.getKey());//如果次数相同,则按照键值的字母升序排序
    }			
    });
    }
    }
    //最后输出
    

    2.2 实验总结

    答:5-3题目与5-2差不多,流程相似,主要区别也是难点在于排序部分,上周做的时候单纯的以为用TreeMap就能直接排序出来,然而结果并没有怎么简单,5-3的排序部分需要用到ArrayList和Collection.sort,需要注意的一点是重新定义排序的时候,升序降序要注意,具体的一些总结包括在上一题的注释当中。

    倒排索引(题目5-4)

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

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

    答:

    //定义map(存放单词+出现该单词的行号),set(单词出现的所有行号)和ArrayList(每一行字符串)
    Map<String, Set<Integer>> map = new TreeMap<String, Set<Integer>>();
    ArrayList<String> lines = new ArrayList<String>();
    Set<Integer> index = new TreeSet<Integer>();
    lines.add(strline1);
    //将每一行根据空格把单词提取出来放到字符串数组中
    String[] words = strline1.split(" ");
    //保存行号和字符串的过程
    for (String word : words)
    if (!map.containsKey(word)) {
       if (!map.containsKey(word)) {
    		index.add(row);
    		map.put(word, index);
    	} else {
    		index = map.get(word);
    		if (!index.contains(row)) {
    			index.add(row);
    			map.put(word, index);
    		}
    	}
    //输出所有单词及出现的所有行号
    for (Map.Entry<String, Set<Integer>> e : map.entrySet()) {
    	System.out.println(e.getKey() + "=" + e.getValue());
    }
    

    下面查询比较复杂:
    将查询的每一行字符串用split把单词分开
    根据map和set,若查找到所有单词以及它们所出现的行号
    再通过map2中行号的出现次数来确定输出
    若没有找到所有单词或没有共同行号
    输出found 0 results

    3.3 实验总结

    答:5-4主要用到map,set和ArrayList,不得不说真的好难,饶了好久没绕出来,题目信息少,不知道怎么入手,只能用最笨的方法,开好多个集合来保存信息,不过很好的一点是TreeMap和TreeSet能自己排序;最难的是在查找那一部分,要分许多情况(用了好多if...else...)

    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中调用,然后输出结果。

    答:eclipse运行截图如下

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

    答;stream(), filter(), collect()的用法还不太了解,参考网上的一些介绍之后才开始做的。
    Java8 Stream - use filter collect foreach in single line
    Java8新特性——StreamAPI(一)

    ArrayList<Student> list2 = new ArrayList<Student> list.stream().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,使其不出现异常。

    答:在条件内排除student为null

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

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

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

    5.2 GeneralStack接口的代码

    答:GeneralStack接口的代码如下

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

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

    答:

    • 不需要使用有风险的强制类型转换
    • 泛型允许指定集合中元素的类型(T->Integer,Double,Car),即使是自定义的类型也适用,比如Car
    • 错误在编译阶段就能发现,而不用等到运行时才发现出错,跟强制转换有关,对比集合跟加有优势
    • 例如
    List<String> strList = new ArrayList<String>();
    strList.add("I am a string");
    strList.add(new Integer(1)); //编译时就报错,避免运行时才报错
    

    泛型方法:基础参考文件GenericMain,在此文件上进行修改。

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

    答:eclipse测试结果截图

    6.2 选做:现有User类,其子类为StuUser,且均实现了Comparable接口。编写方法max1,基本功能同6.1,并使得max1(stuList);可以运行成功,其中stuList为List类型。

    答:eclipse测试结果截图

    3. 码云上代码提交记录及PTA实验总结----题目集:jmu-Java-05-集合

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

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

  • 相关阅读:
    CodeForces 347B Fixed Points (水题)
    CodeForces 347A Difference Row (水题)
    CodeForces 346A Alice and Bob (数学最大公约数)
    CodeForces 474C Captain Marmot (数学,旋转,暴力)
    CodeForces 474B Worms (水题,二分)
    CodeForces 474A Keyboard (水题)
    压力测试学习(一)
    算法学习(一)五个常用算法概念了解
    C#语言规范
    异常System.Threading.Thread.AbortInternal
  • 原文地址:https://www.cnblogs.com/dabaolyr/p/6713279.html
Copyright © 2011-2022 走看看