- Java.lang 包类
- Java.lang - 首页
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包额外内容
- Java.lang - 接口
- Java.lang - 错误
- Java.lang - 异常
- Java.lang 包有用资源
- Java.lang - 有用资源
- Java.lang - 讨论
Java - 枚举compareTo() 方法
描述
Java Enum compareTo() 方法比较此枚举与指定对象的顺序。枚举常量只能与相同枚举类型的其他枚举常量进行比较。
声明
以下是java.lang.Enum.compareTo() 方法的声明
public final int compareTo(E o)
参数
o − 这是要比较的对象。
返回值
此方法返回一个负整数、零或正整数,具体取决于此对象小于、等于或大于指定对象。
异常
无
比较枚举值示例
以下示例演示了如何使用 compareTo() 方法比较相同枚举类型的各种枚举值,以检查大于 0 的结果。
package com.tutorialspoint; // enum showing topics covered under Tutorials enum Tutorials { Java, HTML, Python; } public class EnumDemo { public static void main(String args[]) { Tutorials t1, t2; t1 = Tutorials.Java; t2 = Tutorials.HTML; if(t1.compareTo(t2) > 0) { System.out.println(t2 + " completed before " + t1); } else if(t1.compareTo(t2) < 0) { System.out.println(t1 + " completed before " + t2); } else if(t1.compareTo(t2) == 0) { System.out.println(t1 + " completed with " + t2); } } }
输出
让我们编译并运行上述程序,这将产生以下结果:
Java completed before HTML
比较枚举值示例
以下示例演示了如何使用 compareTo() 方法比较相同枚举类型的各种枚举值,以检查等于 0 的结果。
package com.tutorialspoint; // enum showing topics covered under Tutorials enum Tutorials { Java, HTML, Python; } public class EnumDemo { public static void main(String args[]) { Tutorials t1, t2; t1 = Tutorials.HTML; t2 = Tutorials.HTML; if(t1.compareTo(t2) > 0) { System.out.println(t2 + " completed before " + t1); } else if(t1.compareTo(t2) < 0) { System.out.println(t1 + " completed before " + t2); } else if(t1.compareTo(t2) == 0) { System.out.println(t1 + " completed with " + t2); } } }
输出
让我们编译并运行上述程序,这将产生以下结果:
HTML completed with HTML
比较枚举值示例
以下示例演示了如何使用 compareTo() 方法比较相同枚举类型的各种枚举值,以检查小于 0 的结果。
package com.tutorialspoint; // enum showing topics covered under Tutorials enum Tutorials { Java, HTML, Python; } public class EnumDemo { public static void main(String args[]) { Tutorials t1, t2; t1 = Tutorials.HTML; t2 = Tutorials.Python; if(t1.compareTo(t2) > 0) { System.out.println(t2 + " completed before " + t1); } else if(t1.compareTo(t2) < 0) { System.out.println(t1 + " completed before " + t2); } else if(t1.compareTo(t2) == 0) { System.out.println(t1 + " completed with " + t2); } } }
输出
让我们编译并运行上述程序,这将产生以下结果:
HTML completed before Python
java_lang_enum.htm
广告