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

1. 本周学习总结

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

1.2 选做:收集你认为有用的代码片段

for (int i = 0; i < list.size(); i++) {
    if(list.get(i).equals(str)){
        list.remove(i);
        i--;                   //***
    }
}
Scanner sc=new Scanner(line);//建立扫描器
sc.close();

2. 书面作业

本次作业题集集合

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

1.1 实验总结

元素的删减中,在元素删减后,每个元素的对应位置发生了改变,例如从第一个元素开始删减,for(int i=0;i<size;i++),第1个元素对应的位置是0,第2个元素对应的是1,以此类推,循环开始,第一个元素被删除,此时循环从i=0变到i=1,但此时元素对应的位置已经发生了改变,出错,需要从最后一个元素开始删除避免这种错误。


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

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

while(str!="!!!!!")
{
    if(map.get(s) == null)
        map.put(s, 1);
      else 
        map.put(s, map.get(s)+ 1);
} 

ArrayList<Map.Entry<String, Integer>> arrayList = new ArrayList<Map.Entry<String, Integer>>(word.entrySet());
        
Collections.sort(arrayList, new Comparator<Map.Entry<String, Integer>>(){ 比较的方法 } });
for (Map.Entry<String, Integer> entry :arrayList) 
{
    if(i==10)
    {
        break;
    }
    System.out.println(entry.getKey()+"="+entry.getValue());
        i++;
}

2.2 实验总结

在判断一个元素是否要加入映射表时应该判断这个元素在映射表中是否存在,如果存在就在原来的基础上加1,否则加入这个元素然后数量为1,排序时次数相同以后再按照键值排序只需要在compare加一个if-else语句即可。


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

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

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

while(line not equals(!!!!!)){
    map.put(line,1);
    String [] arr=line.spilt(" ");
    for(i<arr.length){
        if(map.containsKey(arr[i]){
            add line;
        else
            add line and arr[i];
    }
    change array to list;
    if(!list.isEmpty)
        out(结果);

3.3 实验总结

第一个while()循环中,需要判断输入单词是否为空,为空,则map.put(s1, set);,不为空,则map.get(s1).add(row);;
需要考虑一行出现相同单词的情况,除此之外在查询时要先读入一行,然后再使用split方法后list后进行查询。


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

public Student screen()
    {
        if(this.id>10L&&this.name.equals("zhang")&&this.age>20&&this.gender==Gender.女&&this.joinsACM)
        {
            Student stu=new Student(this.id,this.name,this.age,this.gender,this.joinsACM);
            return stu;
        }
        
        else
            return null; 
    }

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,使其不出现异常。

public static List choose(List<Student> list){
	List<Student>newlist = list.stream()
			.filter(line -> line !=null)
			.filter(line -> line.getId()>10 && line.getName().equals("zhang") &&line.getGender().equals(Gender.girl)&& line.isJoninsACM())
			.collect(Collectors.tolist()):
		return newlist;
}


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

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

5.2 GeneralStack接口的代码

interface GeneralStack{
	Object push(Object item);            //如item为null,则不入栈直接返回null。
	Object pop();                 //出栈,如为空,则返回null.
	Object peek();                //获得栈顶元素,如为空,则返回null.
	public boolean empty();//如为空返回true
	public int size();     //返回栈中元素数量
}

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

如果没有使用泛型接口就要写很多不同类型的栈,代码就会很长。如果编写一个对任何引用类型的数据都适用的GeneralStack接口,不仅Integer, Double, Car可以使用,其他类型也可以。多一个类型,不需要再多写一个接口。
声明和建立对象时,使用角括号告知程序对象收集的类型,取回后不用再使用括号转化类型。

6.泛型方法

基础参考文件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)//max方法
       {                     
          T max = list.get(0);
          for (int i=1;i<list.size();i++) {
            if ( list.get(i).compareTo( max ) > 0 ){ 
                max=list.get(i);
          }
        }
          return max;
       }


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

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

3.1. 码云代码提交记录

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

3.2. PTA实验

函数(4-1),编程(5-3,5-4,5-5)

原文地址:https://www.cnblogs.com/GOB8023/p/6714215.html