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

1. 本周学习总结

2. 书面作业

List中指定元素的删除(题目4-1)
1.1 实验总结
每次删除时下标需要-1;原理如图

统计文字中的单词数量并按出现次数排序(题目5-3)
2.1 伪代码(简单写出大体步骤)
先将每个单词在map中建立键值的对应;
对每个单词进行统计出现的次数;
运用匿名内部类 Collections.sort(entryList,new Comparator<Map.Entry<String,Integer>>(){})
对次数进行排序;用for循环输出前十个对象。
2.2 实验总结
建立键值对应时单词输入如果空,则值置为1,否则值+1;
使用匿名内部类进行排序;
倒排索引(题目5-4)
3.1 截图你的提交结果(出现学号)

3.2 伪代码(简单写出大体步骤)
建立List<String>line=new ArrayList<String>();在line中存放句子;
建立 List<Map.Entry<String,Integer>> entryList=new ArrayList<>();
在map中建立对应;运用匿名内部类 Collections.sort(entryList,new Comparator<Map.Entry<String,Integer>>(){})对单词进行排序;再进行输出;
对于每次句子的查找可以把短语放入一个数组中分成每个单词;
如果存在一个单词不能找到就输出found 0 results,否则输出存在的句子。

3.3 实验总结
内容与上一题有点相似,在这里还加了一个查找的功能。
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中调用,然后输出结果。

public boolean student(){
	for (int i = 0; i < Student.size(); i++) {
            if(this.id>10L&&this.name.equals("zhang")&&this.age>20&&this.gender==Gender.女&&this.joinsACM)
            	return true;
            else
            	return false;
		}
	}

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

ArrayList<Student> List = (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,使其不出现异常。

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

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

5.2 GeneralStack接口的代码

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

5.3 结合本题,说明泛型有什么好处
泛型为类型与通用基类型 Object 之间进行强制转换来实现提供了针对这种限制的解决方案。
ArrayList 是一个使用起来非常方便的集合类,无需进行修改即可用来存储任何引用或值类型。
6.1 编写方法max,该方法可以返回List中所有元素的最大值。List中的元素必须实现Comparable接口。编写的max方法需使得String max = max(strList)可以运行成功,其中strList为List类型。也能使得Integer maxInt = max(intList);运行成功,其中intList为List类型。

import java.util.ArrayList;
import java.util.List;
public class Main {
    public static void main(String[] args) {
        List<String> strList = new ArrayList<>();
        List<Integer> intList = new ArrayList<>();
        strList.add("1");
        strList.add("2");
        strList.add("3");
        intList.add(4);
        intList.add(5);
        intList.add(6);
        String max = max(strList);
        Integer max1 = max(intList);
        System.out.println(max);
        System.out.println(max1);
        
    }
    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(max.compareTo(list.get(i))<0)
               max = list.get(i);
	}
        return max;
    }
}

3.1. 码云代码提交记录

posted @ 2017-04-15 17:07  童欢  阅读(106)  评论(0编辑  收藏  举报