Article by:Geoffrey Hunter
Enumerations in Java are treated as proper objects.
Iterating Over A Enum
An enum can be iterated over by using a for in the following way:
Indexing An Array Using An Enum
Because enumerations are proper objects, there is no way to directly use an enumeration as a index to a array (or any type of Collection for that matter). This is unlike C++, in where an enum is essentially a integer, and can be treated as such.
The recommended way is not use a enum as an array index at all, but rather replace the use of the array with a EnumMap instead.
O.K., so you really, really want to use an enum as an array index? This is not recommended, but you can use a Java enum as an array index by using the method
This work is licensed under a Creative Commons Attribution 4.0 International License .