How can a Java enum start with 1? -
taking @ source code of java.time.dayofweek reveals:
public enum dayofweek implements temporalaccessor, temporaladjuster { /** * singleton instance day-of-week of monday. * has numeric value of {@code 1}. */ monday, /** * singleton instance day-of-week of tuesday. * has numeric value of {@code 2}. */ tuesday, // .. } so how can dayofweek.monday = 1? asking because use gwt not support new java.time stuff yet (or never idk). did made own version of dayofweek 1 got public int getvalue() { return ordinal() + 1; } annoying since have call everytime.
that why curious why above version starts 1. other thing i'd know why f has start 1 , not 0 every other enum does. have gone monday = 0, tuesday = 1, etc. no! instead switched sunday = 0, monday = 1, etc. thing there.
no, not option
public enum mydayofweek { dummy, monday, tuesday, // .. ; } simply because annoying:
for(mydayofweek day : mydayofweek.value() { if(mydayofweek.dummy == day) { continue; } }
to address "how enum start 1"?
it doesn't.
enums start 0. add 1 return value:
public int getvalue() { return ordinal() + 1; } note: in comments state:
do not use ordinal() obtain numeric representation of dayofweek. use getvalue() instead.
Comments
Post a Comment