JavaSE第五章 java常用API

5 第五章JavaAPI

是对java预先定义的类或接口, 功能 和方法功能的说明文档,目的是提供给开发人员进行使用帮助说明。

5.1 基本数据类型包装类

Java是一门面向对象的语言,但是其八种基本数据类型却是不面向对象的,这在实际使用中造成了很多不方便,为此引入了包装类的概念。

用途:

  1. 方便涉及到对象时的操作
  2. 包含基本数据类型的相关属性和操作
基本数据类型 包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
character Character
boolean Boolean

5.2 Object

java.lang.Object

方法 作用
String toString() 返回描述该对象值的字符串。在自定义类中应覆盖这个方法
boolean equals(Object otherObject) 比较两个对象是否相等。在自定义类中应覆盖这个方法
Class getClass() int hashCode() 返回包含对象信息的类对象 返回对象的散列码
static wait() static notify() static notifyAll()
转成字符串toString()

返回描述该对象值的字符串

比较equals()

比较对象的地址是否相同 相当于 “==”

返回哈希值hashCode()

返回地址的hash值

5.3 Arrays

方法 作用
static String toString(type[] a) 返回包含a中数据元素的字符串
static void sort(type[] a) 采用优化的快速排序算法对数组进行排序
static void binarySearch(type[] a, type v) 使用二分搜索算法查找值v
static Boolean equals(type[] a, type[] b) 如果两个数字相同,返回true
5.3.1 转成字符串toString()
	public static void main(String[] args) {
   
        int[] a = {
   1,2,3};
        int[] b = {
   1,2,3};

        System.out.println(Arrays.equals(a, b)); //比较值
        System.out.println(a == b);//比较对象(也就是地址)
    }	
5.3.2 比较equals()
	public static void main(String[] args) {
   
        int[] a = {
   1,2,3};
        int[] b = {
   1,2,3};

        System.out.println(Arrays.equals(a, b)); //比较值
        System.out.println(a == b);//比较对象(也就是地址)
    }	
5.3.3 排序 sort()(快速排序)
对普通类型的排序
public class SortDemo {
   
    public static void main(String[] args) {
   
        int[] a = {
   4,7,5,3,9,2,1,8};

//      Arrays.sort(a);//升序排序
        Arrays.sort(a,0,4);// 区间排序
        Integer[] b = {
   4,7,5,3,9,2,1,8};
        Arrays.sort(b); //也可以对Integer包装类数组进行排序
        System.out.println(Arrays.toString(b));
    }
}
对象数组的排序
public class SortDemo2 {
   
    public static void main(String[] args) {
   

        Student s1 = new Student("jim1",101);
        Student s2 = new Student("jim2",102);
        Student s3 = new Student("jim3",103);
        Student s4 = new Student("jim4",104);

        Student[] stuArr = {
   s1,s4,s3,s2};

        Arrays.sort(stuArr); // 对对象数组进行排序 
        /*
        	Student类实现了Comparator接口
        	sort排序调用了Student类中的compare()方法
        	要想用sort对对象数组进行排序,必须先实现ComParactor接口中的compare()方法。
        */

        System.out.println(Arrays.toString(stuArr));
    }
}

public class Student implements Comparator<Student> {
   

    public String name;
    public int id;

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

    public Student() {
   }

    /**
     * 基本数据类型的比较    用Comparator  接口
     * @param o1
     * @param o2
     * @return
     */
    @Override
    public int compare(Student o1, Student o2) {
   
//        return o1.id - o2.id;  // o1 - o2  正序
        return o2.id - o1.id; // 逆序
    }

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

自定义排序

自定义排序 必须实现Comparator接口

用name对Student数组进行排序

对字符串的比较排序会使用到String类实现Comparable接口中的compareTo()方法

/**
*自定义排序  必须实现Comparator接口
*/
public class SortName implements Comparator<Student> {
   

    public SortName() {
   
    }

    @Override
    public int compare(Student o1, Student o2) {
     // sort排序时会调用该函数
        return o2.name.compareTo(o1.name);  
    }
}
public class SortDemo2 {
   
    public static void main(String[] args) {
   

        Student s1 = new Student("jim1",101);
        Student s2 = new Student("jim2",102);
        Student s3 = new Student("jim3",103);
        Student s4 = new Student("jim4",104);

        Student[] stuArr = {
   s1,s4,s3,s2};
		
        //要把你写好的自定义排序类的对象传进去,这样他会自动调用你的自定义排序compare()方法
        Arrays.sort(stuArr,new SortName()); 

        System.out.println(Arrays.toString(stuArr));
    }
}
用id对Student数组进行排序
public class SortId implements Comparator<Student>  {
   
    public SortId() {
   
    }

    @Override
    public int compare(Student o1, Student o2) {
   
        return o1.id - o2.id;  // 正序 
    }
}
public class SortDemo2 {
   
    public static void main(String[] args) {
   

        Student s1 = new Student("jim1",101);
        Student s2 = new Student("jim2",102);
        Student s3 = new Student("jim3",103);
        Student s4 = new Student("jim4",
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值