zoukankan      html  css  js  c++  java
  • 201521123103 《Java学习笔记》 第八周学习总结

    一、本周学习总结

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

     集合部分:TreeMap实现类:对键值进行排序。
               Map的entrySet    Set<Map.Entry<K,V>> entrySet()常用于遍历Map的键值对。Entry是Map内的一个接口。
               Collections 操作集合的工具类,常见方法:sort  排序;Shuffle  打乱集合中的元素。
    

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

    public class MapTest
    {
       public static void main(String[] args)
       {
          Map<String, Employee> staff = new HashMap<String, Employee>();
          staff.put("144-25-5464", new Employee("Amy Lee"));
          staff.put("567-24-2546", new Employee("Harry Hacker"));
          staff.put("157-62-7935", new Employee("Gary Cooper"));
          staff.put("456-62-5527", new Employee("Francesca Cruz"));
    
          // print all entries
    
          System.out.println(staff);
    
          // remove an entry
    
          staff.remove("567-24-2546");
    
          // replace an entry
    
          staff.put("456-62-5527", new Employee("Francesca Miller"));
    
          // look up a value
    
          System.out.println(staff.get("157-62-7935"));
    
          // iterate through all entries
    
          for (Map.Entry<String, Employee> entry : staff.entrySet())
          {
             String key = entry.getKey();
             Employee value = entry.getValue();
             System.out.println("key=" + key + ", value=" + value);
          }
       }
    }
    

    二、书面作业

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

    1.1 实验总结

     总结:学会使用泛型定义 List<String> List=new ArrayList();以及类型转换;remove方法,在remove的过程当中,删除当前下标为i的元素后,该元素后的所有元素将往前移一格,i--。
    

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

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

     (1)创建一个HashMap对象 Map<String,Integer>map=new HashMap<String ,Integer>();这里我限定了键、值类型
     (2)在集合中存入指定映射关系if(map.containsKey(x)) map.put(x, map.get(x)+1); if(map.get(x)==null)	 map.put(x, 1); 
     (3)输出集合的大小map.size()
     (4)遍历集合调用map.entrySet()方法List<Entry<String,Integer>> list =new ArrayList<Entry<String,Integer>>(map.entrySet());看资料说是创建了一个Map集合的映射项集合list
     (5)调用Collections.sort()排序
     (6)通过Map.Entry接口提供的getValue()方法得到value值,将其进行比较,按题目要求排好if (o1.getValue() != o2.getValue()) {return o2.getValue() - o1.getValue();} else {return o1.getKey().compareTo(o2.getKey());}
     (7)输出前十个
    

    2.2 实验总结

     总结:这个题目用到好多Map集合中的方法,写的我好心累,但最后写出来特别开心。这个说明老师上课的PPT十分重要。接下来就是把这些方法好好熟练一下。用到的知识点:
     1)containsKey(Object key)如果此映射包含指定键的映射关系,则返回true。
     2)put(K key,V value)向Map集合存入参数指定的映射关系。
     3)get(Object key)根据参数所指定的键对象搜索对应的值对象。
     4)通过映射项集合遍历Map集合:map.entrySet()方法将Map集合转换为一个映射集合,获取映射项集合的迭代对象,在每次迭代过程中将获取到的元素保存到Map.Entry类型变量中。
     5)通过Map.Entry接口提供的getValue()方法得到value值。
     6)Collections.sort()sort(List<T> list, Comparator<? super T> c)。
    

    参考书籍:Java语言基础教程 李东明 张丽娟主编

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

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

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

     写了两个循环:第一个是
    
    while (sc.hasNextLine()) {
            if ("!!!!!") {
               break;
                } 
            else {
                存入单词和对应的行数;
                }
                row++;
    }
    
     第二个循环
    
    while (sc.hasNextLine()) {
                if (没找到) {
                    System.out.println("found 0 results");
                } 
                else {
                    System.out.println(行数);
                    for each {
                        System.out.println(整行的单词);
                    }
                }
    

    3.3 实验总结

     用到的方法和上题很像,但是不会编写行数的存储。我问的同学,又学到了新的东西。
    

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

     代码:
    
    import java.util.ArrayList;
    
    public class Main {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ArrayList<Student> list=new ArrayList<Student>();
            Student she1=new Student(11,"zhang",21,Gender.female,true);
            Student she2=new Student(9,"zhang",21,Gender.female,true);
            Student she3=new Student(9,"zhang",19,Gender.female,true);
            Student she4=new Student(9,"zhang",21,Gender.male,true);
            list.add(she1);
            list.add(she2);
            list.add(she3);
            list.add(she4);
            for (Student student : list) {
                System.out.println(student.find());
            }
        }
    }
    
    public class Student {
    	    private int id;
    	    private String name;
    	    private int age;
    	    private Gender gender;//枚举类型
    	    private boolean joinsACM; //是否参加过ACM比赛
    
    	    
    	    public Student(int 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>10&&this.name.equals("zhang")&&this.age>20&&this.gender==Gender.female&&this.joinsACM)
    	        {
    	            Student she=new Student(this.id,this.name,this.age,this.gender,this.joinsACM);
    	            return she;
    	        }
    	        
    	        else
    	            return null;
    	        
    	   }
    }
    
     运行结果:
    

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

    问同学写出来的

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

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

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

     main函数不会写,运行不出来,每次只能输出Integer Test,然后没了
    

    5.2 GeneralStack接口的代码

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

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

     答:泛型可以使我们定义的接口对任何引用类型的数据都适用,也就是说类型可以实现通一个接口若干次,只要每次使用不同的类型参数。
    

    6、泛型方法 基础参考文件GenericMain,在此文件上进行修改。

    6.1 编写方法max,该方法可以返回List中所有元素的最大值。List中的元素必须实现Comparable接口。

    编写的max方法需使得String max = max(strList)可以运行成功,其中strList为List类型。也能使得Integer maxInt = max(intList);运行成功,其中intList为List类型。

     代码如下:
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class GenericMain {
    	   public static <T extends Object & Comparable<T>> T max(List<T> list)
    	   {                     
    	      T max = list.get(0);
    	      for (T a : list) {
    	        if ( a.compareTo(max) > 0 ){
    	         max = a; 
    	      }
    	    }
    	      return max;
    	   }
    	    public static void main(String[] args) {
    	        List<String>strList=new ArrayList<String>();
    	        List<Integer>intList=new ArrayList<Integer>();
    	        strList.add("ab");
    	        strList.add("abc");
    	        strList.add("abcd");
    	        intList.add(1);
    	        intList.add(2);
    	        intList.add(3);
    	        String maxStr = max(strList);
    	        Integer maxInt = max(intList);
    	        System.out.println("String max="+maxStr+","+"Integer max="+maxInt);
    	    }
    	}
    
     运行结果:
    

    三、码云上代码提交记录及PTA实验总结

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

    3.1. 码云代码提交记录

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

    3.2. PTA实验

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

    实验总结已经在作业中体现,不用写。

  • 相关阅读:
    python全栈开发从入门到放弃之socket并发编程之协程
    python全栈开发从入门到放弃之socket并发编程多线程GIL
    python全栈开发从入门到放弃之socket并发编程多线程
    python全栈开发从入门到放弃之socket并发编程多进程
    python全栈开发从入门到放弃之socket网络编程基础
    python全栈开发从入门到放弃之异常处理
    python全栈开发从入门到放弃之面向对象反射
    python全栈开发从入门到放弃之面向对象的三大特性
    转:经典ACM算法
    反射在Java Swing中的应用
  • 原文地址:https://www.cnblogs.com/yayaya/p/6706409.html
Copyright © 2011-2022 走看看