zoukankan      html  css  js  c++  java
  • 20165201 课下作业第十周(选做)

    20165201 课下作业第十周(选做)


    一、相关知识点总结

    • 泛型
        可以使用class 名称<泛型列表>声明一个类,<>里面可以是类和接口,但不能是基本数据类型!泛型声明的类称作泛型类
      且使用泛型时,不需要进行强制类型转换
    • 链表
        我们在处理或储存相同类型的数据时,如果用数组的话,必须先定义它的大小,并且不能随意改变大小,这不利于数据的储存,占用了很多不必要的内存资源;然而链表却恰恰弥补了这一点,它能够动态的分配存储空间。
        链表是由若干个称作节点的对象组成的一种数据结构,每个节点含有一个数据和下一个节点的引用
        使用LinkedList来创建链表对象,链表对象可以使用iterator()方法获取一个Iterator对象,该对象就是针对当前链表的迭代器,可以对链表进行遍历
        public static sort(List<E> list)能够把list中的元素升序排列;而int binarySearch(List<T> list, T key,CompareTo<T> c)利用折半法查找list中是否含有与key相等的元素,如果某个元素相等,能返回和key相等的元素在链表中的索引位置,否则返回-1
    • 堆栈
      堆栈是“后进先出”的数据结构,即只能在它的一端进行输入或输出的操作
    • 散列映射
      HashMap泛型类创建的对象
    • 树集
      TreeSet类创建的对象叫做树集,在树集中,按字典的顺序从左到右依次递增,从上到下依次递减
    • 树映射
      TreeMap对象为树映射,树映射的节点可以存储关键字、关键值对。排序时会按照储存的关键字升序排列

    二、课上内容补做代码和运行结果截图

    • 题目要求:
      补充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();
        }
    }
    
    • 代码如下
    import java.util.*;
    
    class Student implements Comparable {
        int id;
        String name;
    
        Student(int i, String n) {
            id = i;
            name = n;
        }
    
        public int compareTo(Object b) {
            Student stu = (Student) b;
            return (this.id - stu.id);
        }
    }
    
    public class MyList {
        public static void main(String[] args) {
            //选用合适的构造方法,用你学号前后各两名同学的学号创建四个结点
            LinkedList<Student> list = new LinkedList<>();
            list.add(new Student(20165238, "步繁"));
            list.add(new Student(20165239, "其米仁增"));
            list.add(new Student(20165202, "贾海粟"));
            list.add(new Student(20165203, "夏云霄"));
            //把上面四个节点连成一个没有头结点的单链表
            Iterator<Student> iter = list.iterator();
            //遍历单链表,打印每个结点的
            System.out.println("打印初始的单链表:");
            while (iter.hasNext()) {
                Student stu = iter.next();
                System.out.println(stu.id + " " + stu.name);
            }
            //把你自己插入到合适的位置(学号升序)
            list.add(new Student(20165201, "李梓豪"));
            Collections.sort(list);
            //遍历单链表,打印每个结点的
            iter = list.iterator();
            System.out.println("打印插入自己的学号和姓名并排序后的单链表:");
            while (iter.hasNext()) {
                Student stu = iter.next();
                System.out.println(stu.id + "  " + stu.name);
            }
            //从链表中删除自己
            list.remove(0);
            iter = list.iterator();
            //遍历单链表,打印每个结点的
            System.out.println("打印删除自己的学号和姓名并排序后的单链表:");
            while (iter.hasNext()) {
                Student stu = iter.next();
                System.out.println(stu.id + "  " + stu.name);
            }
        }
    }
    
    • 运行结果截图

    三、补做代码链接

  • 相关阅读:
    IPC之msgutil.c源码解读
    IPC之msg.c源码解读
    IPC之mqueue.c源码解读
    从锅炉工到AI专家(10)
    从锅炉工到AI专家(9)
    从锅炉工到AI专家(8)
    从锅炉工到AI专家(7)
    从锅炉工到AI专家(6)
    从锅炉工到AI专家(5)
    从锅炉工到AI专家(4)
  • 原文地址:https://www.cnblogs.com/cbmwtsl/p/9000384.html
Copyright © 2011-2022 走看看