Java 枚举如何以 1 开头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35560954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can a Java enum start with 1?
提问by displayname
Taking a look at the source code of java.time.DayOfWeek
reveals:
看一下reveal的源代码java.time.DayOfWeek
:
public enum DayOfWeek implements TemporalAccessor, TemporalAdjuster {
/**
* The singleton instance for the day-of-week of Monday.
* This has the numeric value of {@code 1}.
*/
MONDAY,
/**
* The singleton instance for the day-of-week of Tuesday.
* This has the numeric value of {@code 2}.
*/
TUESDAY,
// ..
}
So how can DayOfWeek.MONDAY = 1
? I am asking because I use GWT which does not support the new java.time
stuff yet (or never will be idk). So what I did I made my own version of DayOfWeek
but that one got a public int getValue() { return ordinal() + 1; }
which is really annoying since I have to call it everytime.
那怎么DayOfWeek.MONDAY = 1
办?我问是因为我使用的 GWT 还不支持新java.time
东西(或者永远不会是 idk)。所以我做了什么,我做了我自己的版本,DayOfWeek
但那个版本public int getValue() { return ordinal() + 1; }
真的很烦人,因为我每次都必须调用它。
That is why I am curious why the above version starts with 1. The other thing I'd like to know is why the f this hasto start with 1 and not with 0 just like every other enum does. They could have gone for Monday = 0
, Tuesday = 1
, etc. but no! They instead switched from Sunday = 0
, Monday = 1
, etc. to that thing up there.
这就是为什么我很好奇为什么上面的版本以 1 开头。我想知道的另一件事是为什么 f this必须以 1 开头,而不是像其他枚举一样以 0 开头。他们可以走了Monday = 0
,Tuesday = 1
等,但没有!相反Sunday = 0
,他们从,Monday = 1
等切换到那里的那个东西。
No, this is not an option
不,这不是一个选项
public enum MyDayOfWeek {
DUMMY,
MONDAY, TUESDAY, // ..
;
}
Simply because this would be annoying:
仅仅因为这会很烦人:
for(MyDayOfWeek day : MyDayOfWeek.value() {
if(MyDayOfWeek.DUMMY == day) {
continue;
}
}
[SUMMARY]
[概括]
If user-defined enums always start with the first element having the value '0' then how does the first element of the java.time.DayOfWeek
enum start with '1', and is there a way to have my enum start with '1' instead of '0'?
如果用户定义的枚举始终以具有值 '0' 的第一个元素java.time.DayOfWeek
开头,那么枚举的第一个元素如何以 '1' 开头,有没有办法让我的枚举以 '1' 而不是 '0' 开头'?
回答by MrHappyAsthma
To address "how does the enum start with 1"?
解决“枚举如何以 1 开头”?
It doesn't.
它没有。
Enums start with 0. They add 1 to the return value:
枚举从 0 开始。它们在返回值上加 1:
public int getValue() {
return ordinal() + 1;
}
Note: In the comments they clearly state:
注意:在评论中,他们明确指出:
Do not use ordinal() to obtain the numeric representation of DayOfWeek. Use getValue() instead.
不要使用 ordinal() 来获取 DayOfWeek 的数字表示。请改用 getValue()。
回答by azurefrog
That is why I am curious why the above version starts with 1.
这就是为什么我很好奇为什么上面的版本以1开头。
According to the comments in the code, it's because they wanted to follow the ISO-8601 week date standard:
根据代码中的注释,这是因为他们想遵循ISO-8601 周日期标准:
DayOfWeek is an enum representing the 7 days of the week - Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.
In addition to the textual enum name, each day-of-week has an int value. The int value follows the ISO-8601 standard, from 1 (Monday) to 7 (Sunday). It is recommended that applications use the enum rather than the int value to ensure code clarity.
DayOfWeek 是一个枚举,代表一周中的 7 天 - 周一、周二、周三、周四、周五、周六和周日。
除了文本枚举名称之外,每个星期几都有一个 int 值。int 值遵循ISO-8601 标准,从 1 (Monday) 到 7 (Sunday)。建议应用程序使用 enum 而不是 int 值以确保代码清晰。
The other thing I'd like to know is why the f this has to start with 1 and not with 0 just like every other enum does.
我想知道的另一件事是为什么 f this 必须从 1 开始,而不是像其他枚举一样从 0 开始。
It doesn't.
它没有。
The enum
itself is 0-indexed like any other in Java. It just fiddles with the values to give you 1-7:
它enum
本身是 0 索引的,就像 Java 中的其他任何东西一样。它只是摆弄了给你 1-7 的值:
public int getValue() {
return ordinal() + 1;
}
or to turn a 1-7 into the appropriate enum
value:
或将 1-7 转换为适当的enum
值:
public static DayOfWeek of(int dayOfWeek) {
if (dayOfWeek < 1 || dayOfWeek > 7) {
throw new DateTimeException("Invalid value for DayOfWeek: " + dayOfWeek);
}
return ENUMS[dayOfWeek - 1];
}