Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Using an enum as an array reference

Tags:

java

arrays

enums

I would like to reference an array with an enum type. This is a pretty standard thing in C++ (my origin), however I'm unsure if this is possible/desirable in Java.

For example, I would like to have an enum such as:

public enum Resource {
    COAL,
    IRON
}

Then later I would like to reference like this:

Amount[COAL] // The amount of coal
Price[IRON]  // The price of iron

I don't want to make Amount and Price fields of Resource as I would like a list of objects (orders for resources) grouped by the resource type. Manually assigning an int to each type of Resource is no better than public static int final BLAH I feel, nothing gained.

In essence, I'm looking for the enum of C++ which tags things. I realise that this could be the 'wrong' way of doing things in Java, so feel free to point me in the direction of the correct Java ethos.

like image 937
Cramer Avatar asked Feb 23 '12 09:02

Cramer


People also ask

Can I use enum as array index?

Frequently, when developers use enumerators of an enum type as indices for an array, they know that the enumerators of the enum type have values starting from zero to a known maximum value, with an increment of one and with no gap between any pair of consecutive enumerators.

Can we use enum as array?

Enums are value types (usually Int32). Like any integer value, you can access an array with their values. Enum values are ordered starting with zero, based on their textual order. MessageType We see the MessageType enum, which is a series of int values you can access with strongly-typed named constants.

Can we have array of enum in Java?

You can iterate through an enum to access its values. The static values() method of the java. lang. Enum class that all enums inherit gives you an array of enum values.

How do you reference an array in Java?

Declaring a Variable to Refer to an Array An array's type is written as type[] , where type is the data type of the contained elements; the brackets are special symbols indicating that this variable holds an array.


3 Answers

In C++, an enum is effectively an alias for an integer. In Java, they're "proper" objects - which is why you can't use them as an array reference.

If you want to look up the value in your array that's associated with a particular enum object - that sounds like a Map to me! How about replacing those arrays with an EnumMap<Resource, Double>?

The EnumMap class is optimised for use with enum keys, such that it does end up using an array keyed on the enums' ordinal (integer) value behind the scenes. So it's much faster than a general-purpose hashmap, and you get the speed of an array-based solution with the semantic expressiveness of a Map - it's the best of both worlds.

like image 53
Andrzej Doyle Avatar answered Oct 14 '22 11:10

Andrzej Doyle


You can do almost it. In contrast to C++ where enum is just an int, Java's enum is class. So you can't use it as an index of array. But every enum element has ordinal() that is int, so you can say

amount[COAL.ordinal()]
price[IRON.ordinal()]

But you have a better approach. Can add methods to enum, so it will look like:

IRON.price(amounts)
COAL.amount(prices)

I think this approach is much better.

like image 25
AlexR Avatar answered Oct 14 '22 10:10

AlexR


Each Java enum element has an ordinal associated with it, indexed from zero based on the order of definition in the enum. Just use e.g.

COAL.ordinal()

However, it sounds to me like you'd be better off creating a class e.g. Order with fields for Amount and Price and then keeping a collection of those e.g. in a Map indexed by the enum elements.

e.g.

Map<Resource, Order> orders = new HashMap<Resource, Order>();
orders.put(Resource.COAL, new Order(price, amount));
like image 29
matthewSpleep Avatar answered Oct 14 '22 09:10

matthewSpleep