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

1. 本周学习总结

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

2. 书面作业

1.本次作业题集集合
public static List convertStringToList(String line)
{
List str=new ArrayList();
for(int i=0;i<line.length();i++)
{
if(line.charAt(i)==' ')continue;
else str.add(line.charAt(i)+"");
}

    return str;
}

public static void remove(List<String> list,String str)
{
    for(int i=0;i<list.size();i++)
    {
        if(list.get(i).equals(str))
        {
            list.remove(i);
            i--;
        }

    }
}

1.1 实验总结

iterator.remove()从迭代器指向的 collection 中移除迭代器返回的最后一个元素。
删除List中指定元素,有多种方法,在使用迭代器的时候发生错误就使用了其它方法。

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

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

 Map<String, Integer> map = new TreeMap<String, Integer>();
    while (input.hasNext()) {
        String word = input.next();
        if (word.equals("!!!!!"))
            break;
        else if (!map.containsKey(word))
            map.put(word, 1);
        else {
            int n = (int) map.get(word);
            map.put(word, n + 1);
        }
    }

2.2 实验总结

Map<String,Integer> map = new HashMap<String,Integer>():创建键对象和值对象的映射。

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

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

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

input each line
input want to search's key or line
Search each Line
then search each key
if key or line is empty or line uncontain key
    found 0 results

3.3 实验总结

用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);        
}

结果:
Student {id=11, name='zhang', age=21, gender=female, joinsAcm=true}
Student {id=12, name='zhang', age=25, gender=female, joinsAcm=true}

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

ArrayList<Student> List2=(ArrayList<Student>) List.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 pop();                 
    public T peek();                
    public boolean empty();
    public int size();     
}

5.3 结合本题,说明泛型有什么好处
使用了泛型可以提高代码复用率。

6.泛型方法

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

6.2 选做:现有User类,其子类为StuUser,且均实现了Comparable接口。编写方法max1,基本功能同6.1,并使得max1(stuList);可以运行成功,其中stuList为List类型。

6.3 选做:编写int myCompare(T o1, T o2, Comparator c)方法,该方法可以比较User对象及其子对象,传入的比较器c既可以是Comparator,也可以是Comparator。注意:该方法声明未写全,请自行补全。

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

3.1. 码云代码提交记录

3.2. PTA实验

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

原文地址:https://www.cnblogs.com/set-L-Ann/p/6715538.html