Re: Using Enumerated Types as Array Indexes
On Tue, 16 Aug 2011, KevinSimonson wrote:
Java is a pretty handy language in its own right. But in Ada one could
define arrays to be indexed by enumerated types. Can Java do that?
No.
If not, why not?
Because in Java, arrays are indexed by integers, and values of enumerated
types are objects (which are not integers).
As Patricia has pointed out, there is EnumMap. And your code below is not
too far off doing the same thing.
sh-4.1$ javac EnumArray1.java
EnumArray1.java:10: cannot find symbol
symbol : method values()
location: class java.lang.Enum<En>
En[] vlues = En.values();
Every enum type has a static values() method, but you can't call static
methods polymorphically like this. In fact, i don't think you can call
static methods on type variables at all, although i could be wrong.
However, the Class class does have a getEnumConstants() methods which does
what you want. If the constructor takes a suitable instance of Class, it
can use that method.
EnumArray1.java:11: generic array creation
enumArray = new Ty[ vlues.length];
^
An annoying consequence of Java's approach to generics (type erasure) is
that you can't create arrays of a generic type. But if you have a Class
object for the component type, you can use java.lang.reflect.Array's
newInstance method to create one.
EnumArray1.java:61: next() in EnumArray1<Ty,En> cannot be applied to
(En)
nextToRead = next( nextToRead);
^
There is no single-argument method called next in your code.
Here's an implementation of the constructor which might work (barring a
bit of try-catch around the reflection):
public EnumArray1(Class<En> type) {
En[] values = type.getEnumConstants();
enumArray = Array.newInstance(type, values.length);
nextToRead = values[0];
}
I've dispensed with the length check, because if someone passes in an
empty enum, then as far as i'm concerned, they deserve the
ArrayIndexOutOfBoundsException they'll get.
I haven't gone over your other classes, because hopefully this should
solve all your problems.
Note that the constructor i suggest is used like this:
EnumArray1<RockPaperScissors> rps = new EnumArray1<RockPaperScissors>(RockPaperScissors.class);
Which is a bit annoying. If you are very annoyed, and if you are prepared
to stomach some reflective witchcraft, you can add another, more magical,
constructor:
protected EnumArray1() {
this((Class<En>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
}
Which lets you then write a more magical but briefer construction
expression:
EnumArray1<RockPaperScissors> rps = new EnumArray1<RockPaperScissors>() {};
I would suggest, though, that you don't.
tom
--
Freedom, Beauty, Truth, and Love!