日撸 Java 三百行day23-24

news2025/3/1 4:18:15

文章目录

  • 说明
  • day23 使用具有通用性的队列
    • 1.复用技术
    • 2.包装类
    • 3.java内存分配
    • 4.代码
  • day24 二叉树的建立
    • 1.思路
    • 2.代码

说明

闵老师的文章链接: 日撸 Java 三百行(总述)_minfanphd的博客-CSDN博客
自己也把手敲的代码放在了github上维护:https://github.com/fulisha-ok/sampledata

day23 使用具有通用性的队列

1.复用技术

为了提高代码的复用,java中可以通过继承,实现接口,等方式来实现

  • 继承(extends)
    其实我们每一个类都会继承超类(Object)这个是隐式继承也是java中唯一的一个隐式集成(子类自动继承超类的所有属性和方法)继承可以提高我们代码的复用性也方便维护,也可以方便我们扩展。在开发中,并不是继承越多越好,如果继承的链太长可能会导致继承爆炸从而影响系统性能,扩展也很麻烦。

  • 接口
    定义的接口类中只是定义了一组方法,但具体的是没有实现的,只有实现了这个接口才完成具体的实现,而对于不同的类实现这个接口,会有不同的逻辑,这也体现了java的动态性。这里主要体现的是接口的复用技术

在这里插入图片描述

2.包装类

包装类型主要是用来封装基本数据类型的, java中基本的8种数据类型并不是对象,可以通过包装类将其转为包装类对象。可以把基本数据类型转为包装类型,也可以把包装类型转为基本数据类型。(装箱和拆箱)。

  • 自动装箱
Integer tempIndexInteger = 0;  // 自动装箱,等价于 Integer tempIndexInteger = Integer.valueOf(0);
  • 自动拆箱
int i = new Integer(10);  // 自动拆箱,等价于 int i = Integer.valueOf(10).intValue();

文章代码中:
 tempIndex = (int) tempIntQueue.dequeue(); //等价于 ((Integer) tempIntQueue.dequeue()).intValue();

我认为特别要注意的是基本数据类型和包装类型在没有赋值时,系统为其赋初始值时,例如int a; 则a默认为0,而 Integer a;则默认a为null (其他类型也类似)从这也可以体现出对象和基本数据类型的差别。

3.java内存分配

java内存分配其实有助于我们去理解数据是如何存储和运行的,我认为这一篇文章写得比较易于理解,这是文章连接:点击这里

4.代码

 /**
     * Covert the tree to data arrays, including a char array and an int array
     * The results are stored in two member variables
     */
    public void toDataArraysObjectQueue(){
        int tempLength = getNumNodes();

        valuesArray = new char[tempLength];
        indicesArray = new int[tempLength];
        int i = 0;

        CircleObjectQueue tempQueue = new CircleObjectQueue();
        tempQueue.enqueue(this);
        CircleObjectQueue tempIntQueue = new CircleObjectQueue();
        Integer tempIndexInteger = 0;  // 自动装箱,等价于 Integer tempIndexInteger = Integer.valueOf(0);
        tempIntQueue.enqueue(tempIndexInteger);

        BinaryCharTree tempTree = (BinaryCharTree) tempQueue.dequeue();
        int tempIndex = (int) tempIntQueue.dequeue();
        System.out.println("tempIndex = " + tempIndex);
        while (tempTree != null){
            valuesArray[i] = tempTree.value;
            indicesArray[i] = tempIndex;
            i++;

            if (tempTree.leftChild != null){
                tempQueue.enqueue(tempTree.leftChild);
                tempIntQueue.enqueue(Integer.valueOf(tempIndex * 2 + 1));
            }

            if (tempTree.rightChild != null) {
                tempQueue.enqueue(tempTree.rightChild);
                tempIntQueue.enqueue(Integer.valueOf(tempIndex * 2 + 2));
            }

            tempTree = (BinaryCharTree) tempQueue.dequeue();
            if (tempTree == null) {
                break;
            }

            tempIndex = (int) tempIntQueue.dequeue(); //等价于 ((Integer) tempIntQueue.dequeue()).intValue();
        }

    }

在这里插入图片描述

day24 二叉树的建立

1.思路

文章代码是用的一个数组存结点值,一个数组存结点的索引位置。

  • :在存储二叉树时,参考day22思路,采用两个数组,一个存储数据,一个存储索引结点值来存储二叉树并存储二叉树结点之间的关系。
    在这里插入图片描述
    在这里插入图片描述

  • :现在建立二叉树,根据两个数组来还原二叉树的关系。结合上面的图,如[a, b, c, d, e, f, g];[0, 1, 2, 4, 5, 9, 10]
    对于第一个结点a,不用考虑肯定是根结点;对于第i个结点,要和那个结点有关系就要往前找(我们用的层次遍历,所以往前找,遍历i之前的结点为记为j),找符合条件的即要满足关系:i = j×2 +1 或 i=j×2+2
    举例:如找c结点与那个结点有联系,则需要去遍历a,和 b两个结点其中是否有满足上面公式的,发现a满足,则表面a是c的双亲结点。
    所以核心思路就是父节点和孩子结点之间的关系。i = j×2 +1 或 i=j×2+2

2.代码

/**
     * The parameters must be correct since no validity.check is undertaken.
     * @param paraDataArray  The array for data.
     * @param paraIndicesArray The array for indices.
     */
    public BinaryCharTree(char[] paraDataArray, int[] paraIndicesArray){
        // Step 1. Use a sequential list to store all nodes.
        int tempNumNodes = paraDataArray.length;
        BinaryCharTree[] tempAllNodes = new BinaryCharTree[tempNumNodes];
        for (int i = 0; i < tempNumNodes; i++){
            tempAllNodes[i] = new BinaryCharTree(paraDataArray[i]);
        }

        for (int i = 1; i < tempNumNodes; i++){
            for (int j = 0; j < i; j++){
                System.out.println("indices " + paraIndicesArray[j] + " vs. " + paraIndicesArray[i]);
                if (paraIndicesArray[i] == paraIndicesArray[j]*2 + 1){
                    // i结点是j结点的左孩子
                    tempAllNodes[j].leftChild = tempAllNodes[i];
                    System.out.println("Linking " + j + " with " + i);
                    break;
                }else if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 2){
                    // i结点是j结点的右孩子
                    tempAllNodes[j].rightChild = tempAllNodes[i];
                    System.out.println("Linking " + j + " with " + i);
                    break;
                }
            }
        }

        //Step 3. The root is the first node.
        value = tempAllNodes[0].value;
        leftChild = tempAllNodes[0].leftChild;
        rightChild = tempAllNodes[0].rightChild;
    }

main方法
  public static void main(String args[]) {
        BinaryCharTree tempTree = manualConstructTree();
        System.out.println("\r\nPreorder visit:");
        tempTree.preOrderVisit();
        System.out.println("\r\nIn-order visit:");
        tempTree.inOrderVisit();
        System.out.println("\r\nPost-order visit:");
        tempTree.postOrderVisit();
        System.out.println("\r\nLevel-order visit:");
        tempTree.levelOderVisit();

        System.out.println("\r\n\r\nThe depth is: " + tempTree.getDepth());
        System.out.println("The number of nodes is: " + tempTree.getNumNodes());

        tempTree.toDataArrays();
        System.out.println("The values are: " + Arrays.toString(tempTree.valuesArray));
        System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));

        tempTree.toDataArraysObjectQueue();
        System.out.println("Only object queue.");
        System.out.println("The values are: " + Arrays.toString(tempTree.valuesArray));
        System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));

        System.out.println("*********************************************");
        //char[] tempCharArray = {'A', 'B', 'C', 'D', 'E', 'F'};
        //int[] tempIndicesArray = {0, 1, 2, 4, 5, 12};
        char[] tempCharArray = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
        int[] tempIndicesArray = {0, 1, 2, 4, 5, 9, 10};
        BinaryCharTree tempTree2 = new BinaryCharTree(tempCharArray, tempIndicesArray);

        System.out.println("\r\nPreorder visit:");
        tempTree2.preOrderVisit();
        System.out.println("\r\nIn-order visit:");
        tempTree2.inOrderVisit();
        System.out.println("\r\nPost-order visit:");
        tempTree2.postOrderVisit();
    }

在这里插入图片描述
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/412528.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

看完这篇 教你玩转渗透测试靶机vulnhub——My File Server: 2

Vulnhub靶机My File Server: 2渗透测试详解Vulnhub靶机介绍&#xff1a;Vulnhub靶机下载&#xff1a;Vulnhub靶机安装&#xff1a;Vulnhub靶机漏洞详解&#xff1a;①&#xff1a;信息收集&#xff1a;②&#xff1a;FTP匿名登入③&#xff1a;SSH私钥登入④&#xff1a;SMB共享…

超详细从入门到精通,pytest自动化测试框架实战-测试运行钩子(七)

目录&#xff1a;导读前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09;前言 pytest中用例执行相…

数据的表示和存储——

目录 浮点数的编码表示 浮点数类型 ​编辑 浮点数的表示 &#xff08;1&#xff09;浮点数&#xff08;Float Point&#xff09;的表示范围 &#xff08;2&#xff09;规格化数形式 &#xff08;3&#xff09;IEEE 754标准 其他形式的机器数表示 个人总结 浮点数的编码表…

深度学习_Learning Rate Scheduling

我们在训练模型时学习率的设置非常重要。 学习率的大小很重要。如果它太大&#xff0c;优化就会发散&#xff0c;如果它太小&#xff0c;训练时间太长&#xff0c;否则我们最终会得到次优的结果。其次&#xff0c;衰变率同样重要。如果学习率仍然很大&#xff0c;我们可能会简…

MySQL NDB Cluster使用docker compose一键部署

本文主要用来学习MySQL NDB Cluster 解决学习过程中的痛点&#xff1a;需要开启N台VMware虚拟机&#xff0c;电脑不堪重负 使用docker部署&#xff0c;完美解决 本文使用的docker image: mysql/mysql-cluster:8.0 创建mysql_cluster目录&#xff0c;后续操作都在这个目录下 …

基于html+css的盒子展示7

准备项目 项目开发工具 Visual Studio Code 1.44.2 版本: 1.44.2 提交: ff915844119ce9485abfe8aa9076ec76b5300ddd 日期: 2020-04-16T16:36:23.138Z Electron: 7.1.11 Chrome: 78.0.3904.130 Node.js: 12.8.1 V8: 7.8.279.23-electron.0 OS: Windows_NT x64 10.0.19044 项目…

系统集成项目管理工程师软考第三章习题(每天更新)

第一章指路&#xff1a;系统集成项目管理工程师软考第一章习题&#xff08;已完结&#xff09;_程序猿幼苗的博客-CSDN博客 第二章指路&#xff1a;系统集成项目管理工程师软考第二章习题&#xff08;已完结&#xff09;_程序猿幼苗的博客-CSDN博客 第3章信息系统集成专业技术…

基于密集学习的半监督目标检测

文章目录Dense Learning based Semi-Supervised Object Detection摘要本文方法实验结果Dense Learning based Semi-Supervised Object Detection 摘要 提出了一种基于密集学习(DSL)的无锚点的半监督目标检测算法用于分配多层级和精确的密集像素伪标签的自适应过滤器用于生成稳…

C++13:搜索二叉树

目录 搜索二叉树概念 模拟实现搜索二叉树 插入函数实现 插入函数实现&#xff08;递归&#xff09; 查找函数实现 删除函数实现 删除函数实现&#xff08;递归&#xff09; 中序遍历实现 拷贝构造函数实现 析构函数实现 赋值重载 我们在最开始学习二叉树的时候&#xff0c;…

【网络】Internet 协议版本 6 (IPv6)

文章目录IPv6 寻址文本表示形式地址类型IPv6 路由邻居发现IPv6 自动配置自动配置类型IPv6 移动性禁用或启用 IPv6配置步骤代码启用操作系统启用来源Internet 协议版本 6 (IPv6) 是 Internet 的网络层的标准协议套件。 IPv6 旨在解决当前版本的 Internet 协议套件&#xff08;称…

二叉树前中后层遍历(递归/非递归)(简单易懂(*^ー^))

文章目录二叉树的遍历1 先序遍历1.1 递归1.2 非递归2 中序遍历2.1 递归2.2 非递归3 后序遍历3.1 递归3.2 非递归4 层序遍历5 前中后层序完整可运行代码&#xff08;C&#xff09;二叉树的遍历 1 先序遍历 1.1 递归 先序遍历(Preorder Traversal)&#xff0c;即根左右的顺序遍…

Anaconda3安装配置/创建删除虚拟环境/在特定虚拟环境下安装库

1. Anaconda3彻底卸载 先说Anaconda3的卸载&#xff0c;在Anaconda3安装路径下有一个Uninstall-Anaconda3.exe&#xff0c;右键以“管理员身份运行”&#xff0c;可执行完全卸载 2. 下载与安装Anaconda3 官网地址https://repo.anaconda.com/ 点击Anaconda Distribution&…

自学大数据第14天NoSQL~MongoDB及其命令

这几天主要是看了一下mongodb的一些知识,网上也有一些教程,今天主要是复习一下mongodb 启动mongodb 在连接mongodb前首先要创建数据存放目录与日志存放目录,还得保证当前用户对这两个目录有相应的读写操作 mongod --dbpath/usr/local/mongodb/data/db/ --logpath/usr/lcoal/mon…

(四)【软件设计师】计算机系统—基础单位进制

文章目录一、计算机基础单位二、进制1.进制表示符号2.进制之间的转换&#xff1a;(1)十进制转换为二进制&#xff08;例子&#xff1a;173&#xff09;(2)十进制转换为八进制&#xff08;3&#xff09;十进制转换为十六进制&#xff08;4&#xff09;二进制转换为十进制&#x…

Linux入门 - 最常用基础指令汇总

目录 ls指令 pwd指令 cd指令 touch指令 mkdir指令 rmdir指令 && rm 指令 man指令&#xff08;重要&#xff09; cp指令&#xff08;重要&#xff09; mv指令&#xff08;重要&#xff09; cat指令 more指令 less指令&#xff08;重要&#xff09; head指令…

交换机PCB板布局布线注意事项

由于板卡在工作中会受到各种各样的干扰&#xff0c;这些干扰不仅影响系统运行的稳定性&#xff0c;同时也有可能带来误差&#xff0c;因此考虑如何抑制干扰&#xff0c;提高电磁兼容性是PCB布局布线时的一项重要任务。海翎光电的小编现将PCB布局布线中需要主要考虑的因素列在下…

银行数字化转型导师坚鹏:深度解读《中华人民共和国数据安全法》

深度解读《中华人民共和国数据安全法》 ——中国数据安全立法 助力企业稳健发展课程背景&#xff1a; 很多金融机构存在以下问题&#xff1a; 不清楚数据安全法立法背景&#xff1f; 不知道如何理解数据安全法相关政策&#xff1f; 不清楚如何数据安全进行合规建设&#xf…

【前端之旅】Vue入门笔记

一名软件工程专业学生的前端之旅,记录自己对三件套(HTML、CSS、JavaScript)、Jquery、Ajax、Axios、Bootstrap、Node.js、Vue、小程序开发(Uniapp)以及各种UI组件库、前端框架的学习。 【前端之旅】Web基础与开发工具 【前端之旅】手把手教你安装VS Code并附上超实用插件…

计算机组成原理第二章数据的表示与运算(中)

提示&#xff1a;且行且忘且随风&#xff0c;且行且看且从容 文章目录前言2.2.0 奇偶校验码(大纲已删)2.2.1 电路的基本原理 加法器设计2.2.2 并行进位加法器2.2.3 补码加减运算器2.2.4 标志位的生成2.2.5 定点数的移位运算2.2.62.2.6.1 原码的乘法运算2.2.6.2 补码的乘法运算2…

Linux下异步socket客户端

文章目录socket 客户端1. 创建socketsocket()函数返回值2. 设置socket的属性connect函数sockaddr_in结构体inet_pton函数3. fcntl设置非阻塞4. recv函数socket 客户端 1. 创建socket socket()函数 #include <sys/socket.h> int socket(int domain, int type, int proto…