Advanced JAVA - How to understand the common APIs of anonymous inner classes and Arrays classes used as parameters (9)

Table of contents

Table of contents

API                

        ​ ​ 1.0 Description of Arrays class

        1.1 toString() static method in Arrays class

        ​ ​ 1.2 copyOfRange(int[] original, int from, int to) static method in Arrays class

        1.3 copyOf(int[] original, int newLength) static method in Arrays class

        1.4 setAll(double[] array, IntToDoubleFunction generator) static method in Arrays class

        1.5 sort() static method in Arrays class

        1.6 But this sort() method has drawbacks. What if the required arrangement is not a basic data type? (Required to be an object of type Student)


API                

        ​ ​ 1.0 Description of Arrays class

        Arrays class is a class in the Java programming language that provides some static methods to operate arrays. These methods include array sorting, searching, filling and other functions. These static methods can be called directly through the Arrays class without creating Arrays instance. By using the Arrays class methods, we can operate and process arrays more conveniently.

        1.1 toString() Static method in Arrays class

        ThroughtoString() the static method can output all the contents of the array in the form of a character string, and the returned value is the string type .

code show as below:

import java.util.Arrays;

public class ArraysAPI {
    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,5};
        //调用静态方法
        String str = Arrays.toString(arr);
        System.out.println(str);
    }
}

The running results are as follows:

        1.2 copyOfRange(int[] original, int from, int to) static method in Arrays class

        Through this static method, an array whose starting position to the end position can be specified is copied, and an array of the same type is returned.

code show as below:

import java.util.Arrays;

public class ArraysAPI {
    public static void main(String[] args) {
            int[] arr = new int[]{1,2,3,4,5};
            int[] arr1 = Arrays.copyOfRange(arr,0,6);
        System.out.println(Arrays.toString(arr));
    }
}

The running results are as follows:

        It should be noted that the specified range [0,6) includes the front and not the end, and the return is a new array.

        1.3 copyOf(int[] original, int newLength) static method in Arrays class

        This method can copy an array and specify the length of the new array.

code show as below:

import java.util.Arrays;

public class ArraysAPI {
    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,5};
        int[] arr1 = Arrays.copyOf(arr,3);
        System.out.println(Arrays.toString(arr1));
    }
}

code show as below:

        1.4 setAll(double[] array, IntToDoubleFunction generator) static method in Arrays class

        After modifying the data in the array and then returning the original array, this static method does not create a new array. This method has a total of two parameters. The first parameter is the array name, and the target data is passed in. The second parameter is an anonymous inner class, and the methods inside need to be overridden.

setAll(double[] array, IntToDoubleFunction generator) The situation inside the static method

Specifically, let’s take a look at the parameters passed in this static method:

        It can be simply understood that when an anonymous inner class is used as a parameter, the abstract method of the parent class and interface needs to be rewritten, so it can be considered that passing in the anonymous inner class is passing a custom method.

code show as below:

import java.util.Arrays;
import java.util.function.IntToDoubleFunction;

public class ArraysAPI {

    public static void main(String[] args) {
      double[] arr = new double[]{1.0, 2, 9, 4, 5};
        Arrays.setAll(arr,new IntToDoubleFunction(){
            @Override
            public double applyAsDouble(int value) {
                return arr[value] * 0.8;
            }
        });
        System.out.println(Arrays.toString(arr));
    }
}

operation result:

        1.5 sort() Static method in Arrays class

        Using this method, arrays of basic types can be arranged in ascending order. This method does not create a new object, but sorts the original array directly. It should be noted that the sort() method directly modifies the order of the original array instead of creating a new sorted array. Therefore, be careful when using the sort() method to avoid accidentally modifying the order of the original array. If you need to preserve the original array, you can first create a copy of the array and then sort the copy array.

code show as below:

import java.util.Arrays;

public class ArraysAPI {
    public static void main(String[] args) {
        int[] arr = new int[]{5,4,3,2,1};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));

    }
}

The running results are as follows:

        1.6 But this sort() method has drawbacks. What if the required arrangement is not a basic data type? (The requirement is an object of type Student )

        Let’s first introduce a way to solve this problem.

        Pass a related anonymous inner class through the sort() static methodnew Comparator<Student> ;(){Overridden method}  , can be simply understood as, to customize a comparison using the content specified in the object, you need to override the method by passing in the anonymous inner class Override your own defined method.

code show as below:

Defined Student 类;

public class Student {
    private String name;
    private int age;

    public Student() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

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

Arrange students' ages in ascending order;

public class ArraysAPI {

    public static void main(String[] args) {
        Student[] students = new Student[4];
        students[0] = new Student("张三",32);
        students[1] = new Student("李四",21);
        students[2] = new Student("王五",20);
        students[3] = new Student("赵六",22);

        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1.getAge()> o2.getAge()){
                    return 1;
                } else if (o1.getAge()< o2.getAge()) {
                    return -1;
                }
                return 0;
            }
        });
        System.out.println(Arrays.toString(students));

    }
}

        This allows you to define which object's content is used to compare objects.



Guess you like

Origin blog.csdn.net/Tingfeng__/article/details/133875834