1. 本周学习总结

2. 书面作业

本次作业题集集合

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

1.1 实验总结

1.删除元素的时候从最后一个元素开始,避免删除元素后位置发生变化而导致有些元素没有删除;2.通过equals方法以及list.remove的方法连用实现list中移除掉以与str内容相同的元素的函数。

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

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

1.建立Map(key,value),key类型String,value类型Integer
2.输入文本, str=in.next()
3.比较,若是出现过key,value+1,否则加入单词,次数=1
4.将map对象转化为list对象;
5.用Collections.sort对list进行排序,输出; 

2.2 实验总结

1.map.entrySet()方法:用于获得存储在Map中所有映射的Set集合;2.泛型的使用

3.倒排索引(题目5-4)

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

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

(1)建立TreeMap<String, Set<Integer>>索引
(2)用while判断,存放数据
(3)whlie()循环解决匹配查找问题

3.3 实验总结

1.TreeSet以及TreeMap的综合使用

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

ArrayList<Student> list =new ArrayList<Student>();
for(Student a:s){
    if(a.getId()>10&&a.getName().equals("zhang")&&a.getAge()>20&&a.getGender().equals(Gender.girl)&&a.isJoinsACM()){
    list.add(a);        
}

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

  ArrayList<Student> arrayList2 = (ArrayList<Student>) arrayList.parallelStream()
  .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,使其不出现异常。

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

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

5.2 GeneralStack接口的代码

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

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

1.提高了JAVA程序的类型安全,在编译时期就避免了程序本可能在程序运行时期发生的错误;2.消除了强制类型转换;3.泛型能够代替Object类型的参数和变量的使用,增强代码的可读性

6.泛型方法

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

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

public class GenericMain {

    public static void main(String[] args) {
        List<String> strList = new ArrayList<String>();
        List<Integer> intList = new ArrayList<Integer>();
        strList.add("a");
        strList.add("b");
        strList.add("c");
        intList.add(1);
        intList.add(2);
        intList.add(3);
        String max = max(strList);
        Integer maxInt = max(intList);
        System.out.println("max = " + max);
        System.out.println("maxInt =" + maxInt);
    }
    
    public static <T extends Comparable<T>> T max(List<T> list) {
        T max = list.get(0);
        for(T i : list){
            if(i.compareTo(max)>0)
                max = i;
        }
        return max;
    }
}

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

3.1. 码云代码提交记录

posted on 2017-04-15 20:29  网一16号  阅读(121)  评论(1编辑  收藏  举报