zoukankan      html  css  js  c++  java
  • 201521123076 《Java程序设计》第8周学习总结

    1. 本周学习总结

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

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

    2. 书面作业

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

    1.1 实验总结
    List<String> list = new ArrayList<String>();
    String[] str = nextLine.split(" ");
    	for (int j = 0; j < str.length; j++) {
    		if (!str[j].equals("")) {
    			list.add(str[j]);
    		}
    	}
    
    for (int i = 0; i < list.size(); i++) {
    			if(list.get(i).equals(word)){
    				list.remove(i);
    				 i--;
    			}
    

    A: 先split(" ")按空格截取,之后把不是空的元素加入list就解决了输入有多个空格的问题。要注意一下删除元素后i值与栈中对应位置的关系。

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

    2.1 伪代码(简单写出大体步骤)
    map = new HashMap<String,Integer>()
    while s = in.next() != "!!!!!"
    	if map.get
    		map.put(s,1)
    	else
    		map.put(s,map.get(s)+1)
    
    list = new ArrayList<Entry<String,Integer>>(map.entrySet());
    Collections.sort(list,new Comparator<Map.Entry<String, Integer>>() {
    	compare getValue
    	if equals 
    	compare Key
    });
    print(list)
    
    2.2 实验总结

    A:本题关键点就在于如何把单词put to map。先要判断是否map内是否存在此单词,若存在,就把其Value值加1,否则就加入此单词,value值设置为1。还有就是掌握Map.Entry的使用。

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

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

    3.2 伪代码(简单写出大体步骤)
    words = new TreeMap<String,ArrayList<Integer>>();  
    while(has nextLine)
    	while(has nextword)
    		if(words.get(this word) is null)
    			words.put(this word,value = this line)
    		else
    			if( words.get(this word) conains this line)
    				continue;
    			else
    				words.put(this.word,value = value add this line)
    	line++;  
    

    	if(words.get(this word) is null)
    		print("found 0 results")
    	else 
    		print(value and each line's content)
    
    3.3 实验总结

    A:用next()添加每个单词。

    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的函数,并测试。
    public static void  streamTest(List<Student> stuList){
    	List<Student> tempList =  stuList.parallelStream().filter(stu -> (stu.getAge() > 20 ||
    			stu.getGender().equals(Gender.female) || stu.getId() > 10 || stu.getName().equals("zhang") ||
    			stu.isJoinsACM())).collect(Collectors.toList());
    	for(Student i : tempList)
    	System.out.println(i);
    
    }  
    输出结果同4.1
    
    4.3 构建测试集合的时候,除了正常的Student对象,再往集合中添加一些null,然后重新改写4.2,使其不出现异常。
    	List<Student> tempList =  stuList.parallelStream().filter(stu -> (stu != null)).filter( stu -> ( stu.getAge() > 20 ||
    			stu.getGender().equals(Gender.female) || stu.getId() > 10 || stu.getName().equals("zhang") ||
    			stu.isJoinsACM())).sorted().collect(Collectors.toList());
    	//先过滤掉null,再按条件过滤
    

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

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

    5.2 GeneralStack接口的代码

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

    }

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

    A:上一次博客中用到了自己写的栈,原来是Integer栈,之后需要用到String栈,无奈只能把栈中的有关Integer类型全部改为String,这无疑是很麻烦的。当然,也可以事先把类型全部定义为Object,之后再进行强制类型转换。 强制类型转换虽然在编译时不会出错,但是有可能会在程序运行期间由于类型错误引起麻烦,所以我们能不用的话尽量可以不用。使用泛型就可以帮助我们实现不用强转而代码可被不同类型的对象所重用。从而避免强制类型转换导致的麻烦。正如本题所写的栈,可以存储指定的String,Integer或者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){
    	T max = list.get(0);
    	for(T i : list){
    		if(i.compareTo(max) > 0)
    			max = i;
    	}
    	return max ;
    }
    

    	intList.add(1);
    	intList.add(3);
    	intList.add(2);
    	strList.add("a");
    	strList.add("c");
    	strList.add("b");
    	String maxStr = max(strList);
    	System.out.println(maxStr);
    	Integer maxInt = max(intList);
    	System.out.println(maxInt);
    
    	测试结果:
    	c
    	3
    

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

     public static  <T extends  Comparable<? super T>> T max1(List<? extends T>  list ){
    	int j = 0;
    	for(;list.get(j) == null;)
    		j++;
    	T max = list.get(j);
    	for(T i : list){
    		if(i == null)continue;
    		if(i.compareTo( max) > 0)
    			max = i;
    	}
    	return  max ;
    } 
    

    	stuList.add( new StuUser(10,"1"));
    	stuList.add( new StuUser(30,"2"));
    	stuList.add( new StuUser(20,"3"));
    	User user = max1(stuList);
    	System.out.println(user);
    	Object user2 = max1(stuList);
    	System.out.println(user2);  
    
    	测试结果:
    	StuUser [no=2, toString()=User [age=30]]
    	StuUser [no=2, toString()=User [age=30]]  
    

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

    	public static <T> int  myCompare (T o1, T o2, Comparator<T> c){
    		return c.compare(o1, o2);	
    	}  
    

    	StuUser stu1 = new StuUser(10,"1");
    	StuUser stu2 = new StuUser(20,"2");
    	User user1 = new User(30);
    	System.out.println(myCompare(stu1,stu2,new StuUserComparator()));
    	System.out.println(myCompare(stu1,user1,new UserReverseComparator()));  
    
    	测试结果:
    	-1
    	20
    

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

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

    3.1. 码云代码提交记录

    3.2. PTA实验

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

  • 相关阅读:
    C#的一些基本问题
    Mac ssh连接远程服务器,并实现文件的上传和下载
    Redis 持久化
    Redis 数据类型
    @dynamic 与 @synthesize 关键词详解
    Redis介绍及安装
    crontab的用法
    修改文件权限
    Linux目录结构
    一些命令
  • 原文地址:https://www.cnblogs.com/slickghost/p/6715520.html
Copyright © 2011-2022 走看看