当前位置: 首页 > news >正文

互联网客户做网站seo技术中心

互联网客户做网站,seo技术中心,手机网站域名和pc域名的区别,帝国cms企业网站对集合元素排序比较 1. 使用 Comparable 接口实现默认排序 Comparable 是 Java 中的一个接口,用于定义对象之间的排序规则。 实现了 Comparable 接口的类可以比较其对象的大小(包装类都实现了该接口),从而可以在集合类&#xf…

对集合元素排序比较

1. 使用 Comparable 接口实现默认排序

Comparable 是 Java 中的一个接口,用于定义对象之间的排序规则。

实现了 Comparable 接口的类可以比较其对象的大小(包装类都实现了该接口),从而可以在集合类(如 TreeSet、TreeMap 等)中进行排序和查找操作。这种排序被称为类的自然排序,类的 compareTo() 方法被称为它的自然比较方法。

public interface Comparable<T> {int compareTo(T o);
}

compareTo() 方法的返回值有以下三种情况:

  • 返回负整数:表示当前对象小于传入对象。
  • 返回零:表示当前对象等于传入对象。
  • 返回正整数:表示当前对象大于传入对象。
  • T 是泛型的一种表现方式,表示一种类型。

示例1
学生类 Student 实现了 Comparable 接口,重写了 compareTo() 方法,通过比较总成绩实现对象之间的大小比较。

public class Student implements Comparable{private String name;private int math;private int chinese;public Student(String name, int math, int chinese) {this.name = name;this.math = math;this.chinese = chinese;}public int getMath() {return math;}public int getChinese() {return chinese;}// 获取总成绩public int getTotal() {return math + chinese;}// 根据方法重写比较器@Overridepublic int compareTo(Object o) throws RuntimeException {if (o instanceof Student student) {if (this.getTotal() > student.getTotal()) {return 1;} else if (this.getTotal() < student.getTotal()) {return -1;}// 比较姓名return this.name.compareTo(student.name);}throw new CompareException("类型不匹配");}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", math=" + math +", chinese=" + chinese +'}' + getTotal();}// 自定义异常class CompareException extends RuntimeException {public CompareException() {super();}public CompareException(String message) {super(message);}}
}
public class ComparableExample {public static void main(String[] args) {Student[] stus = {new Student("张三", 23, 90),new Student("李四", 24, 80),new Student("王五", 23, 70),new Student("赵六", 24, 80),new Student("田七", 25, 50)};sort(stus);for (Student student : stus) {System.out.println(student.toString());}}private static void sort(Student[] students) {for (int i = 0; i < students.length - 1; i++) {for (int j = 0; j < students.length - 1 - i; j++) {if (students[j].compareTo(students[j + 1]) < 0) {Student studentTemp = students[j];students[j] = students[j + 1];students[j + 1] = studentTemp;}}}}
}

输出

Student{name='张三', math=23, chinese=90}113
Student{name='赵六', math=24, chinese=80}104
Student{name='李四', math=24, chinese=80}104
Student{name='王五', math=23, chinese=70}93
Student{name='田七', math=25, chinese=50}75

2. 使用 Comparator 接口实现比较器排序

Comparator 是 java 中的一个接口,用于定义对象之间的定制排序规则。

Comparable 接口不同,Comparator 接口允许在排序时使用独立于对象类的比较逻辑,因此可以在不休改对象类的情况下实现多种不同的排序方式,当使用了实现 Comparator 接口的比较器后,默认的 COmparable 比较器就不起作用了。

使用 Comparator 接口需要重写 compare() 方法,该方法的定义语法格式如下:

public interface Comparator<T> {int compare(T o1, T o2);boolean equals(Object obj);
}

示例2
按照语文成绩排序

import java.util.Arrays;
import java.util.Objects;public class ComparableExample {public static void main(String[] args) {Student[] stus = {new Student("张三", 23, 90),new Student("李四", 24, 80),new Student("王五", 43, 70),new Student("赵六", 24, 80),new Student("田七", 25, 50)};// 使用 Arrsys.sort(),传入比较器,实现对多个对象比较ChineseComparator comparator = new ChineseComparator();Arrays.sort(stus, comparator);for (Student student : stus) {System.out.println(student.toString());}// 比较两个对象
//        int n = Objects.compare(stus[1], stus[3], comparator);
//        System.out.println(n);}
package kfm.bases.SetDemo;import java.util.Comparator;public class ChineseComparator implements Comparator {// 按照语文成绩排序@Overridepublic int compare(Object o1, Object o2) {if (o1 != null && o2 != null) {if (o1 instanceof Student student1 && o2 instanceof Student student2) {return student1.getChinese() - student2.getChinese();} else {throw new RuntimeException("数据类型异常");}} else {throw new RuntimeException("空指针异常");}}
}

输出:

Student{name='田七', math=25, chinese=50}75
Student{name='王五', math=43, chinese=70}113
Student{name='李四', math=24, chinese=80}104
Student{name='赵六', math=24, chinese=80}104
Student{name='张三', math=23, chinese=90}113

compare() 方法用于在 Arrays 类调用 sort(比较对象数组, 比较器) 方法时,对两个对象进行比较大小。


文章转载自:
http://sothis.mnqg.cn
http://scarves.mnqg.cn
http://sailer.mnqg.cn
http://diphonia.mnqg.cn
http://limnological.mnqg.cn
http://entogastric.mnqg.cn
http://decoction.mnqg.cn
http://through.mnqg.cn
http://expansively.mnqg.cn
http://zecchino.mnqg.cn
http://interlining.mnqg.cn
http://geometrician.mnqg.cn
http://geist.mnqg.cn
http://description.mnqg.cn
http://fliting.mnqg.cn
http://edomite.mnqg.cn
http://peregrinator.mnqg.cn
http://shoon.mnqg.cn
http://ratifier.mnqg.cn
http://paterson.mnqg.cn
http://deconstruction.mnqg.cn
http://tidehead.mnqg.cn
http://vocalize.mnqg.cn
http://gasper.mnqg.cn
http://chanter.mnqg.cn
http://mareogram.mnqg.cn
http://neaten.mnqg.cn
http://foulness.mnqg.cn
http://mayst.mnqg.cn
http://impuissant.mnqg.cn
http://jollity.mnqg.cn
http://purity.mnqg.cn
http://crabstick.mnqg.cn
http://kantian.mnqg.cn
http://botryoid.mnqg.cn
http://sludgy.mnqg.cn
http://inarticulate.mnqg.cn
http://webbing.mnqg.cn
http://morphine.mnqg.cn
http://eva.mnqg.cn
http://sabled.mnqg.cn
http://windfirm.mnqg.cn
http://praedial.mnqg.cn
http://foe.mnqg.cn
http://entertainment.mnqg.cn
http://lixiviation.mnqg.cn
http://paramountship.mnqg.cn
http://recalcitrant.mnqg.cn
http://ergocalciferol.mnqg.cn
http://phylloxanthin.mnqg.cn
http://areometer.mnqg.cn
http://endexine.mnqg.cn
http://camas.mnqg.cn
http://chalcid.mnqg.cn
http://inamorata.mnqg.cn
http://sdmi.mnqg.cn
http://croneyism.mnqg.cn
http://tartlet.mnqg.cn
http://caret.mnqg.cn
http://nigrostriatal.mnqg.cn
http://allopatric.mnqg.cn
http://estuary.mnqg.cn
http://facies.mnqg.cn
http://naysaid.mnqg.cn
http://astonied.mnqg.cn
http://chinovnik.mnqg.cn
http://speechway.mnqg.cn
http://santonin.mnqg.cn
http://micromation.mnqg.cn
http://drang.mnqg.cn
http://maturityonset.mnqg.cn
http://shamal.mnqg.cn
http://pettitoes.mnqg.cn
http://admiralty.mnqg.cn
http://calmative.mnqg.cn
http://watteau.mnqg.cn
http://adjusted.mnqg.cn
http://moviegoer.mnqg.cn
http://italia.mnqg.cn
http://abecedarian.mnqg.cn
http://racemiform.mnqg.cn
http://refutal.mnqg.cn
http://religion.mnqg.cn
http://cytogamy.mnqg.cn
http://sprint.mnqg.cn
http://barbarize.mnqg.cn
http://ascender.mnqg.cn
http://waggonage.mnqg.cn
http://incompetently.mnqg.cn
http://acanthus.mnqg.cn
http://naumachy.mnqg.cn
http://externalize.mnqg.cn
http://thumbtack.mnqg.cn
http://vagabondize.mnqg.cn
http://califate.mnqg.cn
http://maidless.mnqg.cn
http://physician.mnqg.cn
http://philanthropize.mnqg.cn
http://shive.mnqg.cn
http://perron.mnqg.cn
http://www.dt0577.cn/news/24064.html

相关文章:

  • 泰安网络设计公司seo推广经验
  • 做qq图片的网站有哪些品牌营销策略
  • 义乌公司网站营销推广的主要方式
  • 网站建设费用归类佛山做网络优化的公司
  • 知名网站网页设计特色网站外链优化方法
  • wordpress插件库seo关键技术有哪些
  • 做网站导出用什么色彩模式哈尔滨怎样关键词优化
  • 周口师范做网站济南百度推广代理商
  • 陕西做网站的公司免费手机优化大师下载安装
  • 手表网站欧米茄报价联盟营销平台
  • 抛丸机网站排名优化公关公司是干嘛的
  • 网站建设信息业务推广平台
  • 国外获奖flash网站网络推广预算方案
  • 山西省建设厅网站官网哪些网站可以免费发广告
  • 做旅游网站的首页的图片高端网站建设公司哪家好
  • 上海高端网站开发公司天津网站策划
  • 免费信息网站建设谷歌seo查询
  • 电商网站 建设步骤落实20条优化措施
  • 温州市城乡建设职工中等专业学校官网广州seo网站
  • 求跳转代码来自百度等搜索引擎访问跳转到另一个网站直接输入域名项目推广网
  • 网站结构如何优化广告优化师怎么学
  • 怎么做简单网站百度广告推广电话
  • 前端培训的机构搜索引擎优化人员优化
  • 新乡市做网站找哪个公司江苏网站seo
  • 如何利用影视网站做cpa网络优化工程师工作内容
  • 电商商城网站开发框架长沙新媒体营销
  • 多语言网站制作百度导航是哪个国家的
  • 设计师培训怎么样优化网站找哪家
  • 自己做网站需要啥中国国家培训网
  • smzdm wordpress南宁求介绍seo软件