java--常用API笔记

  • 什么是API

    API (Application Programming Interface) :应用程序编程接口

  • java中的API

    指的就是 JDK 中提供的各种功能的 Java类,这些类将底层的实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可,我们可以通过帮助文档来学习这些API如何使用。

 

常用API

Math

  • 1、Math类概述

    • Math 包含执行基本数字运算的方法

  • 2、Math中方法的调用方式

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

  • 3、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)

 

 

 

 

 

 

 

 

 

 

 

package Nomal_API.APIdemo;


public class MathDemo {
    public static void main(String[] args) {
//        public static int abs(int a);		//返回参数的绝对值
        int abs = Math.abs(10);
        System.out.println("abs的绝对值为:"+abs);


//        public static double ceil(double a);	//向上取整
        double ceil = Math.ceil(10.1);
        System.out.println("ceil的向上取整:"+ceil);


//        public static double floor(double a);	//向下取整
        double floor = Math.floor(10.9);
        System.out.println("floor的向下取整为:"+floor);


//        public static int round(float a);	//四舍五入
        long round = Math.round(10.1);
        System.out.println("round的四舍五入为:"+round);

        long round1 = Math.round(1.9);
        System.out.println("round1的四舍五入为:"+round1);


//        public static int max(int a,int b);	//返回两个int值中的较大值
        int max = Math.max(10, 20);
        System.out.println("max的最大值为:"+max);


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


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


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

 

System

方法名 说明
public static void exit(int status) 终止当前运行的 Java 虚拟机,非零表示异常终止
public static long currentTimeMillis() 返回当前时间(以毫秒为单位)
package Nomal_API.APIdemo;

public class Systemdemo {
    public static void main(String[] args) {
        // 获取开始的时间节点
        long start = System.currentTimeMillis();
        System.out.println("开始时间为"+start);
        int i;
        for (i = 1; i <= 10000; i++) {
            int c = 0;
            c += i;
        }
        // 获取代码运行结束后的时间节点
        long end = System.currentTimeMillis();
        System.out.println("结束时间为:"+end);
        System.out.println("循环"+i+"共耗时:" + (end - start) + "毫秒");

    }

}

Object类的toString方法、equals方法

  • Object类概述

    • Object 是类层次结构的根,每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类,换句话说,该类所具备的方法,所有类都会有一份

  • 查看方法源码的方式

    • 选中方法,按下Ctrl + B

  • 重写toString方法的方式

      1. Alt + Insert 选择toString

      2. 在类的空白区域,右键 -> Generate -> 选择toString

  • toString方法的作用:

    • 以良好的格式,更方便的展示对象中的属性值

 

  • equals方法的作用

    • 用于对象之间的比较,返回true和false的结果

    • 举例:s1.equals(s2); s1和s2是两个对象

  • 重写equals方法的场景

    • 不希望比较对象的地址值,想要结合对象属性进行比较的时候。

  • 重写equals方法的方式

      1. alt + insert 选择equals() and hashCode(),IntelliJ Default,一路next,finish即可

      1. 在类的空白区域,右键 -> Generate -> 选择equals() and hashCode(),后面的同上。

package Nomal_API.APIdemo;

//让student类继承祖宗类object,不写也是默认继承他
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方法
    public String getName() {
        return name;
    }

    //生成set方法
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //重写object祖宗类的toString方法
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
//重写equals方法,如果不重写equals方法,那么他们的比较是比较地址是否相同,跟 ‘ = = ‘ 是同样的效果
    @Override
    public boolean equals(Object o) {
        //this -- s ,this代表的是调用者
        //o -- s2  , o代表的是与调用者相比较的对象
        if (this == o) return true; // 如果他们地址相等的话,返回TRUE
//        getClass()方法的作用:通过返回的Class对象获取Person的相关信息,比如:获取Person的构造方法,方法,属性有哪些等等信息。

        if (o == null || getClass() != o.getClass()) return false; // 如果被比较者为空,或者他们的类对象不相等,则返回为FALSE
//将o对象赋为student,
        Student student = (Student) o; //student -- s2
//        如果调用者的age不等于被比较者的age,返回为FALSE
        if (age != student.age) return false;
//        三元比较:name不等于null为TRUE-->调用者与被比较者比较,否则被比较者的name也为null
        return name != null ? name.equals(student.name) : student.name == null;
    }
}

public class ObjectDemo {
    public static void main(String[] args) {
        Student s = new Student();
        s.setName("李思思");
        s.setAge(30);
        System.out.println(s);
//        打印重写的toString方法
        System.out.println(s.toString());

        Student s2 = new Student();
        s2.setName("李思思");
        s2.setAge(30);

        //需求:比较两个对象的内容是否相同,重写之后比较他们的内容是否相同,而不是比较地址了
        System.out.println(s.equals(s2));
    }
}

Objects

方法名 说明
public static String toString (对象) 返回参数中对象的字符串表示形式。
public static String toString(对象, 默认字符串) 返回对象的字符串表示形式。
public static Boolean isNull(对象) 判断对象是否为空
public static Boolean nonNull(对象) 判断对象是否不为空
package Nomal_API.APIdemo.objects;

package Nomal_API.APIdemo.objects;

class Student {
    private String name;
    private int age;

    public Student() {
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}



import java.util.Objects;

public class MyObjectsDemo {
    public static void main(String[] args) {
        //        public static String toString(对象): 返回参数中对象的字符串表示形式。
                Student s1 = new Student("小小同学",50);
                String result1 = Objects.toString(s1);
                System.out.println(result1);  // 打印toString方法
                System.out.println(s1);

        //     public static String toString(对象, 默认字符串): 返回对象的字符串表示形式。如果对象为空,那么返回第二个参数.
                Student s2 = new Student("小花同学",23);
                Student s3 = null;
                String result2 = Objects.toString(s3, "随便写一个");
                System.out.println(result2);

        //        public static Boolean isNull(对象): 判断对象是否为空
                Student s4 = null;
                Student s5 = new Student();
                boolean result3 = Objects.isNull(s5);
                System.out.println("对象是否为空:"+result3);

        //        public static Boolean nonNull(对象): 判断对象是否不为空
        Student s6 = new Student();
        Student s7 = null;
        boolean result4 = Objects.nonNull(s6);  // 判断对象是否不为空
        System.out.println("对象是否不为空"+ result4);
    }
}

BigDecimal

  • 作用

    可以用来进行精确计算

  • 构造方法

  • 方法名 说明
    BigDecimal(double val) 参数为double
    BigDecimal(String val) 参数为String
  • 常用方法
  • 方法名 说明
    public BigDecimal add(另一个BigDecimal对象) 加法
    public BigDecimal subtract (另一个BigDecimal对象) 减法
    public BigDecimal multiply (另一个BigDecimal对象) 乘法
    public BigDecimal divide (另一个BigDecimal对象) 除法
    public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式) 除法

总结

  1.  BigDecimal是用来进行精确计算的

  2.  创建BigDecimal的对象,构造方法使用参数类型为字符串的。

  3.  四则运算中的除法,如果除不尽请使用divide的三个参数的方法。

BigDecimal divide = bd1.divide(参与运算的对象,小数点后精确到多少位,舍入模式);
参数1 ,表示参与运算的BigDecimal 对象。
参数2 ,表示小数点后面精确到多少位
参数3 ,舍入模式  
  BigDecimal.ROUND_UP  进一法
  BigDecimal.ROUND_FLOOR 去尾法
  BigDecimal.ROUND_HALF_UP 四舍五入
package Nomal_API.APIdemo.bigdecimal;

import java.math.BigDecimal;

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

        System.out.println("字符串数字"+bd1);
        System.out.println(bd2);

//      BigDecimal bd1 = new BigDecimal(0.1);
//      BigDecimal bd2 = new BigDecimal(0.2);
        BigDecimal bd3 = new BigDecimal("0.1");
        BigDecimal bd4 = new BigDecimal("0.2");
//      public BigDecimal add(另一个BigDecimal对象) 	加法
        BigDecimal add = bd3.add(bd4); // 	加法
        System.out.println("和为" + add);
        //System.out.println(0.1 + 0.2);


//      public BigDecimal subtract (另一个BigDecimal对象)  减法
        BigDecimal subtract = bd3.subtract(bd4);
        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);
    }
}

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);
    }
}

包装类

基本类型包装类(记忆)

  • 基本类型包装类的作用

    将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据

    常用的操作之一:用于基本数据类型与字符串之间的转换

  • 基本类型对应的包装类

  • 基本数据类型 包装类
    byte Byte
    short Short
    int Integer
    long Long
    float Float
    double Double
    char Character
    boolean Boolean

Integer类

包装一个对象中的原始类型 int 的值

方法名 说明
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

自动拆箱和自动装箱

  • 自动装箱

    把基本数据类型转换为对应的包装类类型

  • 自动拆箱

    把包装类类型转换为对应的基本数据类型

       自动拆装箱是JDK1.5新特性,可以实现基本数据类型与对应的包装类之间无缝使用!

int类型如何转为String类型

  • 第一种方式:直接在int类型数据后面加一个空字符串
  • 第二种方式:通过String的静态方法valueOf()

String类型如何转为int类型

  • 第一种方式:先将String转为Integer类型,再通过valueOf()方法转为int
  • 第二种方式:通过Integer静态方法parseInt()
package Nomal_API.APIdemo.basicInterger;

public class IntergerDemo3 {
    public static void main(String[] args) {
        Integer i1 = 100;
        //   对象  = 默认是一个基本数据类型

        //   jdk1.5的特性 --- 自动装箱

        //装箱: 把一个基本数据类型 变量对应的包装类.
        //自动: Java底层会帮我们自动的调用valueof方法.
        System.out.println("自动装箱i1为interger:"+i1);

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

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


        //细节:
        Integer i4 = null;
        if (i4 != null) {
            i4 += 200;
            System.out.println(i4);
        }


    }


}

数组的高级操作-二分查找思路分析

二分查找概述

  查找指定元素在数组中的位置时,以前的方式是通过遍历,逐个获取每个元素,看是否是要查找的元素,这种方式当数组元素较多时,查找的效率很低

  二分查找也叫折半查找,每次可以去掉一半的查找范围,从而提高查找的效率

前提条件

  数组内的元素一定要按照大小顺序排列,如果没有大小顺序,是不能使用二分查找法的

二分查找的实现步骤

  1,定义两个变量,表示要查找的范围。默认min = 0 , max = 最大索引

  2,循环查找,但是min <= max

  3,计算出mid的值

  4,判断mid位置的元素是否为要查找的元素,如果是直接返回对应索引

  5,如果要查找的值在mid的左半边,那么min值不变,max = mid -1.继续下次循环查找

  6,如果要查找的值在mid的右半边,那么max值不变,min = mid + 1.继续下次循环查找

  7,当 min > max 时,表示要查找的元素在数组中不存在,返回-1.

package Nomal_API.APIdemo.MyBinarySearchDemo;


public class MybinarySearchDemo {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int number = 11;

        //1,现在要干嘛? --- 二分查找
        //2.这件事情需要什么? --- 数组 元素
        //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;
    }
}

递归

  • 以编程的角度来看,递归指的是方法定义中调用方法本身的现象

  • 把一个复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解

  • 递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算

package Nomal_API.APIdemo.MyFactorialDemo;
//递归:求阶乘
public class factorialDemo2 {
    public static void main(String[] args) {
        //调用方法
        int result = jc(5);
        //输出结果
        System.out.println("5的阶乘是:" + result);
    }

    //定义一个方法,用于递归求阶乘,参数为一个int类型的变量
    public static int jc(int n) {
        //在方法内部判断该变量的值是否是1
        if (n == 1) {
            //是:返回1
            return 1;
        } else {
            //不是:返回n*(n-1)!
            return n * jc(n - 1);
        }
    }
}

冒泡排序

冒泡排序概述

  一种排序的方式,对要进行排序的数据中相邻的数据进行两两比较,将较大的数据放在后面,依次对所有的数据进行操作,直至所有数据按要求完成排序

  如果有n个数据进行排序,总共需要比较n-1次

  每一次比较完毕,下一次的比较就会少一个数据参与

package Nomal_API.APIdemo.BubbleSortDemo;

//冒泡排序
public class BubbleSort {
    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();
    }

}

Arrays

Arrays的常用方法

方法名 说明
public static String toString(int[] a) 返回指定数组的内容的字符串表示形式
public static void sort(int[] a) 按照数字顺序排列指定的数组
public static int binarySearch(int[] a, int key) 利用二分查找返回指定元素的索引

工具类设计思想

  1. 构造方法用 private 修饰

  2. 成员用 public static 修饰

package Nomal_API.APIdemo.ArrayDemo;


import java.util.Arrays;

public class MyArraysDemo {
    public static void main(String[] args) {
        //        public static String toString(int[] a)    返回指定数组的内容的字符串表示形式
                int [] arr1 = {3,2,4,6,7};
                System.out.println("返回数组的内容字符串:"+Arrays.toString(arr1));

        //        public static void sort(int[] a)	  按照数字顺序排列指定的数组
                int [] arr2 = {3,2,4,6,7};
                Arrays.sort(arr2);
                System.out.println("进行从小到大顺序排序:"+Arrays.toString(arr2));

        //        public static int binarySearch(int[] a, int key) 利用二分查找返回指定元素的索引:key为要查找的值
        int[] arr3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int index = Arrays.binarySearch(arr3, 0);
        System.out.println(index);
        //1,数组必须有序
        //2.如果要查找的元素存在,那么返回的是这个元素实际的索引
        //3.如果要查找的元素不存在,那么返回的是 (-插入点-1)
        //插入点:如果这个元素在数组中,他应该在哪个索引上.
    }
}
posted @ 2022-10-06 23:16  link-零  阅读(150)  评论(0编辑  收藏  举报