当前位置:网站首页>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 +
'}';
}
}
边栏推荐
- 广东首例!广州一公司未履行数据安全保护义务被警方处罚
- [e-mr] error recovery record of namenode
- 如何借助自动化工具落地DevOps|含低代码与DevOps应用实践
- About the idea plug-in I wrote that can generate service and mapper with one click (with source code)
- 量化交易之数字货币篇 - 通过时间戳与方向来合并逐笔成交数据(大单合并)
- Alibaba Cloud Toolkit —— 项目一键部署工具
- How does win11 automatically clean the recycle bin?
- 如何保证缓存和数据库一致性
- 2022牛客暑期多校训练营1(ACDGIJ)
- Comprehensive design of an oppe homepage -- Design of navigation bar
猜你喜欢
![[ctfshow-web]反序列化](/img/cd/b76e148adfc4d61049ab2cf429d4d7.png)
[ctfshow-web]反序列化
![[basic course of flight control development 1] crazy shell · open source formation UAV GPIO (LED flight information light and signal light control)](/img/48/6dcaf4c9695d90e62036396cd81366.png)
[basic course of flight control development 1] crazy shell · open source formation UAV GPIO (LED flight information light and signal light control)

如何借助自动化工具落地DevOps|含低代码与DevOps应用实践

Detailed explanation of tcpdump command

Win11自动删除文件设置方法

浅谈云原生边缘计算框架演进

Vlang's way of beating drums

限流对比:Sentinel vs Hystrix 到底怎么选?

【Flutter -- 进阶】打包

工作流引擎在vivo营销自动化中的应用实践
随机推荐
Win11如何关闭共享文件夹
“青出于蓝胜于蓝”,为何藏宝计划(TPC)是持币生息最后的一朵白莲花
[e-mr] error recovery record of namenode
[untitled]
Tao and art of R & D Efficiency - Tao chapter
srec_ Use of common cat parameters
Current limiting comparison: how to choose sentinel vs hystrix?
浅谈云原生边缘计算框架演进
Understanding JS foundation and browser engine
How to write unit tests
Comprehensively design an oppe homepage -- the design of the top and head
操作系统迁移实战之在openEuler上部署MySQL数据库
营销指南 | 几种常见的微博营销打法
Detailed explanation of tcpdump command
Take you a minute to learn about symmetric encryption and asymmetric encryption
第一章概述-------第一节--1.3互联网的组成
Tdengine landed in GCL energy technology, with tens of billions of data compressed to 600gb
2022牛客暑期多校训练营2(BDGHJKL)
Why is digital transformation so difficult?!
"Green is better than blue". Why is TPC the last white lotus to earn interest with money