常用API

常用API

Math 类

在java.lang.Math包 public final class Math extends Object 继承于object类
包含基本数学运算方法,如指数,对数,平方根和三角函数等.
Math类中没有构造方法, 怎么使用类中的成员呢? -----> 看类的成员是否是静态的(static), 如果是通过类名就可以直接调用.

        System.out.println(Math.abs(-5.123));//绝对值
        System.out.println(Math.round(19.499f));//四舍五入
        System.out.println(Math.pow(2,3));//乘方
        System.out.println(Math.random()*10);//随机数

System 类

在java.lang.Math包 public final class System extends Object 继承于object类 也是类名调用

public class SystemDemo {
    public static void main(String[] args) {
        System.out.println("start");
        System.exit(0);       //结束java虚拟机,非0为异常终止.
        System.out.println("end");
        //--------------------------------
        System.out.println(System.currentTimeMillis()*1.0/1000/60/60/24/365+"年");//毫秒转年
        long start = System.currentTimeMillis();
        for(int i = 0; i<10000; i++){
            System.out.println(i);
        }
        long end = System.currentTimeMillis();
        System.out.println("Time:"+(end-start)+"Millis");//完成10000次循环用时多少.
    }
}

Object 类

Object类是所有类的父类,所有类都直接或间接的继承于它.所有对象(包括数组)都实现了这个类的方法.
只有一个无参构造方法, 为什么子类的构造方法默认访问的是父类的无参构造方法? 因为他们的顶级父类只有无参构造方法.
查看源码-->选中方法, 按CTRL+b \ ctrl+左键 .

toString方法

返回对象的字符串表现形式.建议所有子类覆盖(重写)次方法.

//在没重写toString方法前
  Student student = new Student();
        student.setName("Peppa");
        student.setAge("9");
        System.out.println(student.toString());
//控制台输出: StudentManager.Student@3b07d329
//在Student类中重写toString方法后,控制台输出: Student[sid='null', name='Peppa', age='9', address='null']
   @Override
    public String toString() {
        return "Student{" +
                "sid='" + sid + '\'' +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

equals方法

比较两个对象的内容是否相同.

 public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();
        s1.setName("Peppa");
        s1.setAge("9");
       s2.setName("Peppa");
       s2.setAge("9");
        System.out.println(s1.equals(s2));
//----------------------------------
//在Student类重写equals方法
  @Override
    public boolean equals(Object o) {
        /*
                这个方法默认比较的是地址, 我们重写为比较内容.
                this --- s1
                o    --- s2
         */
        if (this == o) return true;//比较地址值,如果相同返回true
        //再判断o是否为null, 或这两对象是否是同一个类, 就返回false.
        if (o == null || getClass() != o.getClass()) return false;
        //向下转型
        Student student = (Student) o;//student-->s2
        //比较sid,name,age是否相同.
        if (sid != null ? !sid.equals(student.sid) : student.sid != null) return false;
        if (name != null ? !name.equals(student.name) : student.name != null) return false;
        if (age != null ? !age.equals(student.age) : student.age != null) return false;
        //最后比较address,如果不是null就比较内容,是null就看它们是否都是null.
        return address != null ? address.equals(student.address) : student.address == null;
    }
    }

Arrays 类

在Java.util包, 该类包含操作数组的各种方法,如排序,搜索等...

冒泡排序

public class Bubble {       
    public static void main(String[] args) {
        int []arr = {3,2,1,0,-1,-2};         //声明数组
        System.out.print("排序前:");
         printArr(arr);
     //冒泡排序基本格式
        for(int i = 0 ; i < arr.length-1; i++){     //一共(n-1)外层循环
            for(int j = 0 ; j < arr.length-i-1  ; j++){     //(n-i-1)次内层循环
                if(arr[j]>arr[j+1]){        //如果索引j大于j+1,
                int temp = arr[j];         //声明temp临时变量
                arr[j] = arr[j+1];          //[i]和[i+1]位置数据交换.
                arr[j+1] = temp;
                }
            }
        }
        System.out.print("排序后:");
        printArr(arr);
    }
    //遍历数组方法
    public static void printArr(int [] arr){
        //[1, 2, 3]格式遍历数组
        System.out.print("[");
        for(int i = 0 ; i < arr.length; i++){
            if(i==arr.length-1){                  //如果是末索引打印arr[i]+"]"
                System.out.print(arr[i]+"]");
            }else
                System.out.print(arr[i]+" , ");     //否则正常打印arr[i]+","
        }
        System.out.println();
    }
}

posted @ 2023-02-08 10:43  大宝贝94106  阅读(36)  评论(0)    收藏  举报