201521123100《Java程序设计》第八周学习总结

---恢复内容开始---

#1. 本周学习总结

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

2. 书面作业

本次作业题集集合

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

1.1 实验总结
A:

掌握了泛型定义 List List=new ArrayList();以及如何正确删除List中的元素

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

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

for(i=0;str!="!!!!!";i++){
  if(map.get(x)==null)    map.put(x, 1); 
  else map.put(x, map.get(x)+1);
}

List<Entry<String,Integer>> list =new ArrayList<Entry<String,Integer>>(map.entrySet());

Collections.sort()
for(){
  if (o1.getValue() != o2.getValue()) return o2.getValue() - o1.getValue();
  else return o1.getKey().compareTo(o2.getKey());
}

2.2 实验总结
A:

题目中用到很多Map的方法,这时就体现出了知识点掌握不熟的严重弊端,什么时候用什么方法真的很头痛,但最终通过看PPT以及书本得以龟速的解决;觉得比较重要的也是我独自写不出来的是Arraylist实现排序

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

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

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

TreeMap<String,Set<Integer>>  words = new TreeMap<String,Set<Integer>>();

for(){
  for (String word : words) {
    if (!index.containsKey(word)) {
            subIndex.add(line);
            index.put(word, subIndex);
        } 
    else {
      subIndex = index.get(word);
}
}

3.3 实验总结
A:主要注意区分单词查询结果的不同情况,分别写出对应的实现方法即可

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

for(int i=0;i<6;i++)
    {
        k=list.get(i);
        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的函数,并测试。
A:

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.1
4.3 构建测试集合的时候,除了正常的Student对象,再往集合中添加一些null,然后重新改写4.2,使其不出现异常。
A:

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

加个判断,判断student不为空

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

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

5.2 GeneralStack接口的代码
A:

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

5.3 结合本题,说明泛型有什么好处
A:使用泛型则不需要考虑参数的类型,可以在一个集合里面存放多种类型的数据

6.泛型方法

基础参考文件GenericMain,在此文件上进行修改。
6.1 编写方法max,该方法可以返回List中所有元素的最大值。List中的元素必须实现Comparable接口。编写的max方法需使得String max = max(strList)可以运行成功,其中strList为List类型。也能使得Integer maxInt = max(intList);运行成功,其中intList为List类型。
A:

public class GenericMain {
   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;
   }
    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("String max="+max+"  "+"Integer max="+maxInt);
    }
}

测试结果

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

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

3.1. 码云代码提交记录

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

---恢复内容结束---

posted @ 2017-04-15 20:48  焦糖卜丁  阅读(136)  评论(0编辑  收藏  举报