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

1. 本章学习总结


2. 书面作业

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

while(list.contains(str))
    list.remove(str);


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

ArrayList<Map.Entry<String,Integer>> wordlist=new ArrayList<Map.Entry<String,Integer>>(mapword.entrySet());
    Collections.sort(wordlist,(o1,o2)->{
            if(o1.getValue()!=o2.getValue()){
                return o2.getValue()-o1.getValue();
            }
            else return o1.getKey().compareTo(o2.getKey());}
    );  

 2.2 实验总结

  • 使用HashMap,先将单词存入ArrayList<Map.Entry<String,Integer>> wordlist,在编写lambd表达式进行排序(跟之前编写name、age排序类似)。或者使用TreeMap因为其已经排序了,所以只要再对value进行排序。

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

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

            //建立索引
            map.put(linenum, str);//将每行句子存起来
			Scanner str1=new Scanner(str);
			while(str1.hasNext()){
				String word=str1.next();
				if(!mapword.containsKey(word)){//如果不存在word,则linenum添加到set中,并put到mapword
					TreeSet<Integer> set=new TreeSet<Integer>();
					set.add(linenum);
					mapword.put(word, set);
				}
				else{//存在,则在值集合中add新的一行
					mapword.get(word).add(linenum);
				}
			}
			linenum++;//下一行
             //查找,主要判断单词是否存在mapword中或者是否出现交集,如果没有则获取单词对应的行
             TreeSet<Integer> set=new TreeSet<Integer>();
			if (!mapword.containsKey(strarr[j])||set.retainAll(mapword.get(strarr[j]))) {
						break;
			} else {
						set=mapword.get(strarr[j]);
			}
	
			if(set.isEmpty()){
				System.out.println("found 0 results");
			}
			else{
				System.out.println(set);
				for(Integer o:set){
					System.out.println("line "+o+":"+map.get(o));//根据行,找到相应句子
				}
			}

 3.3 实验总结

  • 两个map,TreeMap<String,TreeSet<Integer>>Map<Integer,String>map,一个是单词到行的映射,一个是行到句子的映射。
  • 这题主要是查找比较难写,本来是跟建索引一样用扫描器的方式获取word,然后对其进行判断word是否存在mapword中或者是否出现交集,如果没有则获取单词对应的行。但是输出结果虽然一样,但是再PTA中提交显示是错误的,后面听同学的方法用split获取就可以了。

Scanner str1=new Scanner(str);
while(str1.hasNext()){
String word=str1.next();
if (!mapword.containsKey(word)||set.retainAll(mapword.get(word))) {
break;
} else {
set=mapword.get(word);
}
}


***

**Q4.Stream与Lambda编写一个Student类,属性为:**
```java
private Long id;
private String name;
private int age;
private Gender gender;//枚举类型
private boolean joinsACM; //是否参加过ACM比赛

 4.1 使用传统方法编写一个方法,将id>10,name为zhang, age>20, gender为女,参加过ACM比赛的学生筛选出来,放入新的集合。在main中调用,然后输出结果。

public static void main(String[] args) {
		 ArrayList<Student> list=new ArrayList<Student>();
		 list.add(new Student(11L,"zhang",21,Gender.female,true));
		 list.add(new Student(9L,"li",21,Gender.male,true));
		 list.add(new Student(22L,"wang",51,Gender.male,true));
		 list.add(new Student(45L,"zhang",25,Gender.female,true));
		 ArrayList<Student> list1 = new ArrayList<Student>();
		 for (Student student : list) {
		        if (student.getId() > 10L && student.getName().equals("zhang") && student.getAge() > 20 && student.getGender().equals(Gender.female) && student.isJoinsACM()) {
		             list1.add(student);
		             System.out.println(student);
		        } 
		 }
	    
	}
  • 截图如下:

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

ArrayList<Student> list1 = (ArrayList<Student>) list.Stream()
			        .filter(student -> (student.getId() > 10L && student.getName().equals("zhang")
			                && student.getAge() > 20 && 
			                student.getGender().equals(Gender.female)
			                && student.isJoinsACM()))
			        .collect(Collectors.toList());
		 System.out.println(list1);
  • 截图如下:

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

ArrayList<Student> list1 = (ArrayList<Student>) list.lStream()
        .filter(student -> student != null && (student.getId() > 10L && student.getName().equals("zhang")
                && student.getAge() > 20 && 
                student.getGender().equals(Gender.female)
                && student.isJoinsACM()))
        .collect(Collectors.toList());
  • 截图如下:

Q5.泛型类: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 结合本题,说明泛型有什么好处

  • 泛型允许指定集合中元素的类型,在之前的实验中我们只能建StringStack或者IntegerStack等已经确定类型的栈,在我们需要使用多种类型栈的时候就会出现大量代码,所以可以通过将栈泛型化,可以直接创建多个不同类型的栈,减少了代码量,同时减少了很多强制转化,避免更多出错的可能。

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

public class GenericMain {
	public static void main(String[] args) {
		List<String>strList=new ArrayList<String>();
        List<Integer>intList=new ArrayList<Integer>();
        strList.add("fds");strList.add("fda"); strList.add("f12");
        intList.add(33); intList.add(25); intList.add(62);
        String max = max(strList);
        Integer maxInt = max(intList);
        System.out.println("String max="+max+"  "+"Integer max="+maxInt);

	}
	public static <T extends Comparable<T>> T max(List<T> list){
		T max=list.get(0);
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).compareTo(max)>0){
				max=list.get(i);
			}
		}
		return max;
	}
}
  • 截图如下:

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

public class GenericMain {
	public static void main(String[] args) {
        List<StuUser>stuList=new ArrayList<StuUser>();
        stuList.add(new StuUser(12,"11"));
        stuList.add(new StuUser(32,"12"));
        stuList.add(new StuUser(33,"13"));
        System.out.println("max="+max1(stuList));
	}
	public static <StuUser extends Comparable<T>> StuUser max1(List<StuUser> list){                     
		StuUser max=list.get(0);
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).compareTo(max)>0){
				max=list.get(i);
			}
		}
		return max;
	}   
}
  • 截图如下:

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

3.1本周Commit历史截图

3.2PTA实验

posted @ 2017-04-15 19:03  _Another  阅读(164)  评论(0编辑  收藏  举报