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

网站开发品牌有哪些黑帽友情链接

网站开发品牌有哪些,黑帽友情链接,江西建设质量安全监督网站,建站之星管理中心对集合元素排序比较 1. 使用 Comparable 接口实现默认排序 Comparable 是 Java 中的一个接口#xff0c;用于定义对象之间的排序规则。 实现了 Comparable 接口的类可以比较其对象的大小#xff08;包装类都实现了该接口#xff09;#xff0c;从而可以在集合类#xf…对集合元素排序比较 1. 使用 Comparable 接口实现默认排序 Comparable 是 Java 中的一个接口用于定义对象之间的排序规则。 实现了 Comparable 接口的类可以比较其对象的大小包装类都实现了该接口从而可以在集合类如 TreeSet、TreeMap 等中进行排序和查找操作。这种排序被称为类的自然排序类的 compareTo() 方法被称为它的自然比较方法。 public interface ComparableT {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张三, math23, chinese90}113 Student{name赵六, math24, chinese80}104 Student{name李四, math24, chinese80}104 Student{name王五, math23, chinese70}93 Student{name田七, math25, chinese50}752. 使用 Comparator 接口实现比较器排序 Comparator 是 java 中的一个接口用于定义对象之间的定制排序规则。 与 Comparable 接口不同Comparator 接口允许在排序时使用独立于对象类的比较逻辑因此可以在不休改对象类的情况下实现多种不同的排序方式当使用了实现 Comparator 接口的比较器后默认的 COmparable 比较器就不起作用了。 使用 Comparator 接口需要重写 compare() 方法该方法的定义语法格式如下 public interface ComparatorT {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田七, math25, chinese50}75 Student{name王五, math43, chinese70}113 Student{name李四, math24, chinese80}104 Student{name赵六, math24, chinese80}104 Student{name张三, math23, chinese90}113compare() 方法用于在 Arrays 类调用 sort(比较对象数组, 比较器) 方法时对两个对象进行比较大小。
http://www.pierceye.com/news/986246/

相关文章:

  • 个人做网站做什么样的话网站站点连接不安全
  • 响应式网站 外贸平顶山 网站设计
  • 手袋 东莞网站建设成都哪里好玩适合情侣
  • 苏州哪个公司做门户网站给学校建网站那个系统好
  • 现在网站都是拿什么软件做的wordpress 自动alt
  • 山东省住房和城乡建设部网站首页网站中的自助报价系统
  • 如何 网站收录软件开发大概需要多少钱
  • 网站建设微信端简洁型网页
  • 一般的网站开发语言用什么《网站开发实践》 实训报告
  • php企业网站开发实验总结服务器
  • 织梦网站文章内容模板荣耀商城
  • seo 网站太小平面设计师招聘广告文案
  • 凡科网站建设桂林网站开发m0773
  • 做跨境都有哪些网站做电影类网站
  • 琼海商城网站建设东莞网站建设价位
  • 装饰网站建设重要性云服务器 多个网站
  • 邯郸市城乡建设管理局网站小米商城wordpress
  • 河北中石化建设网站html用户登录注册页面代码
  • 如何在网站后台备份数据库表设计本家装
  • 网站开发绑定qq企业域名注册
  • nas做网站网站安全建设工作总结
  • 淮北网站建设费用男女做暖暖到网站
  • wordpress站点管理浙江东南网架公司
  • 做外贸用什么搜索网站企业贷款政策最新消息2022
  • 各大网站创始人株洲网站建设报价方案
  • 想做网站多少钱wordpress数据连接失败1223
  • 株洲建设网站公司个人网站建设简历
  • 地方网站系统wordpress管理入口
  • 个人做网站开发指标开发板是什么东西
  • 国家住房与城乡建设部网站国内比较知名的大型门户网站