工具类:Arrays类、Math类、System类、Collections类、自定义比较器Comparale和Comparator

Arrays类

概述:

Arrays包含操作数组的各种方法,其所有方法均为静态方法。

  • Arrays没有看到构造方法,我们不能创建对象.但是这个类中的所有方法都是静态的.可以使用类名直接调用。

常用的成员方法:

static String toString​(int[] a)

  • 返回指定数组的内容的字符串表示形式。
  • 字符串格式:  【11,22,33,44】

static void sort​(int[] a)

  • 按照数字顺序排列指定的数组。
  • 直接改变数组本身。

示例代码:

public class Demo121 {
    public static void main(String[] args) {
        int[] arr = new int[] {66, 33, 22, 88, 55};

        // 1.static String toString​(int[] a) 返回指定数组的内容的字符串表示形式。
        String str = Arrays.toString(arr); // 将数组中的内容拼接成字符串
        System.out.println("str = " + str); // str = [66, 33, 22, 88, 55]

        // 2.static void sort​(int[] a) 按照数字顺序排列指定的数组。
        Arrays.sort(arr); // 将传入的数组中的数据进行排序(升序)

        String str2 = Arrays.toString(arr);
        System.out.println("排序后的: " + str2); // 排序后的: [22, 33, 55, 66, 88]
    }
}

Math类

作用:

  • Math包含执行基本数字运算的方法.一些数学方法
  • Math类也没有构造方法,都是静态方法,使用类名直接调用。

主要成员方法:

static int abs​(int a)

  • 返回一个 int绝对值。

static double ceil​(double a)

  • 向上取整(取大的整数)。

​​​​​​​static double floor​(double a)

  • 向下取整(取小的整数)。

​​​​​​​static double pow​(double a, double b)

  • a的b次方。

​​​​​​​static long round​(double a)

  • 四舍五入。

System类

常用方法:

public static long currentTimeMillis()

  • 获得当前时间的毫秒值的。
  • 缺点:不能传参,只能是当前时间。
  • 主要作用:用来测试程序运行的时间。

​​​​​​​public static void arraycopy(Object src, int srcPos,Object dest, int destPos, int length)

  • 数组复制。
  • src:原数组
  • srcPos:原数组起始索引。
  • dest:目标数组
  • destPos:目标数组的起始索引。
  • length:要拷贝的元素个数。
  • 只要越界就报错。

​​​​​​​public static void gc() ==> garbage collect 垃圾回收器

  • 通知垃圾回收器回收垃圾对象。
  • 对象没有任何引用变量指向时就会编程垃圾对象。

​​​​​​​public static void exit(int status)

  • 退出JVM,终止程序运行。
  • status:退出状态  (只有0和-1两种状态)
  • 0:正常退出
  • ‐1:异常退出

​​​​​​​public static Properties getProperties()

  • 获得操作系统的属性信息,比如操作系统名称。

Collectons类

常用方法:

public static <T> boolean addAll(Collection<T> c, T... elements)

  • 将数组的元素添加到指定的集合中。

​​​​​​​public static void shuffle(List<?> list)

  • 将集合元素乱序。
  • List是上限,因为Set本来就是无序的。

​​​​​​​public static void reverse(List<?> list)

  • 将集合元素反转。
  • List是上限,因为Set没有顺序。

​​​​​​​public static <T> void sort(List<T> list)

  • 对集合元素进行排序,默认是升序。
  • List是上限,因为Set没有顺序。

String也能排序,排序方式是:

  • 先不论长度,从第一个字符开始,比较ASII码大小。谁的ASII码大,谁的字符串就大。若一样,则往后推。
  • 当字符串前面的都一样,遍历过程中,如果一个字符串没有元素可遍历了,另外一个字符串还有,则还有元素的字符串大。

自定义比较器

有两种方式:

方式1:让自定义类实现Comparable接口,重写compareTo方法:定义排序规则

  • ​​​​​​​public static <T> void sort(List<T> list)
  • 类实现接口。

​​​​​​​方式2:自定义类实现Comparator接口,重写compare方法:定义排序规则

  • public static <T> void sort(List<T> list,Comparator<T> c)
  • 用匿名内部类。

compare方法的调用时机:

  • 不能手动调用,由系统自动调用。
  • 每当对集合元素进行排序时,系统内部会将集合中的元素获取出来调用比较器的该方法。

示例代码:

public class CollectionsDemo03 {
    public static void main(String[] args){
        // 创建集合对象
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("jack",20));
        list.add(new Student("rose",27));
        list.add(new Student("lily",24));
        // 对集合学生进行排序:自定义类实现Comparable接口
        // public static <T extends Comparable> void sort(List<T> list)
        Collections.sort(list);
        // 对集合学生进行排序:使用自定义比较器
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() ‐ o2.getAge();
            }
        });
        for (Student student : list) {
            System.out.println(student);
        }
    }
}
public class Student implements Comparable<Student> {
    private String name;
    private int age;
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public Student() {
    }
    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 +
                '}';
    }
    /*
        用来定义排序规则
        该方法的调用者:集合中的任意一个对象
        方法参数:集合中的另一个对象
     */
    public int compareTo(Student o) {
        // this  和 o 进行比较
        return o.age ‐ this.age;
    }
}

Comparable 与 Comparator 区别:

  1. Comparable 与Comparator都是java的接口,用来定义排序规则。
  2. Comparable 是将排序规则定义在类内部,如果想换一种比较规则,那么就必须修改类内部的代码。
  3. Comparator 是将排序规则定义在类外部,对List排序时必须同时传入数据和比较器。

Comparable 与Comparator选择:

  • 如果排序规则只有一种且规定不变,则推荐使用Comparable接口。
  • 如果排序规则不只一种,则推荐使用Comparator接口。

猜你喜欢

转载自blog.csdn.net/weixin_41630924/article/details/81636381