zoukankan      html  css  js  c++  java
  • 20165333第十周课下补做

    一、知识点总结

    泛型
    主要目的是可以建立具有类型安全的集合框架,如链表、散列映射等数据结构
    泛型类声明
    可以使用class 名称<泛型列表>声明一个类,为了和普通的类有所区别,这样声明的类称作泛型类,没有指定E是何种类型的数据,它可以是任何对象或接口,但不能是基本类型数据
    使用泛型类声明对象
    泛型类声明和创建对象时,类名后多了一对“<>”,而且必须要用具体的类型替换“<>”中的泛型
    链表
    链表是由若干个称作节点的对象组成的一种数据结构,每个节点含有一个数据和下一个节点的引用
    LinkedList泛型类
    LinkedList
    常用方法
    LinkedList
    LinkedList
    遍历链表
    当用户需要遍历集合中的对象时,应当使用该集合提供的迭代器,而不是让集合本身来遍历其中的对象
    链表对象可以使用iterator()方法获取一个Iterator对象,该对象就是针对当前链表的迭代器
    排序与查找
    Collections类提供的用于排序和查找的类方法如下:
    public static sort(List list): 该方法可以将list中的元素升序排列
    int binarySearch(List list, T key,CompareTo c): 使用折半法查找list是否含有和参数key相等的元素,如果key链表中某个元素相等,方法返回和key相等的元素在链表中的索引位置(链表的索引位置从0考试),否则返回-1
    洗牌与旋转
    Collections类还提供了将链表中的数据重新随机排列的类方法以及旋转链表中数据的类方法
    public static void shuffle(List list) 将list中的数据按洗牌算法重新随机排列
    static void rotate(List list, int distance) 旋转链表中的数据
    public static void reverse(List list) 翻转list中的数据
    堆栈
    堆栈是一种“后进先出”的数据结构,只能在一端进行输入或输出数据的操作
    Stack
    散列映射
    HashMap
    树集
    TreeSet泛型类
    TreeSet
    TreeSet mytree=new TreeSe();
    mytree.add("boy"); 使用add方法为树集添加节点
    TreeSet类的常用方法
    public boolean add(E o) 向树集添加加节点
    public void clear() 删除树集中的所有节点
    public void contains(Object o) 如果树集中有包含参数指定的对象,该方法返回true,否则返回false
    public E first() 返回树集中的第一个节点中的数据(最小的节点)
    public E last() 返回最后一个节点中的数据(最大的节点)
    public isEmpty() 判断是否是空树集,如果树集不含任何节点,该方法返回true
    public boolean remove(Object o) 删除树集中的存储参数指定的对象的最小节点
    public int size() 返回树集中节点的数目

    补做

    补做第二题
    在数据结构和算法中,排序是很重要的操作,要让一个类可以进行排序,有两种方法:

    • 有类的源代码,针对某一成员变量排序,让类实现Comparable接口,调用Collection.sort(List)
    • 没有类的源代码,或者多种排序,新建一个类,实现Comparator接口 调用Collection.sort(List, Compatator)

    针对下面的Student类,使用Comparator编程完成以下功能:

    1. 在测试类StudentTest中新建学生列表,包括自己和学号前后各两名学生,共5名学生,给出运行结果(排序前,排序后)
    2. 对这5名同学分别用学号和总成绩进行增序排序,提交两个Comparator的代码
    3. 课下提交代码到码云
      示例代码
    class Student {
       private String id;//表示学号
       private String name;//表示姓名
       private int age;//表示年龄
       private double computer_score;//表示计算机课程的成绩
       private double english_score;//表示英语课的成绩
       private double maths_score;//表示数学课的成绩
       private double total_score;// 表示总成绩
       private double ave_score; //表示平均成绩
       public Student(String id, String name){
           this.id = id;
           this.name = name;
    }
       public Student(String id, String name, char sex, int age){
           this(id, name);
           this.sex = sex;
           this.age = age;
    }
       public String getId(){
           return id;
    }//获得当前对象的学号,
       public double getComputer_score(){
           return computer_score;
    }//获得当前对象的计算机课程成绩,
       public double getMaths_score(){
           return maths_score;
    }//获得当前对象的数学课程成绩,
       public double getEnglish_score(){
           return english_score;
    }//获得当前对象的英语课程成绩,
    
       public void setId(String id){
           this.id=id;
    }// 设置当前对象的id值,
       public void setComputer_score(double computer_score){
           this.computer_score=computer_score;
    }//设置当前对象的Computer_score值,
       public void setEnglish_score(double english_score){
           this.english_score=english_score;
    }//设置当前对象的English_score值,
       public void setMaths_score(double maths_score){
           this.maths_score=maths_score;
    }//设置当前对象的Maths_score值,
    
       public double getTotalScore(){
           return computer_score+maths_score+english_score;
    }// 计算Computer_score, Maths_score 和English_score 三门课的总成绩。
       public double getAveScore(){
           return getTotalScore()/3;
    }// 计算Computer_score, Maths_score 和English_score 三门课的平均成绩。
    
    }
    
    class Undergraduate extends Student{
       private String classID;
    
       public Undergraduate(String id, String name, char sex, int age,String classID){
           super(id,name,sex,age);
           this.classID=classID;
       }
       public String getClassID(){
           return classID;
       }
       public void setClassID(String classID){
           this.classID=classID;
       }
    }
    
    • 改写代码
    import java.util.*;
    public class MyComparator {
        public  static void main(String[] args) {
            List<StudentIF> list = new LinkedList<>();
            StudentIF stu1=new StudentIF(20165331,"胡麟","male",20,55,77,88);
            StudentIF stu2=new StudentIF(20165332,"延亿卓","male",20,88,100,66);
            StudentIF stu3=new StudentIF(20165333,"陈国超","female",20,77,88,66);
            StudentIF stu4=new StudentIF(20165334,"李天龙","male",20,99,77,88);
            StudentIF stu5=new StudentIF(20165335,"薛茂哲","male",20,66,99,77);
            stu1.getAveScore();
            stu2.getAveScore();
            stu3.getAveScore();
            stu4.getAveScore();
            stu5.getAveScore();
            list.add(stu1);
            list.add(stu2);
            list.add(stu3);
            list.add(stu4);
            list.add(stu5);
            System.out.println("排序前链表中的数据:");
            Iterator<StudentIF> iter=list.iterator();
            while(iter.hasNext()) {
                StudentIF studentIF=iter.next();
                System.out.println(studentIF.toString());
            }
            IDComparator idComparator = new IDComparator();
            Collections.sort(list, idComparator);
            System.out.println("根据学生学号升序排序后链表中的数据:");
            Iterator<StudentIF> iter2=list.iterator();
            while(iter2.hasNext()) {
                StudentIF studentIF=iter2.next();
                System.out.println(studentIF.toString());
            }
            ScoreComparator scoreComparator = new ScoreComparator();
            Collections.sort(list, scoreComparator);
            System.out.println("根据学生成绩升序排序后链表中的数据:");
            Iterator<StudentIF> iter3=list.iterator();
            while(iter3.hasNext()) {
                StudentIF studentIF=iter3.next();
                System.out.println(studentIF.toString());
            }
        }
    }
    class IDComparator implements Comparator {
        @Override
        public int compare(Object o1, Object o2) {
            StudentIF st1 = (StudentIF)o1;
            StudentIF st2 = (StudentIF)o2;
            return (st1.getId()-st2.getId());
        }
    }
    class ScoreComparator implements Comparator{
        @Override
        public int compare(Object o1, Object o2){
            StudentIF st1 = (StudentIF)o1;
            StudentIF st2 = (StudentIF)o2;
            return (int)(st1.getTotalScore()-st2.getTotalScore());
        }
    }
    class StudentIF {
        private int id;//表示学号
        private String name;//表示姓名
        private int age;//表示年龄
        private String sex;//表示性别
        private double computer_score;//表示计算机课程的成绩
        private double english_score;//表示英语课的成绩
        private double maths_score;//表示数学课的成绩
        private double total_score;// 表示总成绩
        private double ave_score; //表示平均成绩
        @Override
        public String toString() {
            return "姓名:"+name+",年龄:"+age+",学号:"+id+",总分:"+total_score;
        }
        public StudentIF(int id, String name, String sex, int age,double computer_score,
                         double english_score,double maths_score) {
            this.id = id;
            this.name = name;
            this.sex = sex;
            this.age = age;
            this.computer_score = computer_score;
            this.english_score = english_score;
            this.maths_score = maths_score;
        }
        public int getId() {
            return id;
        }//获得当前对象的学号,
    
        public double getComputer_score() {
            return computer_score;
        }//获得当前对象的计算机课程成绩,
    
        public double getMaths_score() {
            return maths_score;
        }//获得当前对象的数学课程成绩,
    
        public double getEnglish_score() {
            return english_score;
        }//获得当前对象的英语课程成绩,
    
        public void setId(int id) {
            this.id = id;
        }// 设置当前对象的id值,
    
        public void setComputer_score(double computer_score) {
            this.computer_score = computer_score;
        }//设置当前对象的Computer_score值,
    
        public void setEnglish_score(double english_score) {
            this.english_score = english_score;
        }//设置当前对象的English_score值,
    
        public void setMaths_score(double maths_score) {
            this.maths_score = maths_score;
        }//设置当前对象的Maths_score值,
    
        public double getTotalScore() {
            total_score=computer_score + maths_score + english_score;
            return total_score;
        }// 计算Computer_score, Maths_score 和English_score 三门课的总成绩。
    
        public double getAveScore() {
            return getTotalScore() / 3;
        }// 计算Computer_score, Maths_score 和English_score 三门课的平均成绩。
    
    }
    

    运行截图

    课下补做三

    • 补充MyList.java的内容
      原始代码
    public class MyList {
    	public static void main(String [] args) {
    		//选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
    		
    		
    		//把上面四个节点连成一个没有头结点的单链表
    		
    		//遍历单链表,打印每个结点的
    
    		//把你自己插入到合适的位置(学号升序)
    
    		//遍历单链表,打印每个结点的
    
    		//从链表中删除自己
    
    		//遍历单链表,打印每个结点的
    	}
    }
    public class Node<T>                             //单链表结点类,T指定结点的元素类型
    {
        public T data;                               //数据域,存储数据元素
        public Node<T> next;                         //地址域,引用后继结点
    
        public Node(T data, Node<T> next)            //构造结点,data指定数据元素,next指定后继结点
        {
            this.data = data;                        //T对象引用赋值
            this.next = next;                        //Node<T>对象引用赋值
        }
        public Node()
        {
            this(null, null);
        }
        public String toString()                     //返回结点数据域的描述字符串
        {
            return this.data.toString();
        }
    }
    

    完成代码

    public class Node<T>
    //单链表结点类,T指定结点的元素类型
    {
        public T data;
        //数据域,存储数据元素
        public Node<T> next;
        //地址域,引用后继结点
    
        public Node(T data, Node<T> next)
        //构造结点,data指定数据元素,next指定后继结点
        {
            this.data = data;
            //T对象引用赋值
            this.next = next;
            //Node<T>对象引用赋值
        }
        public Node()
        {
            this(null, null);
        }
        @Override
        public String toString()
        //返回结点数据域的描述字符串
        {
            return this.data.toString();
        }
    }
    
    public class MyList2 {
        public static void main(String [] args) {
    //选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
            Node<Integer> S1 = new Node<Integer>(20165313, null);
            Node<Integer> S2 = new Node<Integer>(20165314, null);
            Node<Integer> S3 = new Node<Integer>(20165316, null);
            Node<Integer> S4 = new Node<Integer>(20165317, null);
    //把上面四个节点连成一个没有头结点的单链表
            S1.next=S2;
            S2.next=S3;
            S3.next=S4;
    //遍历单链表,打印每个结点
            Node<Integer> oriList=S1;
            System.out.println("打印初始链表");
            while(oriList!=null){
                System.out.println(oriList.data);
                oriList=oriList.next;
            }
    //把你自己插入到合适的位置(学号升序)
            Node<Integer> S5=new Node<Integer>(20165315,null);
            Node<Integer> oriAddList=S1;
            while (oriAddList != null) {
                if(oriAddList.data < S5.data && oriAddList.next.data > S5.data){
                    S5.next=oriAddList.next;
                    oriAddList.next=S5;
                    break;
                }
                else{
                    oriAddList=oriAddList.next;
                }
            }
    
    //遍历单链表,打印每个结点的
            Node<Integer> addList=S1;
            System.out.println("打印插入后的链表");
            while(addList!=null){
                System.out.println(addList.data);
                addList=addList.next;
            }
    //从链表中删除自己
            Node<Integer> oriDeleteList=S1;
            while(oriDeleteList!=null){
                if(oriDeleteList.next.data.equals(S5.data)){
                    oriDeleteList.next=S5.next;
                    break;
                }
                oriDeleteList=oriDeleteList.next;
            }
    //遍历单链表,打印每个结点的
            Node<Integer> deleteList=S1;
            System.out.println("打印删除后的链表");
            while(deleteList!=null){
                System.out.println(deleteList.data);
                deleteList=deleteList.next;
            }
        }
    }
    

    第十五章代码分析

    例子1:使用泛型类Cone声明了一个圆锥对象和一个方锥对象,并计算它们的体积,并且不考虑底的类型
    例子2:分别使用迭代器和get()方法遍历链表,通过调用currentTimeMillis()方法比较二者所用的时间
    例子3:创建一个链表,加入两个节点,将节点类型强制转换后输出
    例子4:创建一个链表,加入姓名、身高等数据,利用sort方法(调用compareTo方法)对链表中的数据按身高排序,如果有身高相同的情况,则输出身高相同的姓名
    例子5:使用shuffle()、rotate()方法分别对链表中的数据进行随机排列、向右旋转一次,最后输出
    例子6:使用压栈弹栈操作输出斐波那次递归序列
    例子7:wordPolice类使用Scanner解析Word.txt中的单词,然后将英文单词-汉语翻译作为建/值存储到散列映射中用户查询
    例子8:使用add(V value)方法对树集中的数据value按英语成绩进行升序排序,并且compareTo方法不允许有成绩相同的结点
    例子9:使用put(K key,V value)方法对树映射中的数据`value按关键字key进行升序排列,并且compareTo方法允许有成绩相同的结点
    例子10:使用ArrayList对链表中数据进行自动装箱(add()方法)和自动拆箱(get()方法)
    例子11:使用对象流实现商品库存的录入与显示系统,程序将Goods类的对象作为链表的结点,并将链表写入文件

    十五章课后编程习题

    (1)用堆栈结构输出an的若干项,其中an=2an-1+2an-2,a1=3,a2=8

    import java.util.*;
    public class E1 {
        public static void main(String[] args) {
            Stack<Integer> stack=new Stack<Integer>();
            stack.push(new Integer(3));
            stack.push(new Integer(8));
            int k=1;
            while(k<=10){
                for(int i =1;i<=2;i++){
                    Integer F1=stack.pop();
                    int f1=F1.intValue();
                    Integer F2=stack.pop();
                    int f2=F2.intValue();
                    Integer temp=new Integer(2*f1+2*f2);
                    System.out.println(""+temp.toString());
                    stack.push(temp);
                    stack.push(F2);
                    k++;
                }
            }
        }
    }
    

    (2)编写一个程序,将链表中的学生英语成绩单存放到一个树集中,使得按成绩自动排序,并输出排序结果

    import java.util.*;
    public class E2 {
        public static void main(String[] args) {
            List<Student> list=new LinkedList<Student>();
            int score[]={99,88,77,66,55,44,33,22,11};
            String name[]={"张三","李四","王五","赵六","白云","黑土","蓝天","绿水","红花"};
            for(int i=0;i<score.length;i++){
                list.add(new Student(score[i],name[i]));
            }
            Iterator<Student> iter=list.iterator();
            TreeSet<Student> mytree=new TreeSet<Student>();
            while(iter.hasNext()){
                Student stu=iter.next();
                mytree.add(stu);
            }
            Iterator<Student> te=mytree.iterator();
            while(te.hasNext()){
                Student stu=te.next();
                System.out.println(""+stu.name+" "+stu.english);
            }
        }
    }
    class Student implements Comparable{
        int english=0;
        String name;
        Student(int english,String name){
            this.name=name;
            this.english=english;
        }
        @Override
        public int compareTo(Object b){
            Student st=(Student)b;
            return (this.english-st.english);
        }
    }
    

    (3)有10个U盘,有两个重要的属性:价格和容量。编写一个应用程序,使用TreeMap

    import java.util.*;
    
    class UDiscKey implements Comparable {
        double key = 0;
        UDiscKey(double d) {
            this.key = d;
        }
        @Override
        public int compareTo(Object b) {
            UDiscKey discKey = (UDiscKey) b;
            if ((this.key - discKey.key) == 0) {
                return -1;
            }
            else {
                return (int) ((this.key - discKey.key) * 1000);
            }
        }
    }
    
    class UDisc {
        int amount;
        double price;
        UDisc(int a,double p) {
            amount=a;
            price=p;
        }
    }
    
    public class E3 {
        public static void main(String args[]) {
            TreeMap<UDiscKey,UDisc> treemap = new TreeMap<UDiscKey, UDisc>();
            int amount[]={100,200,300,400,500,600,700,800,900,1000};
            double price[]={111,222,333,444,555,666,777,888,999,1000};
            UDisc uDisc[] = new UDisc[10];
            for (int k = 0; k < uDisc.length; k++) {
                uDisc[k] = new UDisc(amount[k],price[k]);
            }
            UDiscKey key[] = new UDiscKey[10];
            for (int k = 0; k < key.length; k++) {
                key[k] = new UDiscKey(uDisc[k].amount);
            }
            for (int k = 0; k < uDisc.length; k++) {
                treemap.put(key[k], uDisc[k]);
            }
            int number = treemap.size();
            System.out.println("树映射中有" + number + "个对象,按容量排序:");
            Collection<UDisc> collection = treemap.values();
            Iterator<UDisc> iter = collection.iterator();
            while (iter.hasNext()) {
                UDisc disc = iter.next();
                System.out.println(" 容量 " + disc.amount);
            }
            treemap.clear();
            for (int k = 0; k < key.length; k++) {
                key[k] = new UDiscKey(uDisc[k].price);
            }
            for (int k = 0; k < uDisc.length; k++) {
                treemap.put(key[k], uDisc[k]);
            }
            number = treemap.size();
            System.out.println("树映射中有" + number + "个对象:按价格排序:");
            collection = treemap.values();
            iter = collection.iterator();
            while (iter.hasNext()) {
                UDisc disc = (UDisc) iter.next();
                System.out.println(" 价格 " + disc.price);
            }
        }
    }
    
  • 相关阅读:
    python基础之字符串和字节的转换
    python学习笔记(三)字符串方法、读写文件、json处理以及函数
    python学习笔记(二):list,字典,字符串,元组,文件
    python学习笔记(一):python入门
    接口测试:jmeter学习笔记:数据库操作和压测
    接口测试:postman和jmeter随记
    设计模式之建造者模式
    设计模式之外观模式
    设计模式之模板模式
    设计模式之原型模式
  • 原文地址:https://www.cnblogs.com/challengeeverthing/p/9000242.html
Copyright © 2011-2022 走看看