当前位置:网站首页>Interface comparator
Interface comparator
2022-07-26 16:55:00 【XLMN】
public class Study04 {
// Define a method to sort all classes , The premise is that classes must be familiar comparable1 Interface
// Use polymorphism , Use interface classes to reference arrays as formal parameters , Receive different argument arrays
public static void sort(Comparable1[] comparable1s) {
for (int i = 1; i < comparable1s.length; i++) {// Control the number of trips
// Operations performed per trip , Start with the first element and compare two adjacent elements , If the sorting conditions are met , Exchange two element values
// Define a tag variable before the start of each trip
boolean flag = true;
for (int j = 0; j < comparable1s.length - i; j++) {
// Meet the conditions for exchange
if (comparable1s[j].compareTo(comparable1s[j + 1]) > 0) {
Comparable1 temp;
temp = comparable1s[j];
comparable1s[j] = comparable1s[j + 1];
comparable1s[j + 1] = temp;
flag = false;// If there's an exchange , Change the value of the tag variable
}
}
if (flag) {
break;
}
}
}
public static void main(String[] args) {
/**
* Interface internal comparator
*/
Student001 st1 = new Student001(" Xiao Huang ", 2, 98);
Student001 st2 = new Student001(" Little green ", 23, 98);
Student001 st3 = new Student001(" Little ice ", 1, 90);
Student001 st4 = new Student001(" petty thief ", 32, 8);
Student001 st5 = new Student001(" Xiao Chen ", 5, 28);
Student001[] stt = {st1, st2, st3, st4, st5};
System.out.println(" Traverse the array before sorting =======");
for (Student001 st : stt
) {
System.out.println(st.toString());
}
sort(stt);
// Traverse the sorted array
System.out.println(" Traverse the sorted array =======");
for (Student001 st : stt
) {
System.out.println(st.toString());
}
}
}
interface Comparable1 {
//Object type , You can pass in several subtypes , Polymorphic usage ;
public int compareTo(Object object);
}
// Create subclasses to implement interfaces
class Student001 implements Comparable1 {
private String name;
private int id;
private double score;
// Define nonparametric construction methods
public Student001() {
}
public Student001(String name, int id, double score) {
super();
this.name = name;
this.id = id;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student001{" +
"name='" + name + '\'' +
", id=" + id +
", score=" + score +
'}';
}
@Override
public int compareTo(Object object) {
Student001 student001 = (Student001) object;
int result = this.id - student001.id;
return result;
}
}
External comparator
public class Student05 {
public static void sort(Object[] objects, Comparator01 comparator01) {
for (int i = 1; i < objects.length; i++) {// Control the number of trips
// Operations performed per trip , Start with the first element and compare two adjacent elements , If the sorting conditions are met , Exchange two element values
// Define a tag variable before the start of each trip
boolean flag = true;
for (int j = 0; j < objects.length - i; j++) {
// Meet the conditions for exchange
if (comparator01.compare(objects[j], objects[j + 1]) > 0) {
Object temp;
temp = objects[j];
objects[j] = objects[j + 1];
objects[j + 1] = temp;
flag = false;// If there's an exchange , Change the value of the tag variable
}
}
if (flag) {
break;
}
}
}
public static void main(String[] args) {
/**
* Interface internal comparator
*/
Student002 st1 = new Student002(" Xiao Huang ", 2, 98);
Student002 st2 = new Student002(" Little green ", 23, 98);
Student002 st3 = new Student002(" Little ice ", 1, 90);
Student002 st4 = new Student002(" petty thief ", 32, 8);
Student002 st5 = new Student002(" Xiao Chen ", 5, 28);
Student002[] stt = {st1, st2, st3, st4, st5};
// Define an external comparator object
StuIdComp stuIdComp = new StuIdComp();
System.out.println(" Traverse the array before sorting =======");
for (Student002 st : stt
) {
System.out.println(st.toString());
}
sort(stt, stuIdComp);
// Traverse the sorted array
System.out.println(" Traverse the sorted array =======");
for (Student002 st : stt
) {
System.out.println(st.toString());
}
}
}
// Define an external comparator
interface Comparator01 {
// Formal parameters are Object type , With polymorphism
public int compare(Object obj1, Object Obj2);
}
class StuIdComp implements Comparator01 {
@Override
public int compare(Object obj1, Object Obj2) {
// Cast
Student002 st = (Student002) obj1;
Student002 st01 = (Student002) Obj2;
int result = st.getId() - st01.getId();
return result;
}
}
// Define the student class
class Student002 {
private String name;
private int id;
private double score;
public Student002() {
}
public Student002(String name, int id, double score) {
super();
this.name = name;
this.id = id;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student002{" +
"name='" + name + '\'' +
", id=" + id +
", score=" + score +
'}';
}
}
边栏推荐
- Response对象-响应字符数据
- 广东首例!广州一公司未履行数据安全保护义务被警方处罚
- Digital currency of quantitative transactions - merge transaction by transaction data through timestamp and direction (large order consolidation)
- Sharing of 40 completed projects of high-quality information management specialty [source code + Thesis] (VI)
- 公共数据如何兼顾开放利用和隐私安全合规?
- Guangzhou Municipal Safety Committee Office issued warnings and reminders on safety precautions in hot weather
- Probe of kubernetes
- 极大似然估计
- Recurrence of historical loopholes in ThinkPHP
- “青出于蓝胜于蓝”,为何藏宝计划(TPC)是持币生息最后的一朵白莲花
猜你喜欢

数字化转型怎么就那么的难?!

My SQL is OK. Why is it still so slow? MySQL locking rules

How to implement Devops with automation tools | including low code and Devops application practice

如何保证缓存和数据库一致性
Final consistency distributed transaction TCC

Win11系统如何一键进行重装?

Difference between C event and delegation

Matlab论文插图绘制模板第40期—带偏移扇区的饼图

NUC 11构建 ESXi 7.0.3f安装网卡驱动-V2(2022年7月升级版)

Marxan model, reserve optimization and protection vacancy selection technology, application in invest ecosystem
随机推荐
kubernetes之ConfigMap
How does win11 automatically clean the recycle bin?
Win11 auto delete file setting method
My SQL is OK. Why is it still so slow? MySQL locking rules
限流对比:Sentinel vs Hystrix 到底怎么选?
【开发教程9】疯壳·ARM功能手机-I2C教程
JS API summary of Array Operations
Oracle创建表分区后,查询的时候不给出partition,但是会给分区字段指定的值,会不会自动按照分区查询?
Comprehensively design an oppe homepage -- the design of the top and head
Differences between the use of structs and classes
Win11怎么自动清理回收站?
操作系统迁移实战之在openEuler上部署MySQL数据库
Use verdaccio to build your own NPM private library
Response对象-响应字符数据
Win11系统如何一键进行重装?
srec_cat 常用参数的使用
Stop using xshell and try this more modern terminal connection tool
Difference between C event and delegation
Quickly learn to configure local and network sources of yum, and learn to use yum
[basic course of flight control development 1] crazy shell · open source formation UAV GPIO (LED flight information light and signal light control)