📜  Java Enum 中 ordinal() 和 compareTo() 的区别

📅  最后修改于: 2021-09-13 02:49:14             🧑  作者: Mango

枚举用于在编程语言中表示一组命名常量。如果我们想表示一个名为 constant 的组,那么我们应该选择 Enum。每个枚举常量都是静态的。因此,我们可以使用枚举名称访问它。

例子:

enum Day{
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}

ordinal() 方法

Java.lang.Enum.ordinal()讲述了序数(它是枚举声明中的位置,其中初始常量被分配了零序数) 对于特定的枚举。

ordinal() 方法是一个非静态方法,这意味着它只能通过类对象访问,如果我们尝试访问另一个类的对象,它将给出错误。这是一个最终方法,不能被覆盖。

ordinal() 方法返回枚举常量的序数。 (它在枚举声明中的位置,其中第一个常量的序号是零)。

句法:

public final int ordinal();

返回值:

This method returns the ordinal of this enumeration constant.

例子:

Day d1 = Day.TUESDAY;
System.out.println("the ordinal value is :" + d1.ordinal());

Output:
the ordinal value is : 2
Java
// Java program to show the usage of 
// ordinal() method of java enumeration 
  
enum Day{
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
  
class Main{
      
    public static void main(String args[])
    {
       Day days[] = Day.values();
       for(Day d : days)
       System.out.print(d.ordinal() + " " );
    }
}


Java
// Java program to show the usage of 
// compareTo() method of java enumeration 
  
enum Day
{
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
  
class GFG{
      
    public static void main(String args[])
    {
        Day d1 = Day.SUNDAY;
          Day d2 = Day.MONDAY;
          Day d3 = Day.TUESDAY;
        
        // will return negative value since d1 < d2    
        System.out.println(d1.compareTo(d2) );
        
        // will return positive value since d2 > d1
          System.out.println(d2.compareTo(d1) );
        
        // will return 0 since d3 == d3
        System.out.println(d3.compareTo(d3) );
         
    }
}


输出
0 1 2 3 4 5 6 

compareTo() 方法

Enum 类的compareTo()方法将此枚举对象与定义的对象进行比较以进行排序。枚举常量只能与相同类型的其他枚举常量进行比较。

返回:

  1. 如果此枚举小于定义的对象,则为负整数。
  2. 零,如果此枚举等于定义的对象。
  3. 一个正整数,如果这个枚举大于定义的对象。

句法:

int compareTo(Object obj)

例子:

Java

// Java program to show the usage of 
// compareTo() method of java enumeration 
  
enum Day
{
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
  
class GFG{
      
    public static void main(String args[])
    {
        Day d1 = Day.SUNDAY;
          Day d2 = Day.MONDAY;
          Day d3 = Day.TUESDAY;
        
        // will return negative value since d1 < d2    
        System.out.println(d1.compareTo(d2) );
        
        // will return positive value since d2 > d1
          System.out.println(d2.compareTo(d1) );
        
        // will return 0 since d3 == d3
        System.out.println(d3.compareTo(d3) );
         
    }
}
输出
-1
1
0

Ordinal() Method

CompareTo() Method

Returns the position of the enum constant
  • Returns A negative integer, if this enum is less than the defined object.
  • Returns zero, if this enum is equal to the defined object.
  • Returns a positive integer, if this enum is greater than the defined object.
It doesn’t take any argument.  It takes an enum constant as an argument.

Example: 

enum Day{

SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;

}

Day d = Day.SUNDAY;

d.ordinal() // 0

  

Example:

enum Day{

SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;

}

Day d1 = Day.SUNDAY;

Day d2 = Day.MONDAY;

d1.compareTo(d2) // since d1 < d2 output will be -ve