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

1. 本章学习总结

2. 书面作业

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

使用iterator迭代器的remove()函数来移除元素,能够从最后一个元素来移除,可以避免从第一个开始移除元素导致元素下标改变。

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

map=new HashMap()
map.put(key,value)
if(map.containsKey(新加入的元素))
  value++
else 
  value=1
if(新加入的元素.equals("!!!!!"))
  break

2.2 实验总结

Map可以用keySet()方法返回键的Set,values()方法来返回值的Collection,或者用entrySet()方法来返回键值对的Set。

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

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

TreeMap map = new TreeMap
map.put(新的元素,row)
row++
if(新加入的元素.equals("!!!!!"))
      break    
遍历TreeMap,建立索引
if(map.containsKey(key))
    return 所有key对应的value值
else 
    found 0 results

3.3 实验总结

主要用到了map,set和ArrayList,使用TreeMap<String, Set<Integer>> line = new TreeMap<String, Set<Integer>>(); 来完成查找功能。

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 class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<Student> list=new ArrayList<Student>();
        Student she1=new Student(11,"zhang",21,Gender.female,true);
        Student she2=new Student(9,"zhang",21,Gender.female,true);
        Student she3=new Student(9,"zhang",19,Gender.female,true);
        Student she4=new Student(9,"zhang",21,Gender.male,true);
        list.add(she1);
        list.add(she2);
        list.add(she3);
        list.add(she4);
        for (Student student : list) {
            System.out.println(student.find());
        }
    }
}

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

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

再加上student != null就可以了。

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

5.2 GeneralStack接口的代码

interface GeneralStack<E>
{
    E push(E item);
    E pop();
    E 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类型。

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

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

3.1本周Commit历史截图

posted on 2017-04-15 20:23  一叶笙箫  阅读(126)  评论(0编辑  收藏  举报

导航