当前位置:网站首页>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 +
'}';
}
}
边栏推荐
猜你喜欢

【飞控开发基础教程1】疯壳·开源编队无人机-GPIO(LED 航情灯、信号灯控制)

Acl-ijcai-sigir top conference paper report meeting (AIS 2022) Note 3: dialogue and generation

OA项目之我的会议(会议排座&送审)

The difference and efficiency comparison of three methods of C # conversion integer
![[ctfshow-web]反序列化](/img/cd/b76e148adfc4d61049ab2cf429d4d7.png)
[ctfshow-web]反序列化

如何保证缓存和数据库一致性

37.【重载运算符的类别】

Stop using xshell and try this more modern terminal connection tool

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

Singleton mode
随机推荐
The first case in Guangdong! A company in Guangzhou was punished by the police for failing to fulfill its obligation of data security protection
Probe of kubernetes
Movable view component (it can be dragged up, down, left and right)
利用MySQL主从复制延迟拯救误删数据
docker安装redis?如何配置持久化策略?
How can win11 system be reinstalled with one click?
极大似然估计
Nacos win10 installation and configuration tutorial
Alibaba Cloud Toolkit —— 项目一键部署工具
2022 Niuke summer multi school training camp 2 (bdghjkl)
kubernetes之探针
【开发教程9】疯壳·ARM功能手机-I2C教程
Tcpdump命令详解
Stop using xshell and try this more modern terminal connection tool
srec_ Use of common cat parameters
【开发教程8】疯壳·开源蓝牙心率防水运动手环-三轴计步伐
About the idea plug-in I wrote that can generate service and mapper with one click (with source code)
2022软件测试技能 Postman+newman+jenkins 持续集成 实战教程
Nacos win10 安装配置教程
Guetzli simple to use