201521123005 《java程序设计》 第八周学习总结

1. 本周学习总结##

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

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

2. 书面作业##

本次作业题集集合

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

Scanner sc=new Scanner(line);//建立扫描器
sc.close();
做了这道题,发现有很多快速的方法实现你想要的,而不需要自己去写,自己写还不一定更好。

1.2截图提交结果

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

while(has next input)
    if(next word doesn's inside map)
        put word into map
    else
        get the value in map to add 1
sort the map
output the result

2.2 实验总结

一开始我判断map中是否有str用的是if (map.containsKey(str))结果运行时间过长。后来想直接找不不就更快就用if(map.get(s) == null)。提交一下就过了。这题难点是在用ArrayList来实现排序。

2.3截图提交结果

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

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

create a map;
read all words in line;
create a set and add the line number and nonredundant words into the set;
ergodic(遍历) all words to search keywords;
if(the search result in map is unEmpty)
    print(the set);
else
print(found 0 results);

3.3 实验总结

我觉得这道题输出部分考虑的要比建倒排索引多,我是把它分成四种情况:查找的单词只有一个但查找不到,查找的单词只有一个但查找到,查找的单词有多个但找到有公共的行,查找的单词有多个但找不到即
if(findword.length==1}
{
    if(find)   输出找到的行;
    else 输出找不到; 

}
else
{
    if(find)   输出找到的行;
    else 输出找不到; 

}

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

import java.util.ArrayList;
import java.util.Set;
enum Gender {  
	  female, male
	} 
class Student
{
	private Long id;
	private String name;
	private int age;
	private Gender gender;//枚举类型
	private boolean joinsACM; //是否参加过ACM比赛

	
	public Student(Long id, String name, int age, Gender gender, boolean joinsACM) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.gender = gender;
		this.joinsACM = joinsACM;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + ", joinsACM=" + joinsACM
				+ "]";
	}

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

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<Student> list=new ArrayList<Student>();
		Student e1=new Student(11L,"zhang",21,Gender.female,true);
		Student e2=new Student(9L,"zhang",21,Gender.female,true);
		list.add(e1);
		list.add(e2);
		for (Student student : list) {
			System.out.println(student.find());
		}
	}

}

运行结果:

订正:
public class Main {
	public static Student find(Student student) {
		// TODO Auto-generated method stub
		
		if(student.getId()>10L&&student.getName().equals("zhang")&&student.getAge()>20&&student.getGender()==Gender.female&&student.isJoinsACM())
		{
			Student e=new Student(student.getId(),student.getName(),student.getAge(),student.getGender(),student.isJoinsACM());
			return e;
		}
		
		else
			return null;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<Student> list=new ArrayList<Student>();
		Student e1=new Student(11L,"zhang",21,Gender.female,true);
		Student e2=new Student(9L,"zhang",21,Gender.female,true);
		list.add(e1);
		list.add(e2);
		for (Student student : list) {
			System.out.println(find(student));
		}
	}
}

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

ArrayList<Student> arrayList2 = (ArrayList<Student>) arrayList.parallelStream()
        .filter(student -> student != null && (student.getId() > 10L && student.getName().equals("zhang")
                && student.getAge() > 20 && 
                student.getGender().equals(Gender.female)
                && student.isJoinsACM()))
        .collect(Collectors.toList());


加个判断,判断student不为空。
Q5泛型类:GeneralStack(题目5-5)
5.1 截图你的提交结果(出现学号)

5.2 GeneralStack接口的代码

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

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

pta题目5-1所定义IntegerStack接口,只能用于存放Integer类型的数据。然而对于栈来说,不管内部存放的是什么类型的数据,基本操作与元素的具体类型无关。就拿5-5题目来说,对于Integer, Double, Car三个引用类型的栈,它们的基本操作都是push(item),pop(),peek(),public boolean empty(),public int size()。如果没有使用泛型接口就要写三个不同类型的栈,代码就会很长。如果编写一个对任何引用类型的数据都适用的GeneralStack接口,不仅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 GenericMain {
   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>();
		List<Integer>intList=new ArrayList<Integer>();
		strList.add("a");
		strList.add("b");
		strList.add("c");
		intList.add(1);
		intList.add(2);
		intList.add(3);
		String max = max(strList);
		Integer maxInt = max(intList);
		System.out.println("String max="+max+"  "+"Integer max="+maxInt);
	}
}

运行结果:

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

   public static <StuUserComparator extends Comparable<StuUser>> StuUser max1(List<StuUser> stuList)
   {                     
	   StuUser max = stuList.get(0);
      for (StuUser t : stuList) {
		if ( t.compareTo( max ) > 0 ){
         max = t; 
      }
	}
      return max; // 返回最大对象
   }
	public static void main(String[] args) {
		List<String>strList=new ArrayList<String>();
		List<Integer>intList=new ArrayList<Integer>();
		List<StuUser>stuList=new ArrayList<StuUser>();
		StuUser e1=new StuUser(12,"66493");
		stuList.add(e1);
		StuUser e2=new StuUser(23,"33493");
		stuList.add(e2);
		StuUser e3=new StuUser(25,"26493");
		stuList.add(e3);
		System.out.println(max1(stuList));

	}
}
	public static void main(String[] args) {
		List<String>strList=new ArrayList<String>();
		List<Integer>intList=new ArrayList<Integer>();
		List<StuUser>stuList=new ArrayList<StuUser>();
		StuUser e1=new StuUser(12,"66493");
		stuList.add(e1);
		StuUser e2=new StuUser(23,"33493");
		stuList.add(e2);
		StuUser e3=new StuUser(25,"26493");
		stuList.add(e3);
		System.out.println(max1(stuList));

	}
}

运行结果:

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

Q7选做:逆向最大匹配分词算法
集合实验文件中的第07次实验(集合).doc文件,里面的题目6.
7.1 写出伪代码
7.2 实验总结

Q8选做:JavaFX入门
完成其中的作业1、作业2。内有代码,可在其上进行适当的改造。建议按照里面的教程,从头到尾自己搭建。

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

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

3.1. 码云代码提交记录

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

3.2. PTA实验

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

原文地址:https://www.cnblogs.com/yangxy/p/6698642.html