201521123027 <java程序设计>第八周学习总结

1.本周学习总结

1.1思维导图

2.书面作业

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

总结:判断List中是否存在指定元素,需要用到equals方法,若存在就用remove进行对元素的删除。remove方法就是删除当前下标为i的元素,然后将该元素的所有元素向前移一位,i自减。

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

(1)创建HashMap对象Map<String,Integer> map = new HashMap<String,Integer>();
(2)统计字符出现次数,若为空,则为1;若不为空,则n+1;
(3)转化,Map转化为List;
(4)Collections.sort比较排序;
(5)输出十个;

2.2 实验总结

总结:首先此题要使用到HashMap,将字符串中的字符存入且没有重复。
(1)判断输入的字符是否为空,若为空,则赋值value=1;若不为空,则n+1;这里要用到Map中的get()、put()方法;
(2)List<Map.Entry<String, Integer>> entryList=new ArrayList();将Map转化为List,因为Collections.sort不能用在Map中;
(3)另外还需要使用到 Comparator中的Compare对字符进行按照value从大到小排序;

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

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

(1)构建TreeMap,用于存放单词和单词所在行数;
(2)split方法,将单词存在数组中;
(3)将数组与List<String> line比较,找出每个单词所在行数,存入Map中;
(4)遍历找关键字,若找到,则输出所在行数和所在行的一整句;若找不到,输出found 0 results。

3.3 实验总结

(1)Map、Set、List的使用;
(2)调用split方法将一句话根据空格分开存入数组中,在与原句进行查找所在行数;
我感觉这一题是PTA中最难的一题,若让我独立完成,我认为我做不出来,这一题是参考了同学的代码以及同学的讲解,但是还没有完完全全掌握,还需要再琢磨、修改。

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

list.add(new Student(15L,"cheng",19,getGender(1),true));
list.add(new Student(20L,"li",20,getGender(1),false));
list.add(new Student(15L,"zhang",21,getGender(2),true));
list.add(new Student(2L,"cheng",25,getGender(2),false));
list.add(new Student(20L,"li",18,getGender(1),false));

结果截图:

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

list2=list.stream().filter(s->10<s.getId()).collect(Collectors.toList());
list2=list2.stream().filter(s->"zhang".equals(s.getName())).collect(Collectors.toList());
list2=list2.stream().filter(s->20<s.getAge()).collect(Collectors.toList());
list2=list2.stream().filter(s->getGender(2).equals(s.getGender())).collect(Collectors.toList());
list2=list2.stream().filter(s->s.isJoinsACM()==true).collect(Collectors.toList());

结果截图:

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

list.add(null);

功能实现,增加排除null的情况:

list=list.stream().filter(s->s!=null).collect(Collectors.toList());

结果截图:

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

5.2 GeneralStack接口的代码

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

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

好处:使用泛型可以不必直接决定类的类型,此题中对于Integer, Double, Car三个引用类型的栈,若不使用泛型接口则需要写三个不同类型的栈,若直接使用一个泛型接口,让Integer, Double, Car三个引用类型的栈都适用,则可以少写很多代码。

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

public class 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("e");
        intList.add(2);
        intList.add(4);
        intList.add(6);
        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:33  clclcl  阅读(181)  评论(1编辑  收藏  举报

导航