常用类API

API(应用程序编程接口):是对java预先定义的类或接口功能和函数功能的说明文档,目的是提供给开发人员进行使用帮助说明。java提供的类的集合

Object类

**Object类:**所有java类的祖先(根基类),所有类都继承Object与实现Object类方法。
为是用extends关键字指明其基类,默认基类为Object类。
提供的方法

toString方法

equals方法

  • Object中的equals默认使用==比较,比较的是对象的地址。
  • JDK提供的一些类,如String,Date等,重写了Object的equals方 法,调用这些类的equals方法, x.equals (y) ,当x和y所引用的对象 是同一类对象且属性内容相等返回 true 否则返回 false。

Arrays类

用于操作数组工具类,定义类常见操作数组的静态方法。

equals方法:

比较两数组对象元素是否相等。
而数组对象中的equals用来判断与另一个数组对象是否相等

sort方法(快排)

作用于数组的所有元素
public static void sort(type[] a)
作用于数组指定范围内的元素
public static void sort(type[] a, int fromIndex(包括), int
toIndex(不包括))
将指定的类型数组所有元素按数字升序进行排序。
自定义对象排序
自定义类实现Comparable接口
重写compareTo方法

binarySearch方法(二分查找)

binarySearch -使用二分搜索算法搜索指定数组

public static int binarySearch(type[] a, type key)
public static int binarySearch(long[] a,int fromIndex,int
toIndex,long key)
● 参数:
a - 要搜索的数组。
key - 要搜索的值。
fromIndex - 要排序的第一个元素的索引(包括)。
toIndex - 要排序的最后一个元素的索引(不包括)。
● 如果key在数组中,则返回搜索值的索引;否则返回负数,表示不存在

copyOf方法

● 数组复制,将指定数组中的元素复制到一个指定长度的新数组中,并返回
新数组
● static int[] copyOf(int[] a, int newLength)

fill方法

● 将指定的int值分配给指定的int数组的每个元素。
● fill(int[] a, int val)

toString() 方法

● public static String toString(type[] a)
● 返回指定数组内容的字符串表示形式。

Arrays类方法示例

import java.util.Arrays;

public class Demo1 {
    public static void main(String[] args) {
        int [] a={1,2,3,4,5};
        int []e={1,2,3,4,5};
        System.out.println(Arrays.equals(a,e));//true
        System.out.println(Arrays.binarySearch(a, 0, 3, 2));
        int [] b= Arrays.copyOf(a,a.length*2);
        Arrays.fill(a,0);
        System.out.println(Arrays.toString(a));
        System.out.println(Arrays.toString(b));
    }
}

构造方法

public class Student implements Comparable<Student>{
    private int id;
    private String name;
    private String sex;

    public Student() {
    }

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


    public int getId() {
        return id;
    }

    /**
     * 设置
     * @param id
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return sex
     */
    public String getSex() {
        return sex;
    }

    /**
     * 设置
     * @param sex
     */
    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Student{id = " + id + ", name = " + name + ", sex = " + sex + "}";
    }

    @Override
    public int compareTo(Student o) {
        //return this.id-o.id;
        return this.name.compareTo(o.name);//调用string类中的compareto方法, //返回int类型

    }
}

测试类

import java.util.Arrays;

public class StudentSort {
    public static void main(String[] args) {
        Student student1=new Student(1001,"jim1","男");
        Student student2=new Student(1002,"jim2","男");
        Student student3=new Student(1003,"jim3","男");
        Student student4=new Student(1004,"jim4","男");
        Student[]students=new Student[4];
        students[0]=student2;
        students[1]=student3;
        students[2]=student4;
        students[3]=student1;
        //对象内属性排序
        Arrays.sort(students);
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i].toString());
        }

    }
}

包装类

String类

StringBuffer类与StringBuilder类

StringBuffer类概述
我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题线程安全的可变字符序列

与String的区别

  • String: 是字符常量,适用于少量的字符串操作的情况
  • StringBuilder: 适用于单线程下在字符缓冲区进行大量操作的情况
  • StringBuffer: 适用多线程下在字符缓冲区进行大量操作的情况

StringBuffer的应用

public class StringBufferDemo1 {
    //String:字符串不可以改变
    //StringBuilder:单线程下字符缓冲区大量操作的情况,可改变的字符串
    //StringBuffer:多线程安全,可改变的字符串
    public static void main(String[] args) {
 /*     String str=" abc ";
        System.out.println(str.trim());//去字符串头尾空白符*/
        StringBuffer sb=new StringBuffer();
      //  System.out.println(sb);
        //添加
        sb.append("abcdefghijklmnopqrstuvwxyz");
        //删除
        System.out.println(sb.delete(0, 1));//要前不要后
        System.out.println(sb.deleteCharAt(0));
        //替换
        System.out.println(sb.replace(0,5,"a"));
        //反转
        String strs="12345";
        StringBuffer str=new StringBuffer();
        str.append(strs);
        System.out.println(str.reverse());
        //截取功能
        //截取功能与前面几个功能不同;
        // 返回值类型为String,本身没有发生变化
        str.reverse();
        System.out.println(str.substring(0));//1
        System.out.println(str.substring(0,2));//1,2
    }

}

StringBuilder类功能和StringBuffer功能完全一致,StringBuffer是线程安全的

Math类

java.lang.Math提供了一系列静态方法用于科学计算;其方法的参数和返回值类型一般为double型。

public class MathDemo1 {
    public static void main(String[] args) {
        //想下取整
        System.out.println(Math.floor(2.2));//2.0
        //向上取整
        System.out.println(Math.ceil(2.2));//3.0
        // 随机数
        System.out.println(Math.random());//0-1
        //四舍五入
        System.out.println(Math.round(2.4));//2
        System.out.println(Math.round(2.5));//3
        //绝对值
        System.out.println(Math.abs(-120));//120
    }
}
public class mathDemo1{
    public static void main(String[] args) {
       System.out.println( Math.abs(-88));//绝对值
       //bug,要在类型范围内例:int :-2147493648-2147483647
       //System.out.println(Math.toIntExact(-331888313));
        System.out.println(Math.ceil(12.34));//向上取整:13.0
        System.out.println(Math.floor(12.34));//x向下取整;12.0
        System.out.println(Math.ceil(-12.34));//向上取整:-12.0
        System.out.println(Math.floor(-12.34));//向下取整:-13.0
        //四舍五入
        System.out.println(Math.round(12.34));//12    
        System.out.println(Math.round(12.54));//13
        System.out.println(Math.round(-12.34));//-12
        System.out.println(Math.round(-12.54));//-13
        System.out.println("---------------------------");
        //获取两个整数最值的方法
        System.out.println(Math.max(12.1,12.3));
        System.out.println(Math.min(12,14));
        System.out.println("---------------------------");
        //次幂
        System.out.println(Math.pow(2, 3));
        System.out.println(Math.pow(4,0.5));//2.0    
        System.out.println(Math.pow(2,-2));//0.25
        //第二个参数:一般传递大于等于一的整数
        System.out.println(Math.sqrt(4));//2.0   平方根
        System.out.println(Math.cbrt(8));//2.0   立方根 
        //随机数
        for (int i = 0; i < 10; i++) {
            System.out.println(Math.floor(Math.random()*100)+1);
            //floor:去掉小数
            //Math.random[0.0,1.0)
        }
        
        
    }
}

Random(随机数)

import java.util.Random;

public class RandomDemo {
    public static void main(String[] args) {
        Random random=new Random();
        random.nextInt(10);//0-9;
        System.out.println(random.nextInt(10));
        System.out.println(Math.random());
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值