定义学生类,包含学号、姓名、年龄三个属性。为学生定义自然序(按学号升序排序),再为学生定义比较器(按年龄降序排序)。创建5个元素的学生数组,分别按自然序和比较器顺序排序后输出。
时间: 2024-02-13 20:05:00 浏览: 17
好的,我来为您解答。以下是 Java 代码实现:
```java
import java.util.Arrays;
import java.util.Comparator;
class Student implements Comparable<Student> {
private int id;
private String name;
private int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public int compareTo(Student other) {
return Integer.compare(this.id, other.id);
}
public static Comparator<Student> ageComparator = new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return Integer.compare(s2.age, s1.age);
}
};
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class Main {
public static void main(String[] args) {
Student[] students = {
new Student(3, "Alice", 20),
new Student(1, "Bob", 19),
new Student(4, "Charlie", 21),
new Student(2, "David", 18),
new Student(5, "Eve", 22)
};
System.out.println("按自然序排序:");
Arrays.sort(students);
System.out.println(Arrays.toString(students));
System.out.println("按年龄降序排序:");
Arrays.sort(students, Student.ageComparator);
System.out.println(Arrays.toString(students));
}
}
```
程序输出如下:
```
按自然序排序:
[Student{id=1, name='Bob', age=19}, Student{id=2, name='David', age=18}, Student{id=3, name='Alice', age=20}, Student{id=4, name='Charlie', age=21}, Student{id=5, name='Eve', age=22}]
按年龄降序排序:
[Student{id=5, name='Eve', age=22}, Student{id=4, name='Charlie', age=21}, Student{id=3, name='Alice', age=20}, Student{id=1, name='Bob', age=19}, Student{id=2, name='David', age=18}]
```
首先定义了一个 `Student` 类,包含学号、姓名、年龄三个属性,并实现了 `Comparable` 接口,以便按学号升序排序。然后定义了一个 `ageComparator` 比较器,用于按年龄降序排序。最后在 `main` 方法中创建了一个包含 5 个元素的学生数组,先按自然序排序,再按比较器排序,最后输出排序结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)