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

1. 本周学习总结

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

2. 书面作业

本次作业题集集合
1.List中指定元素的删除(题目4-1)
1.1 实验总结

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

答:实验中运用到了equals方法和remove方法。当使用了scanner方法时,要使用sc.close()将其关闭。

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

答:2.1

Map<String, Integer>map = new HashMap<String.Integer>();//用hashmap进行键值对的放入
获取输入的英文单词
while(sc,hasNext())
{
if(get(str)==null)//如果为空
put(key,value);//就放入一对键值对
else
put(key,value+1);
}
创造一个ArrayList<Map.Entry<String, Integer>> arrayList
和一个collection接口的比较方法,来比较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
用hashmap的方法,hashmap用于储存键值对,用get()put()方法来获取和放入键值对。用列表来实现排序。collection接口那里有点不会。

3.倒排索引(题目5-4)
3.1 截图你的提交结果(出现学号)
3.2 伪代码(简单写出大体步骤)
3.3 实验总结

答:
3.1

3.2

Map<String, Set<Integer>>index = new TreeMap<String, Set<Integer>>();
ArrayList<String> wordlines=new ArrayList <String>();
输入的时候
if(!key.word)
{
    put(key,value)
}
else//有key.word
{
    get(key.word)
}
//关键句同理
输出的时候
if(findword.length==1}
{
    if(find key.word)
       System.out.println();
    else 
      found 0 results; 

}
else
{
    if(find key.line)
       System.out.println();
    else 
       found 0 results; 

}

3.3
输入输出的时候要考虑很多种可能,尤其是输出的时候,找不到时返回null。

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中调用,然后输出结果。
4.2 使用java8中的stream(), filter(), collect()编写功能同4.1的函数,并测试。
4.3 构建测试集合的时候,除了正常的Student对象,再往集合中添加一些null,然后重新改写4.2,使其不出现异常。

答:
4.1

    public Student find()
    {
        if(this.id>10L&&this.name.equals("zhang")&&this.age>20&&this.gender==Gender.female&&this.joinsACM)
        {
            Student n=new Student(this.id,this.name,this.age,this.gender,this.joinsACM);
            return n;
        }
        
        else
            return null;
        
    }
//主函数

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<Student> list=new ArrayList<Student>();
        Student n1=new Student(100L,"zhang",15,Gender.male,true);
        Student n2=new Student(18L,"zhang",22,Gender.female,true);
        Student n3=new Student(18L,"xu",22,Gender.female,true);
        list.add(n1);
        list.add(n2);
        list.add(n3);
        for (Student student : list) {
            System.out.println(student.find());
        }

4.2

ArrayList<a> arrayList2 = (ArrayList<a>) arrayList.parallelStream().filter(a->10<a.getId()&&a->"zhang".equals(a.getName())&&a->20<a.getAge()&&a->a.isJoinsACM()==true).collect(Collectors.toList());

4.3

ArrayList<a> arrayList2 = (ArrayList<a>) arrayList.parallelStream().filter(a->a!=null&&a->10<a.getId()&&a->"zhang".equals(a.getName())&&a->20<a.getAge()&&a->a.isJoinsACM()==true).collect(Collectors.toList());
    	

5.泛型类:GeneralStack(题目5-5)
5.1 截图你的提交结果(出现学号)
5.2 GeneralStack接口的代码
5.3 结合本题,说明泛型有什么好处

答:
5.1

5.2

interface GeneralStack<T> {
    T push(T item);          
    T pop();    
    T peek();     //获取栈顶元素           
    public boolean empty();
    public int size(); 
}

5.3
使用泛型就可以不用定义多个类,GeneralStack接口对任何引用类型的数据都适用,泛型消除强制类型转换,错误在编译阶段就能发现不用等到运行的时候,这样可以简化代码同时减少了出错机会。

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

import java.util.ArrayList;
import java.util.List;

public class Main {
	   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>();
	        strList.add("q");
	        strList.add("z");
	        strList.add("w");
	        String max = max(strList);
	        System.out.println("String max="+max);
	    }
	}

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

题目集:jmu-Java-05-集合
3.1. 码云代码提交记录
在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图

posted @ 2017-04-15 10:04  叫我小天才  阅读(265)  评论(0编辑  收藏  举报