代码拉取完成,页面将自动刷新
import javax.naming.Name;
import java.util.Arrays;
import java.util.Comparator;
class Student1 {
private String name;
private int age;
private double score;
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public Student1(String name, int age, double score) {
this.name=name;
this.age=age;
this.score=score;
}
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 "Student1{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
class AgeComparator implements Comparator<Student1> {
@Override
public int compare(Student1 o1, Student1 o2) {
return o1.getAge()-o2.getAge();
}
}
class NameComparator implements Comparator<Student1> {
@Override
public int compare(Student1 o1, Student1 o2) {
return o2.getName().compareTo(o1.getName());
}
}
class ScoreComparator implements Comparator<Student1> {
@Override
public int compare(Student1 o1, Student1 o2) {
return (int)( o1.getScore()-o2.getScore());
}
}
public class TestComparator {
public static void main(String[] args) {
Student1[] students=new Student1[3];
students[0]=new Student1("鸢也",20,99.9);
students[1]=new Student1("张三",18,89.8);
students[2]=new Student1("李四",25,78.8);
System.out.println("排序前:" + Arrays.toString(students));
AgeComparator ageComparator=new AgeComparator();
NameComparator nameComparator=new NameComparator();
ScoreComparator scoreComparator=new ScoreComparator();
Arrays.sort(students,scoreComparator);
System.out.println("排序后: " +Arrays.toString(students));
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。