第七章_常用API(1)_Object&包装类&递归&排序算法&Arrays

前言

一、Math

  • Math类概述:Math包含执行基本数字运算的方法

  • Math中方法的调用方式:Math类中无构造方法,但内部的方法都是静态的,则可以通过类名.方法名进行调用

  • Math类的常用方法

    方法名 方法名说明
    public static int abs(int a)返回参数的绝对值
    public static double ceil(double a)返回大于或等于参数的最小double值,等于一个整数
    public static double floor(double a)返回小于或等于参数的最大double值,等于一个整数
    public static int round(float a)按照四舍五入返回最接近参数的int
    public static int max(int a,int b)返回两个int值中的较大值
    public static int min(int a,int b)返回两个int值中的较小值
    public static double pow (double a,double b)返回a的b次幂的值
    public static double random()返回值为double的正值,[0.0,1.0)

ceil和floor记忆方法,查看英文ceil越来越高,向上取整;floor越来越低,向下取整

在这里插入图片描述

public class MathDemo {
    public static void main(String[] args) {
//        public static int max(int a,int b)	返回两个int值中的较大值
//        int max = Math.max(10, 20);
//        System.out.println(max);

//        public static int min(int a,int b)	返回两个int值中的较小值
//        int min = Math.min(10, 20);
//        System.out.println(min);

//        public static double pow(double a,double b)返回a的b次幂的值
//        double pow = Math.pow(2, 3);
//        System.out.println(pow);

//        public static double random()		返回值为double的正值,[0.0,1.0)
        for (int i = 0; i < 10 ; i++) {
            double random = Math.random();
            System.out.println(random);
        }
    }
}

二、System

  • System类的常用方法

    方法名说明
    public static void exit(int status)终止当前运行的 Java 虚拟机,非零表示异常终止
    public static long currentTimeMillis()返回当前时间(以毫秒为单位)
    arraycopy(源数组,起始索引,目标数组,起始索引,拷贝个数)数组cpoy
public class Test {
    public static void main(String[] args) {
//        public static void exit​(int status)	终止当前运行的 Java 虚拟机,非零表示异常终止
        System.out.println(111);
        System.exit(0);  //当代码执行到这个方法的时候,就表示虚拟机已经停止了
        System.out.println(2222);

//        public static long currentTimeMillis​()  返回当前时间(以毫秒为单位)
        long start = System.currentTimeMillis();//获取当前时间
        //System.out.println(l);
        for (int i = 0; i < 10000; i++) {
            System.out.println(i);
        }
        long end = System.currentTimeMillis();//获取当前时间
        System.out.println(end - start);//472 --- 得到的就是这个for循环运行的时间.

//        arraycopy(数据源数组, 起始索引, 目的地数组, 起始索引, 拷贝个数)	数组copy
        int [] arr1 = {1,2,3,4,5};
        int [] arr2 = new int[10];
        //需求:我要把arr1中的数据拷贝到arr2中.
        System.arraycopy(arr1,0,arr2,0,arr1.length);

        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }

        //我要把arr1最后两个元素,拷贝到arr2的最后两个索引上
        System.arraycopy(arr1,3,arr2,8,2);
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }
    }
}

三、Object

  • Object类概述: Object 是类层次结构的根,每个类都可以将 Object 作为超类(父类);所有类都直接或者间接的继承自该类,换句话说,该类所具备的方法,所有类都会有一份
  • public String toString():返回对象的字符串表示形式。建议所有子类重写该方法,自动生成
public class Student /*extends Object*/{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    //GET...SET...
}
public class Demo {
    public static void main(String[] args) {
        Student s = new Student("张三",23);
        System.out.println(s);//com.itheima.demo1.Student@3f3afe78
        System.out.println(s.toString());
    }
}
  • 通过源码分析:Student未重写toString方法,因为Student当没有手动指定继承的父类时,Student默认继承Object类,所以调用的是Object类的toString方法,打印的就是com.itheima.demo1.Student@3f3afe78

在这里插入图片描述

  • 如果重写了toString方法:打印出来的就不是Object类的toString内容了,而是具体的Student的toString的内容了
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
  • public boolean equals(另一对象):比较对象是否相等;默认比较地址,重写可以比较内容,自动生成
public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
	//GET...SET...

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Demo {
    public static void main(String[] args) {
        Student s1 = new Student("zhangsan",23);
        Student s2 = new Student("zhangsan",23);

        System.out.println(s1 == s2);//false,==比较引用类型的时候比较的是地址值
        //Object类中的equals方法,底层也是用==号比较地址值.
        System.out.println(s1.equals(s2));//false
    }
}
  • equals经典面试题
public class InterviewTest {
    public static void main(String[] args) {
        String s1 = "abc";
        StringBuilder sb = new StringBuilder("abc");
        //1.此时调用的是String类中的equals方法.
        //保证参数也是字符串,否则不会比较属性值而直接返回false
        System.out.println(s1.equals(sb));//false

        //StringBuilder类中是没有重写equals方法,用的就是Object类中的.
        System.out.println(sb.equals(s1));//false
    }
}

四、Objects

  • 常用方法

    方法名说明
    public static String toString(对象)返回参数中对象的字符串表示形式。
    public static String toString(对象, 默认字符串)返回对象的字符串表示形式。
    public static Boolean isNull(对象)判断对象是否为空
    public static Boolean nonNull(对象)判断对象是否不为空
  • 测试代码

public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
	//GET...SET...

    @Override
    public String toString() {
        //System.out.println("看看我执行了吗?");
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Demo1 {
    public static void main(String[] args) {
//        public static String toString(对象)  		 返回参数中对象的字符串表示形式。
        Student s = new Student("小罗同学", 50);
        String result = Objects.toString(s);
        System.out.println(result);
        System.out.println(s);
        
//        public static String toString(对象, 默认字符串)  返回对象的字符串表示形式。如果对象为空,那么返回第二个参数.
        //Student s1 = new Student("小花同学",23);
        Student s1 = null;
        String result1 = Objects.toString(s1, "随便写一个");
        System.out.println(result1);

//        public static Boolean isNull(对象)		 判断对象是否为空
//        Student s2 = null;
        Student s2 = new Student();
        boolean result2 = Objects.isNull(s2);
        System.out.println(result2);


//        public static Boolean nonNull(对象)		 判断对象是否不为空
//        Student s3 = new Student();
        Student s3 = null;
        boolean result3 = Objects.nonNull(s3);
        System.out.println(result3);
    }
}

五、BigDecimal

  • 作用:可以用来进行精确计算

  • 构造方法

    方法名说明
    BigDecimal(double val)参数为double
    BigDecimal(String val)参数为String
import java.math.BigDecimal;

public class MyBigDecimalDemo2 {
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal(10.0);
        BigDecimal bd2 = new BigDecimal("0.3");

        System.out.println(bd1);
        System.out.println(bd2);
    }
}
  • 常用方法:如果想要进行精确运算,那么请使用字符串的构造

    方法名说明
    public BigDecimal add(另一个BigDecimal对象)加法
    public BigDecimal subtract (另一个BigDecimal对象)减法
    public BigDecimal multiply (另一个BigDecimal对象)乘法
    public BigDecimal divide (另一个BigDecimal对象)除法
    public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式)除法
import java.math.BigDecimal;

public class MyBigDecimalDemo3 {
    //如果想要进行精确运算,那么请使用字符串的构造
    public static void main(String[] args) {
//        BigDecimal bd1 = new BigDecimal(0.1);
//        BigDecimal bd2 = new BigDecimal(0.2);
        BigDecimal bd1 = new BigDecimal("0.1");
        BigDecimal bd2 = new BigDecimal("0.2");
//        public BigDecimal add(另一个BigDecimal对象) 	加法
        BigDecimal add = bd1.add(bd2);
        System.out.println("和为" + add);
        //System.out.println(0.1 + 0.2);

//        public BigDecimal subtract (另一个BigDecimal对象)  减法
        BigDecimal subtract = bd1.subtract(bd2);
        System.out.println("差为" + subtract);

//        public BigDecimal multiply (另一个BigDecimal对象)  乘法
        BigDecimal multiply = bd1.multiply(bd2);
        System.out.println("积为" + multiply);
//        public BigDecimal divide (另一个BigDecimal对象)    除法
        BigDecimal divide = bd1.divide(bd2);
        System.out.println("商为"+divide);

    }
}
  • BigDecimal总结
    • BigDecimal是用来进行精确计算的
    • 创建BigDecimal的对象,构造方法使用参数类型为字符串的
    • 四则运算中的除法,如果除不尽请使用divide的三个参数的方法
import java.math.BigDecimal;

public class MyBigDecimalDemo4 {
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal("0.3");
        BigDecimal bd2 = new BigDecimal("4"); //0.075

       /* BigDecimal divide = bd1.divide(bd2);
        System.out.println(divide);*/

       //参数一:表示参数运算的另一个对象
       //参数二:表示小数点后精确到多少位
       //参数三:舍入模式
                //进一法  BigDecimal.ROUND_UP
                //去尾法  BigDecimal.ROUND_FLOOR
                //四舍五入 BigDecimal.ROUND_HALF_UP
        BigDecimal divide = bd1.divide(bd2, 2, BigDecimal.ROUND_HALF_UP);
        System.out.println(divide);
    }
}

六、包装类

  • 基本类型包装类:将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据
    • 常用的操作之一:用于基本数据类型与字符串之间的转换
public class MyIntegerDemo1 {
    public static void main(String[] args) {
        //需求:我要判断一个整数是否在 int 范围内?
        //Integer
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
    }
}
  • 基本类型对应的包装类

    基本数据类型包装类
    byteByte
    shortShort
    intInteger
    longLong
    floatFloat
    doubleDouble
    charCharacter
    booleanBoolean
  • Integer类构造方法

    方法名说明
    public Integer(int value)根据 int 值创建 Integer 对象(过时)
    public Integer(String s)根据 String 值创建 Integer 对象(过时)
    public static Integer valueOf(int i)返回表示指定的 int 值的 Integer 实例
    public static Integer valueOf(String s)返回一个保存指定值的 Integer 对象 String
public class MyIntegerDemo2 {
    public static void main(String[] args) {
        Integer i1 = new Integer(100);
        Integer i2 = new Integer("100");
        System.out.println(i1);
        System.out.println(i2);
        
        Integer i3 = Integer.valueOf(200);
        Integer i4 = Integer.valueOf("200");
        System.out.println(i3);
        System.out.println(i4);
    }
}
  • 装箱和拆箱:JDK1.5特性
    • 自动装箱:把基本数据类型转换为对应的包装类类型
    • 自动拆箱:把包装类类型转换为对应的基本数据类型
public class MyIntegerDemo3 {
    public static void main(String[] args) {
        Integer i1 = 100;
        //   对象      = 默认是一个基本数据类型
        //jdk1.5的特性 --- 自动装箱
        //装箱: 把一个基本数据类型 变量对应的包装类.
        //自动: Java底层会帮我们自动的调用valueof方法.
        System.out.println(i1);

        //jdk1.5的特性 --- 自动拆箱
        //拆箱: 把一个包装类型 变成对应的基本数据类型
        int i2 = i1;
        System.out.println(i2);

        Integer i3 = 100; //自动装箱机制
        i3 += 200;//i3 = i3 + 200;
                   //会把i3这个对象变成基本数据类型100.
                    //100 + 200 = 300
                //把基本数据类型300再次自动装箱变成Integer对象赋值给i3
        System.out.println(i3);

        //细节:
        Integer i4 = null;
        if(i4 != null){
            i4 += 200;
            System.out.println(i4);
        }
    }
}
  • Integer类型转换parseInt
public class MyIntegerDemo4 {
    public static void main(String[] args) {
        String s1 = "100";
        int i1 = 200;
        System.out.println(s1 + i1);//100200 --- 字符串+任意的数据类型 结果都是一个字符串
        int i2 = Integer.parseInt(s1);//可以将字符串类型的整数变成int类型的整数
        System.out.println(i2 + i1);

        //int  ---> String
        //方式一: +""
        int i3 = 100;
        String s2 = i3 + "";
        System.out.println(s2 + 100);
        //方式二: 可以调用String类中valueof方法
        String s3 = String.valueOf(i3);
        System.out.println(s3 + 100);
    }
}
  • 案例:Integer应用
public class MyIntegerDemo5 {
    //需求:有一个字符串:“91 27 46 38 50”,把其中的每一个数存到int类型的数组中
    //步骤:
    //定义一个字符串
    //把字符串中的数字数据存储到一个int类型的数组中
    //遍历数组输出结果
    public static void main(String[] args) {
        String s = "91 27 46 38 50";
        //获取字符串中的每一个数字.
        String[] strArr = s.split(" ");

        //创建一个int类型的数组.
        int [] numberArr = new int[strArr.length];

        //把strArr中的数据进行类型转换并存入到int数组中
        for (int i = 0; i < strArr.length; i++) {
            int number = Integer.parseInt(strArr[i]);
            numberArr[i] = number;
        }
        //遍历int类型的数组
        for (int i = 0; i < numberArr.length; i++) {
            System.out.println(numberArr[i]);
        }
    }
}

七、递归

  • 递归的介绍
    • 以编程的角度来看,递归指的是方法定义中调用方法本身的现象
    • 把一个复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解
    • 递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算
  • 递归的注意事项
    • 递归一定要有出口。否则内存溢出
    • 递归虽然有出口,但是递归的次数也不宜过多。否则内存溢出
public class MyFactorialDemo2 {
    public static void main(String[] args) {
        int sum = getSum(100);
        System.out.println(sum);
    }
    private static int getSum(int i) {
        //1- 100之间的和
            //100 + (1-99之间的和)
                    // 99 + (1- 98之间的和)
                        //....
                            //1
        //方法的作用: 求 1- i 之间和
        if(i == 1){
            return 1;
        }else{
            return i + getSum(i -1);
        }
    }
}
  • 案例:递归实现求阶乘
  • 内存图
    在这里插入图片描述
public class MyFactorialDemo3 {
    public static void main(String[] args) {
        int result = getJc(5);
        System.out.println(result);
    }

    private static int getJc(int i) {
        //1,就是一定要找到出口.
        if(i == 1){
            return 1;
        }else{
            //2.就是递归的规则
            return i * getJc(i - 1);
       }
    }
}

八、数组的高级操作

1、二分查找算法

  • 前提条件:数组内的元素一定要按照大小顺序排列,如果没有大小顺序,是不能使用二分查找法的
public class MyBinarySearchDemo {
    public static void main(String[] args) {
        int [] arr = {1,2,3,4,5,6,7,8,9,10};
        int number = 3;
        int index = binarySearchForIndex(arr,number);
        System.out.println(index);
    }
    private static int binarySearchForIndex(int[] arr, int number) {
        //1,定义查找的范围
        int min = 0;
        int max = arr.length - 1;
        //2.循环查找 min <= max
        while(min <= max){
            //3.计算出中间位置 mid
            int mid = (min + max) >> 1;
            //mid指向的元素 > number
            if(arr[mid] > number){
                //表示要查找的元素在左边.
                max = mid -1;
            }else if(arr[mid] < number){
                //mid指向的元素 < number
                //表示要查找的元素在右边.
                min = mid + 1;
            }else{
                //mid指向的元素 == number
                return mid;
            }
        }
        //如果min大于了max就表示元素不存在,返回-1.
        return -1;
    }
}

2、冒泡排序算法

  • 冒泡排序概述
    • 一种排序的方式,对要进行排序的数据中相邻的数据进行两两比较,将较大的数据放在后面,依次对所有的数据进行操作,直至所有数据按要求完成排序
    • 如果有n个数据进行排序,总共需要比较n-1次;每一次比较完毕,下一次的比较就会少一个数据参与
public class MyBubbleSortDemo2 {
    public static void main(String[] args) {
        int[] arr = {3, 5, 2, 1, 4};
        //1 2 3 4 5
        bubbleSort(arr);
    }
    private static void bubbleSort(int[] arr) {
        //外层循环控制的是次数 比数组的长度少一次.
        for (int i = 0; i < arr.length -1; i++) {
            //内存循环就是实际循环比较的
            //-1 是为了让数组不要越界
            //-i 每一轮结束之后,我们就会少比一个数字.
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        printArr(arr);
    }
    private static void printArr(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}

3、快速排序算法

  • 快速排序概述
    • 冒泡排序算法中,一次循环结束,就相当于确定了当前的最大值,也能确定最大值在数组中应存入的位置
    • 快速排序算法中,每一次递归时以第一个数为基准数,找到数组中所有比基准数小的。再找到所有比基准数大的,小的全部放左边,大的全部放右边,确定基准数的正确位置
  • 快速排序核心步骤
    • a)从右开始找比基准数小的
    • b)从左开始找比基准数大的
    • c)交换两个值的位置
    • d)红色继续往左找,蓝色继续往右找,直到两个箭头指向同一个索引为止
    • e)基准数归位
public class MyQuiteSortDemo2 {
    public static void main(String[] args) {
//        1,从右开始找比基准数小的
//        2,从左开始找比基准数大的
//        3,交换两个值的位置
//        4,红色继续往左找,蓝色继续往右找,直到两个箭头指向同一个索引为止
//        5,基准数归位
        int[] arr = {6, 1, 2, 7, 9, 3, 4, 5, 10, 8};
        quiteSort(arr,0,arr.length-1);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    private static void quiteSort(int[] arr, int left, int right) {
        if(right < left){
            return;
        }
        int left0 = left;
        int right0 = right;
        //计算出基准数
        int baseNumber = arr[left0];
        while(left != right){
//        1,从右开始找比基准数小的
            while(arr[right] >= baseNumber && right > left){
                right--;
            }
//        2,从左开始找比基准数大的
            while(arr[left] <= baseNumber && right > left){
                left++;
            }
//        3,交换两个值的位置
            int temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
        }
        //基准数归位
        int temp = arr[left];
        arr[left] = arr[left0];
        arr[left0] = temp;
        quiteSort(arr,left0,left-1);
        quiteSort(arr,left +1,right0);
    }
}

九、Arrays

  • Arrays的常用方法

    方法名说明
    public static String toString(int[] a)返回指定数组的内容的字符串表示形式
    public static void sort(int[] a)按照数字顺序排列指定的数组
    public static int binarySearch(int[] a, int key)利用二分查找返回指定元素的索引
  public class MyArraysDemo {
        public static void main(String[] args) {
    //        public static String toString(int[] a)    返回指定数组的内容的字符串表示形式
    //        int [] arr = {3,2,4,6,7};
    //        System.out.println(Arrays.toString(arr));

    //        public static void sort(int[] a)	  按照数字顺序排列指定的数组
    //        int [] arr = {3,2,4,6,7};
    //        Arrays.sort(arr);
    //        System.out.println(Arrays.toString(arr));

    //        public static int binarySearch(int[] a, int key) 利用二分查找返回指定元素的索引
            int [] arr = {1,2,3,4,5,6,7,8,9,10};
            int index = Arrays.binarySearch(arr, 0);
            System.out.println(index);
            //1,数组必须有序
            //2.如果要查找的元素存在,那么返回的是这个元素实际的索引
            //3.如果要查找的元素不存在,那么返回的是 (-插入点-1)
                //插入点:如果这个元素在数组中,他应该在哪个索引上.
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无休止符

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值