当前位置:网站首页>接口比较器
接口比较器
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 +
'}';
}
}
边栏推荐
- Bugku login2
- [physical simulation] the principle and practice of the simplest shape matching
- Test cases should never be used casually, recording the thinking caused by the exception of a test case
- Draw a beautiful outline of the middle school playground and generate longitude and latitude data
- Pat grade a 1048 find coins
- Vlang's way of beating drums
- guetzli简单使用
- [e-mr] error recovery record of namenode
- Which book is the "the Nine Yin Manual" in the field of programming
- docker安装redis?如何配置持久化策略?
猜你喜欢

2022 latest Tibet Construction scaffolder (construction special operation) simulation exam questions and answers

Pat grade a 1049 counting ones

Internet Protocol

2022年最新西藏建筑施工架子工(建筑特种作业)模拟考试试题及答案

综合设计一个OPPE主页--布局与初始化

SQL statement -- single line comment and multi line comment

2022牛客暑期多校训练营1(ACDGIJ)

Bugku login1

Comprehensively design an oppe homepage -- layout and initialization

Technology vane | interpretation of cloud native technology architecture maturity model
随机推荐
微信小程序---网络数据请求
What is the complexity often said during the interview?
2022 latest Tibet Construction scaffolder (construction special operation) simulation exam questions and answers
FTP protocol
ZABBIX 6.2.0 deployment
kubernetes之ReplicationController与ReplicaSet
How to test the circle of friends (mind map)
综合设计一个OPPE主页--布局与初始化
NUC 11 build esxi 7.0.3f install network card driver-v2 (upgraded version in July 2022)
PAT甲级 1044 Shopping in Mars
[RCTF2015]EasySQL
Alibaba cloud DMS MySQL cloud database report error, solve!!
Is it safe for Guoyuan futures to open an account online? What is the account opening process?
Compiler analysis of clojure operation principle
Why is digital transformation so difficult?!
2022 Niuke summer multi school training camp 1 (acdgij)
My SQL is OK. Why is it still so slow? MySQL locking rules
Bugku login1
2022年全国最新消防设施操作员(高级消防设施操作员)考试试题及答案
srec_cat 常用参数的使用