Java学习day20-从通用队列中认识面向对象

一级目录

二级目录

三级目录

一.前言

对于二叉树存储的代码,我们使用了两个队列,一个存储而二叉树结点,一个存储整数。但是如果二叉树结点的类型产生变化,我们是否会需要重新写一个队列?这样没有体现Jvava面向对象的特点也没有充分利用代码的复用性。实际上,我们只需要一个存储对象的队列就行。
对比代码
昨天
在这里插入图片描述

今天
在这里插入图片描述

在这里我们只使用了CilcleObjectQueue来生成两个队列,分别来存储二叉树结点和整数,但是这里使用的是基本数据类型int的包装类Integer,由于在Java中,所有的类都直接或间接的继承Object类,所以存储对象的队列就相当于是存储对象的地址,可以存储任何类的对象。
关于基本数据类型和包装类的理解
一.包装类概述
Java有8种基本数据类型:整型(byte、short、int、long)、浮点型(float、double)、布尔型boolean、字符型char,相对应地,Java提供了8种包装类Byte、Short、Integer、Long、Float、Double、Boolean、Character。包装类创建对象的方式就跟其他类一样,有了类的特点,就可以调用类中的方法,Java就实现了面向对象的特点。
二.基本数据类型和包装类的转换
在这里插入图片描述

Java为了方便我们使用采用了自动装箱和自动拆箱的机制,这种机制简化了基本类型和包装类型的转换。
如何自动装箱和拆箱

Integer  num1 = 1;		//自动装箱
int num2 = num1;		//自动拆箱

如何创建包装类
1.new关键字

  private final int value;
   public Integer(int value) {
        this.value = value;
     
  

2.valueOf()方法

 public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

在这里插入图片描述

可以看出Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],保存了-128到127的整数,如果我们采用自动装箱的方式,给Integer赋值的范围在-128到127内时,可以直接使用数组里的元素,不用再去new了,从而提高效率。如果不满足的话,使用new创建新对象。

二.代码展示

  /**
     * convert the tree to data arrays,including a cha array and an int array.
     * the results are stored int two member varibles.
     * @see #valuesArray
     * @see #indicesArray
     */
    public void toDataArrayObjectQueue()
    {
        //initialize arrays.
        int tempLength=getNodes();
        valuesArray=new char[tempLength];
        indicesArray=new int[tempLength];
        int i=0;
        //traverse and convert at the same time
        CircleObjectQueue tempQueue = new CircleObjectQueue();
        tempQueue.enqueue(this);
        CircleObjectQueue tempIntQueue=new CircleObjectQueue();
        Integer tempIndexInteger=Integer.valueOf(0);
        tempIntQueue.enqueue(tempIndexInteger);
        BinaryCharTree tempTree=(BinaryCharTree)  tempQueue.dequeue();
        int tempIndex=((Integer) tempIntQueue.dequeue()).intValue();
        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));

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


            }//of if
            tempTree = (BinaryCharTree) tempQueue.dequeue();
            if(tempTree==null)
            {
                break;
            }//of if
            tempIndex=(Integer) ((Integer) tempIntQueue.dequeue()).intValue();

        }//of while



运行结果

The values are :[a, b, c, e, d, f, g]
The indices are :[0, 1, 2, 3, 4, 9, 10]

三.总结

Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的,将每个基本数据类型设计一个对应的类进行代表,这种方式增强了Java面向对象的性质。
如果仅仅有基本数据类型,那么在实际使用时将存在很多的不便,很多地方都需要使用对象而不是基本数据类型。比如,在集合类中,我们是无法将int 、double等类型放进去的,因为集合的容器要求元素是Object类型。而包装类型的存在使得向集合中传入数值成为可能,包装类的存在弥补了基本数据类型的不足,此外,包装类还为基本类型添加了属性和方法,丰富了基本类型的操作。由此可以看出Java面向对象的优点展现出来,即继承性很好的利用起来,增加了代码的复用性。现在对于Java的学习,逐渐深入到源码当中去,对其中java方便我们使用而实现的功能进行学习,从而对Java有更深层的了解,方便以后对框架的认识。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值