[英]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?
在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.
RED(100)正在调用Color(int value)构造函数。 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). 请注意,java.lang.Enum.ordinal()方法返回枚举常量的序数(它在枚举声明中的位置,其中初始常量的序数为零)。
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. 这里发生的是
Colors
enum有一个构造函数,它以int
作为参数。 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.