1.Arrays

用来操作数组的一个工具类。

API 之 Arrays 为Lambda打基础_升序

public class test {
    public static void main(String[] args) {
        // 示例数组
        int[] arr = {5, 3, 8, 1, 2};
        double[] doubleArr = {5.5, 3.3, 8.8, 1.1, 2.2};

        // 使用toString方法
        System.out.println("数组内容: " + Arrays.toString(arr));

        // 使用copyOfRange方法,包前不包后
        int[] newArr = Arrays.copyOfRange(arr, 1, 4);
        System.out.println("拷贝指定范围的数组: " + Arrays.toString(newArr));

        // 使用copyOf方法,如果新数组比原数组短,就只复制原数组的部分元素
        int[] copiedArr = Arrays.copyOf(arr, 7); // 新数组长度为7
        System.out.println("数组拷贝到新长度: " + Arrays.toString(copiedArr));

        // 使用setAll方法,可以对数组中的每个元素进行操作,这里将每个元素乘以2
        Arrays.setAll(doubleArr, new IntToDoubleFunction() {
            @Override
            public double applyAsDouble(int value) {
                return doubleArr[value] * 2 ; // 这里将每个元素乘以2
            }
        });
        System.out.println("数组内容被更改: " + Arrays.toString(doubleArr));

        // 使用sort方法
        Arrays.sort(arr);
        System.out.println("排序后的数组: " + Arrays.toString(arr));
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

API 之 Arrays 为Lambda打基础_数组_02

(2)如果数组中存储的是对象,应该怎么排次序?

API 之 Arrays 为Lambda打基础_数组_03

方法一:

test

public class test {
    public static void main(String[] args) {
        Student[] students = new Student[4];

        students[0] = new Student("蜘蛛精", 169.5, 23);
        students[1] = new Student("紫霞", 163.8, 26);
        students[2] = new Student("紫霞", 163.8, 26);
        students[3] = new Student("至尊宝", 167.5, 24);

        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

Student  要实现Comparable接口,并重写compareTo方法

package com.lzk.test;



public class Student implements Comparable<Student> {
    private String name;
    private double height;
    private int age;

    public Student() {
    }

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

    public String getName() {
        return name;
    }

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

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public int getAge() {
        return age;
    }

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

    @Override
    public int compareTo(Student o) {
        // @约定1:认为左边对象 大于 右边对象 请您返回正整数
        // @约定2:认为左边对象 小于 右边对象 请您返回负整数
        // @约定3:认为左边对象 等于 右边对象 请您一定返回0

        // 按照年龄升序排序。
        if (this.age > o.age) {
            return 1;
        } else if (this.age < o.age) {
            return -1;
        }

        return 0;

        // 或者可以简写为一行代码:
        // return this.age - o.age;
        //降序就反过来
    }
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.

方法二

学生类,不用再实现接口重写代码

//这里不能做方法一的减法,因为返回是int,提供的是double,强转之后198.1-198.0=0.1=0,也会出错。

public class test {
    public static void main(String[] args) {
        Student[] students = new Student[4];

        students[0] = new Student("蜘蛛精", 169.5, 23);
        students[1] = new Student("紫霞", 163.8, 26);
        students[2] = new Student("紫霞", 163.8, 26);
        students[3] = new Student("至尊宝", 167.5, 24);

//        Arrays.sort(students);
//        System.out.println(Arrays.toString(students));

//方法二,先写一个数组,再调用一个接口对象
        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1.getHeight() > o2.getHeight()) {
                } else if (o1.getHeight() < o2.getHeight()) {
                    return -1;
                }
                return 0;
//                return Double.compare(o1.getHeight(), o2.getHeight());升序
//                 return Double.compare(o2.getHeight(), o1.getHeight());降序
                }
            });  
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

默认升序规则是:左边对象大于右边对象返回正整数。