简体   繁体   中英

How to determine lowest/highest index of enum in java?

Suppose there is the enum declaration somewhere in code:

enum Colors { RED(100), BLUE(200); }

Can I get the lowest/highest index value for that particular enum type presuming I am not aware of the declaration? Is it possible in java?

Example:

int lowIndex = Colors.minIndex(); // should return 100

Thanks everyone. So there are no implicit methods to query for min/max defined integer value. I'll have to iterate through the enum values and determine it from there as you have described.

You'll have to iterate over the enum set:

for (Color p : Color.values()) {
    // keep track of min "index"
}

Remember that an enum is essentially collection of predefined object instances. RED(100) is calling the Color(int value) constructor. That said, I could make a color enum with values defined like this:

RED("best", 14, 3.33546)

Hence, the logic for finding the minimum "index" will be different case by case.

With index do you mean the ordinal or the integer values given in the enum ?

Anyways this is a simple example that may help you:-

enum Mobile {
   Samsung(400), Nokia(250),Motorola(325);

   int price;
   Mobile(int p) { //values in brackets are set to price property in enum
      price = p;
   }
   int showPrice() {
      return price;  //you have to declare methods in enum to return value, there is no predefined function like returnValue()
   } 
}

public class EnumDemo {

   public static void main(String args[]) {

     System.out.println("CellPhone List:");
     for(Mobile m : Mobile.values()) {
        System.out.println(m + " costs " + m.showPrice() + " dollars");
     }

     Mobile ret = Mobile.Samsung;
     System.out.println("The ordinal is = " + ret.ordinal());
     System.out.println("MobileName = " + ret.name());                      
   }
}

Note that the java.lang.Enum.ordinal() method returns the ordinal of the enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).

And the output is:-

CellPhone List:
Samsung costs 400 dollars
Nokia costs 250 dollars
Motorola costs 325 dollars
The ordinal is = 0
MobileName = Samsung

You can't override an enum's 'index'. What's happening here is that the Colors enum has a constructor that takes an int as a parameter. It's up to the implementation of the constructor to store that value.

You could maintain a static map of these values, updated by the enum constructor, that could be queried for min/max values.

Alternatively you could just look over all the enums every time looking for the min/max:

int max = Integer.MIN_VALUE;
for (Colors c in Colors.values())
  max = Math.max(c.getIndex(), max);

What about this? It "caches" the value when the enum is loaded.

public enum Color {

    RED(100),
    BLUE(200);

    public final int val;

    private Color(int val) {
        this.val = val;

        if (Dummy.maxColor == null || Dummy.maxColor.val < val) {
            Dummy.maxColor = this;
        }
    }

    // This seems to be needed because you can't access static fields in enum constructors
    private static class Dummy {
        private static Color maxColor = null;
    }

    public static Color getMaxColor() {
        return Dummy.maxColor;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM