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

1. 本周学习总结

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

2. 书面作业

本次作业题集集合

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

1.1 实验总结

Scanner scanner = new Scanner(Line);

我们可以建立一个scanner扫描器,通过next()方法就会自动将String根据空格断开。

1.2截图提交结果

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

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

while(!str.equals("!!!!!"))
		{
			if(word.get(str) == null)
				word.put(str, 1);
			else
			    word.put(str, word.get(str)+ 1);       
			str=in.next();
		}	
for (Map.Entry<String, Integer> entry :arrayList) 
		{
			if(i==10)
			{
				break;
			}
			System.out.println(entry.getKey()+"="+entry.getValue());
			i++;
		}

2.2 实验总结


TreeMap不支持按照值来维护内部数据排列顺序,我们可以用用ArrayList来实现排序。

Q3:倒排索引(题目5-4)

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

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

  if (words=sc.nextLine())
           Set<Integer> subIndex = new TreeSet<Integer>();
       for (Map.Entry<String, Set<Integer>> e : index.entrySet()) { 
        if (words == 0)
            System.out.println("found 0 results");
        else {
            System.out.println(line);

3.3 实验总结

这道题主要是使用treemap来实现,如果找到,输出行集与行集内每一行的内容,如果没找到输出found 0 results,难度较大,参照很多其他同学的代码才写出来。

Q4: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 Student find()
    {
        if(this.id>10L&&this.name.equals("zhang")&&this.age>20&&this.gender==Gender.female&&this.joinsACM)
        {
            Student e=new Student(this.id,this.name,this.age,this.gender,this.joinsACM);
            return e;
        }
        
        else
            return null;
        
    }

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

List<Student> result=student.stream().filter(stu-> stu.getId() > 10L && stu.getName().equals("zhang")
            && stu.getAge() > 20 &&
            stu.getGender().equals(Gender.FEMALE)
            && stu.isJoinsAcm())
            .collect(Collectors.toList());

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

List<Student> result=student.stream().filter(stu-> stu.getId() > 10L && stu.getName().equals("zhang")中加上对null的判断,即List<Student> result=student.stream().filter(stu-> stu!=null&&stu.getId() > 10L &&stu.getName().equals("zhang")

Q5:泛型类: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 结合本题,说明泛型有什么好处

泛型的本质是参数化类型,也就是说所操作的数据类型被指定成一个参数。所以,不需要考虑不同数据的不同实现,可以提高代码的效率。

Q6:泛型方法

基础参考文件GenericMain,在此文件上进行修改。

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

3. 码云上代码提交记录及PTA实验总结

题目集:jmu-Java-05-集合

3.1. 码云代码提交记录

3.2. PTA实验

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

posted @ 2017-04-14 21:52  张晨晨  阅读(157)  评论(1编辑  收藏  举报