K
~kurt
Generics are really new to me, and catching on has been a bit confusing.
The Sun tutorial is like a man page - you gotta read it a few times and
I've been too impatient to do so. I've actually been reading through
the java.util.EnumMap code to get a better grip on things. Since all
my googling didn't turn up an example of how to pass enums (and EnumMaps),
I figured I'd post it here for others if they have the same question.
Some of the recent threads on this topic were very helpful to me, but
I still needed to put it all together.
If anyone sees anything stupid (or any suggestions for improvement,
please let me know).
import java.util.*;
public class TestEnumMap {
enum ThreeD { X, Y, Z }
public static void main(String[] args) {
double dval;
EnumMap<ThreeD, Double> xyz =
new EnumMap<ThreeD, Double>(ThreeD.class);
xyz.put(ThreeD.X, 5.0);
xyz.put(ThreeD.Y, 10.0);
xyz.put(ThreeD.Z, 15.0);
System.out.println("Not Passed");
for (ThreeD i : ThreeD.values()) {
dval = xyz.get(i);
System.out.println("" + i + ": " + dval);
}
printVals(xyz);
printVals(ThreeD.class, xyz);
}
public static void printVals (EnumMap<ThreeD, Double> em) {
System.out.println("Passed, but with just the EnumMap:");
double dval;
for (ThreeD i : ThreeD.values()) {
dval = em.get(i);
System.out.println("" + i + ": " + dval);
}
}
public static <K extends Enum<K>> void printVals(Class<K> keyType,
EnumMap<ThreeD, Double> em) {
double dval;
K[] keyUniverse = keyType.getEnumConstants();
System.out.println("Everything Passed");
for (int i=0; i<keyUniverse.length; i++) {
dval = em.get(keyUniverse);
System.out.println("" + keyUniverse + ": " + dval);
}
}
}
- Kurt
The Sun tutorial is like a man page - you gotta read it a few times and
I've been too impatient to do so. I've actually been reading through
the java.util.EnumMap code to get a better grip on things. Since all
my googling didn't turn up an example of how to pass enums (and EnumMaps),
I figured I'd post it here for others if they have the same question.
Some of the recent threads on this topic were very helpful to me, but
I still needed to put it all together.
If anyone sees anything stupid (or any suggestions for improvement,
please let me know).
import java.util.*;
public class TestEnumMap {
enum ThreeD { X, Y, Z }
public static void main(String[] args) {
double dval;
EnumMap<ThreeD, Double> xyz =
new EnumMap<ThreeD, Double>(ThreeD.class);
xyz.put(ThreeD.X, 5.0);
xyz.put(ThreeD.Y, 10.0);
xyz.put(ThreeD.Z, 15.0);
System.out.println("Not Passed");
for (ThreeD i : ThreeD.values()) {
dval = xyz.get(i);
System.out.println("" + i + ": " + dval);
}
printVals(xyz);
printVals(ThreeD.class, xyz);
}
public static void printVals (EnumMap<ThreeD, Double> em) {
System.out.println("Passed, but with just the EnumMap:");
double dval;
for (ThreeD i : ThreeD.values()) {
dval = em.get(i);
System.out.println("" + i + ": " + dval);
}
}
public static <K extends Enum<K>> void printVals(Class<K> keyType,
EnumMap<ThreeD, Double> em) {
double dval;
K[] keyUniverse = keyType.getEnumConstants();
System.out.println("Everything Passed");
for (int i=0; i<keyUniverse.length; i++) {
dval = em.get(keyUniverse);
System.out.println("" + keyUniverse + ": " + dval);
}
}
}
- Kurt