~kurt Guest
|
Posted: Thu Apr 12, 2007 7:10 am Post subject: enum and EnumMap as parameters? |
|
|
I've managed to confuse myself on this - it may be because I haven't worked
with Maps much.
I want to pass an enum and an EnumMap as a parameters.
I have a method that operates on each element of an array using a for loop.
It is a generic method, and the array could represent {x, y}, {x, y, z},
{x, y, z, dx, dy, dz}, and so on (the method solves a system of linear
differential equations and the array represents the "state" of the system).
To make it safer, and check for potential indexing errors at compile time,
I would like to make use of an enum, and an EnumMap.
For example, something like this:
enum TwoD { X, Y }
enum ThreeD { X, Y, Z }
EnumMap<ThreeD, Double> xyz =
new EnumMap<ThreeD, Double>(ThreeD.class);
xyz.put(ThreeD.X, 20.0);
xyz.put(ThreeD.Y, 10.0);
xyz.put(ThreeD.Z, 50.0);
/*
* The call to this method is conceptual - doesn't work.
*/
someMethod(xyz, ThreeD);
And, for someMethod:
/*
* Once again, just conceptual
*/
public void someMethod(EnumMap xyz, Enum D) {
/* loop on all values */
for (D d : D.values()) {
.
.
.
a = xyz.get(d);
.
.
.
}
Any suggestions on how to pass this information through the method?
Thanks,
- Kurt |
|