Object array sorting
- 1. How to output the contents of an object array?
- 2. How to sort objects in an array
1. How to output the contents of the object array?
You need to override the toString method in the object’s class
//Rewrite method @Override public String toString() {<!-- --> ret![Please add image description](https://img-blog.csdnimg.cn/503f7f999cd845a7bae729fff4ba1699.png) rn "test_Student{" + "name='" + name + '\'' + ", height=" + height + ", age=" + age + '}'; }
Then use Arrays.toString(students) to output in main
//Arrays.toString(students) outputs the address of the object array // If you want to output content, you need to override the toString method in the array class Arrays.sort(students); System.out.println(Arrays.toString(students));
2. How to sort objects in an array
- 1.Method 1
Let the object’s class implement the Comparable (comparison rules) interface, then override the compareTo method and specify the comparison rules yourself.
- ArraysTest class
package com.API1; import java.util.Arrays; public class test_ArraysTest2 {<!-- --> public static void main(String[] args) {<!-- --> //Goal: Master how to sort objects in an array test_Student[] students = new test_Student[3]; students[0]=new test_Student("Li Lei",169.5,21); students[2]=new test_Student("Xu Song",182,32); students[1]=new test_Student("Zhang Wanyi",181.9,27); Arrays.sort(students); //Arrays.toString(students) outputs the address of the object array // If you want to output content, you need to override the toString method in the array class System.out.println(Arrays.toString(students)); } }
- Student class
After sorting, if Arrays.toString(students) is output in the ArraysTest class, and the string type of each address comes out, you need to override the toString method in the Student class to get the content string of the object array.
package com.API1; //Generic interface public class test_Student implements Comparable<test_Student>{<!-- --> private String name; private double height; private int age; //Write the comparison method in this overridden method @Override public int compareTo(test_Student o) {<!-- --> //Convention 1: Left object > Right object ==》Return any positive integer //Convention 2: Left object <Right object==》Return any negative integer //Convention 3: Left object = Right object ==》must return 0 //First form // if(this.age > o.getAge()) // return 1; // else if(this.age < o.getAge()) // return -1; //else // return 0; //Second form //return this.age-o.getAge();//Ascending order return o.getAge()-this.age;//descending order } //Override method @Override public String toString() {<!-- --> return "test_Student{" + "name='" + name + '\'' + ", height=" + height + ", age=" + age + '}'; } public test_Student() {<!-- --> } public test_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; } }
- 2.Method 2
Use the following sort method to create an anonymous inner class object of the Comparator interface, and then formulate your own comparison rules.
Use: public static void sort(T[] arr,Comparator super T> c); the second parameter is an anonymous inner class, compare is the sampling method in this inner class, override this method, the function of this method is : Compare the contents in the object array pairwise.
Arraystest class
package com.API1; import java.lang.reflect.Array; import java.util.Arrays; import java.util.Comparator; public class test_ArraysTest2 {<!-- --> public static void main(String[] args) {<!-- --> //Goal: Master how to sort objects in an array test_Student[] students = new test_Student[3]; students[0]=new test_Student("Li Lei",169.5,21); students[2]=new test_Student("Xu Song",182,32); students[1]=new test_Student("Zhang Wanyi",181.9,27); //Arrays.sort(students); //Arrays.toString(students) outputs the address of the object array // If you want to output content, you need to override the toString method in the array class //public static <T> void sort(T[] arr,Comparator<? super T> c) //Parameter 1: Array to be sorted //Parameter two: Comparator object (used to formulate comparison rules for objects) // comparator is an interface class, and interfaces cannot define objects, so the second parameter is an anonymous inner class (essentially a class, and a subclass object will be created immediately), compare is an abstract method of the anonymous inner class Arrays.sort(students, new Comparator<test_Student>() {<!-- --> @Override public int compare(test_Student o1, test_Student o2) {<!-- --> //Comparison rules: left object o1 right object o2 //Convention 1: It is considered that the object on the left is greater than the object on the right. Please return a positive integer. //Convention 2: It is considered that the object on the left is smaller than the object on the right. Please return a negative integer. //Convention 3: The object on the left is considered to be equal to the object on the right. Please return 0. //Because this return value is of int type and the comparison is of double type, return o1.getHeight() - o2.getHeight(); cannot be used anymore. //the first method if(o1.getHeight() > o2.getHeight()) return 1; else if(o1.getHeight()<o2.getHeight()) return -1; else return 0; //The second method: has the same effect as the first one //return Double.compare(o1.getHeight(),o2.getHeight()); } }); System.out.println(Arrays.toString(students)); } }
- operation result
Double.compare(o1.getHeight(),o2.getHeight()); compares the sizes of these two double parameters. If the former is larger than the latter, a positive number is returned; if the former is smaller than the latter, a negative number is returned; and if the former is smaller than the last, a negative number is returned; and 0 is returned.