当前位置:网站首页>接口比较器
接口比较器
2022-07-26 16:18:00 【XLMN】
public class Study04 {
//定义一个方法为所有的类进行排序,前提是类必须熟悉comparable1接口
//运用多态,用接口类引用数组做形式参数,接收不同的实参数组
public static void sort(Comparable1[] comparable1s) {
for (int i = 1; i < comparable1s.length; i++) {//控制趟数
//每趟所作的操作,从第一个元素开始进行比较相邻的两个元素,如果符合排序条件,交换两个元素值
//每趟开始之前定义一个标记变量
boolean flag = true;
for (int j = 0; j < comparable1s.length - i; j++) {
//符合条件进行交换
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 (flag) {
break;
}
}
}
public static void main(String[] args) {
/**
* 接口内部比较器
*/
Student001 st1 = new Student001("小黄", 2, 98);
Student001 st2 = new Student001("小绿", 23, 98);
Student001 st3 = new Student001("小冰", 1, 90);
Student001 st4 = new Student001("小李", 32, 8);
Student001 st5 = new Student001("小陈", 5, 28);
Student001[] stt = {st1, st2, st3, st4, st5};
System.out.println("遍历排序之前的数组=======");
for (Student001 st : stt
) {
System.out.println(st.toString());
}
sort(stt);
//遍历排序之后的数组
System.out.println("遍历排序之后的数组=======");
for (Student001 st : stt
) {
System.out.println(st.toString());
}
}
}
interface Comparable1 {
//Object类型,可以传入若干子类型,多态用法;
public int compareTo(Object object);
}
//创建子类实现接口
class Student001 implements Comparable1 {
private String name;
private int id;
private double score;
//定义无参构造方法
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;
}
}
外部比较器
public class Student05 {
public static void sort(Object[] objects, Comparator01 comparator01) {
for (int i = 1; i < objects.length; i++) {//控制趟数
//每趟所作的操作,从第一个元素开始进行比较相邻的两个元素,如果符合排序条件,交换两个元素值
//每趟开始之前定义一个标记变量
boolean flag = true;
for (int j = 0; j < objects.length - i; j++) {
//符合条件进行交换
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 (flag) {
break;
}
}
}
public static void main(String[] args) {
/**
* 接口内部比较器
*/
Student002 st1 = new Student002("小黄", 2, 98);
Student002 st2 = new Student002("小绿", 23, 98);
Student002 st3 = new Student002("小冰", 1, 90);
Student002 st4 = new Student002("小李", 32, 8);
Student002 st5 = new Student002("小陈", 5, 28);
Student002[] stt = {st1, st2, st3, st4, st5};
//定义一个外部比较器对象
StuIdComp stuIdComp = new StuIdComp();
System.out.println("遍历排序之前的数组=======");
for (Student002 st : stt
) {
System.out.println(st.toString());
}
sort(stt, stuIdComp);
//遍历排序之后的数组
System.out.println("遍历排序之后的数组=======");
for (Student002 st : stt
) {
System.out.println(st.toString());
}
}
}
//定义外部比较器
interface Comparator01 {
//形参是Object类型,用多态
public int compare(Object obj1, Object Obj2);
}
class StuIdComp implements Comparator01 {
@Override
public int compare(Object obj1, Object Obj2) {
//强制类型转换
Student002 st = (Student002) obj1;
Student002 st01 = (Student002) Obj2;
int result = st.getId() - st01.getId();
return result;
}
}
//定义学生类
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 +
'}';
}
}
边栏推荐
- Tao and art of R & D Efficiency - Tao chapter
- Pat grade a 1046 shortest distance
- Linux Installation mysql8.0.29 detailed tutorial
- Comprehensively design an oppe homepage -- Design of star models
- Pat class a 1047 student list for course
- Acl-ijcai-sigir top conference paper report meeting (AIS 2022) Note 3: dialogue and generation
- Is it safe for Guoyuan futures to open an account online? What is the account opening process?
- 2022 latest Tibet Construction scaffolder (construction special operation) simulation exam questions and answers
- 可信隐私计算框架“隐语”开源专家观点集锦
- DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
猜你喜欢

Advanced CAD exercises (I)

综合设计一个OPPE主页--明星机型的设计

Bugku login2

Re7:读论文 FLA/MLAC Learning to Predict Charges for Criminal Cases with Legal Basis

The "nuclear bomb level" log4j vulnerability is still widespread and has a continuing impact

Linux安装mysql8.0.29详细教程

PAT甲级 1047 Student List for Course

TDengine 落地协鑫能科,数百亿数据压缩至 600GB

匿名方法和lambda表达式使用的区别

微信小程序---网络数据请求
随机推荐
Sql语句——单行注释与多行注释
Linux安装mysql8.0.29详细教程
[BJDCTF2020]Easy MD5
[RCTF2015]EasySQL
量化交易之数字货币篇 - 通过时间戳与方向来合并逐笔成交数据(大单合并)
2022年最新北京建筑安全员模拟题库及答案
基于NoCode构建简历编辑器
2022 latest Tibet Construction scaffolder (construction special operation) simulation exam questions and answers
[physical simulation] the principle and practice of the simplest shape matching
2022 Niuke summer multi school training camp 1 (acdgij)
Summary of key knowledge of C language
kubernetes之ReplicationController与ReplicaSet
First knowledge of OpenGL (4) link shader
PAT甲级 1049 Counting Ones
结构体和类使用的区别
srec_cat 常用参数的使用
Analyzing method and proc in Ruby
【物理模拟】最简单的shape matching的原理与实践
【Flutter -- 进阶】打包
The process and harvest of developing browser plug-ins with clojurescript