Reputation: 61
I'm storing 12 values in an array and each one represents a piece of data for a certain month. I have an enum for all the months of the year and would like to be able to choose a month that displays the correct data instead of using the array index. Is there a way to do this? Thanks
Upvotes: 2
Views: 15725
Reputation: 1
You probably don't need any extra object beyond the enum. In the case that you want to associate enum constants with some objects or values and you need to do that only once for a given enum, you can put that data into the enum itself. You simply use the constructor arguments for that.
Here's an example how to make enum whose constants are associated with strings:
public enum ArgumentError {
NO_ARGS("%nError: No arguments."),
TOO_MANY_ARGS("%nError: Too many arguments."),
WRONG_NUMBER("%nError: You provided the wrong number."),
INPUT_PATH_FORMAT("%nError: Input path has the wrong format."),
OUTPUT_PATH_FORMAT("%nError: Output path has the wrong format.");
private final String errorMessage;
ArgumentError(String errorMessage) {
this.errorMessage = errorMessage;
}
public String getErrorMessage() {
return errorMessage;
}
}
Upvotes: 0
Reputation: 13169
I would recommend against using enum members as array indices because there's the risk that the numeric index of the enum members will change (if new members are added or if the order of the members is changed within the enum class).
Instead I would use a Map<Month, Data>
so that each enum member points to a Data
object (or any other type that you need to use) within the map:
Map<Month, Data> monthMap = new HashMap<>();
monthMap.put(Month.MARCH, dataObject);
Data retrievedData = monthMap.get(Month.MARCH);
Update: if your Map
uses an enum type as its key then it's always more efficient to use an EnumMap
instead of a HashMap
, TreeMap
, etc. So the code above would be better starting off like this:
Map<Month, Data> monthMap = new EnumMap<>(Month.class);
Behind the scenes, EnumMap
is in fact maintaining an array whose indices align with the enum instances, exactly as the OP was hoping to achieve manually.
Upvotes: 11
Reputation: 800
so for example you have your enum like:
public enum Month {
January, February, March, April, May, June, July, August, September, October, November, December
}
then you can use ordinal()
to get a numerical representation (index of you definition).
where name()
returns you the enum's name
here an example which shows you the useage for both of em:
Month[] months = Month.values();
Month m = Month.August;
System.out.println(String.format("Month #%d > %s", m.ordinal(), months[m.ordinal()].name()));
Upvotes: 2
Reputation: 1107
If you have enum of days of week like:
public enum Days {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
and arrays like: String days[]=new String[7]
The indexing in the array can only be done by int
.So you can use enum as the indexes as:
days[Days.SUNDAY.ordinal()]="sunday";
System.out.println(days[Days.SUNDAY.ordinal()]);
Output:sunday
Ordinal()
returns the position of the enum
constant in the enum
declaration.
But it does not look so elegant.
Upvotes: 8