views:

58

answers:

2

I'm not too experienced with Java yet, and I'm hoping someone can steer me in the right direction because right now I feel like I'm just beating my head against a wall...

The first class is called MeasuredParams, and it's got 40+ numeric fields (height, weight, waistSize, wristSize - some int, but mostly double). The second class is a statistical classifier called Classifier. It's been trained on a subset of the MeasuredParams fields. The names of the fields that the Classifier has been trained on is stored, in order, in an array called reqdFields.

What I need to do is load a new array, toClassify, with the values stored in the fields from MeasuredParams that match the field list (including order) found in reqdFields. I can make any changes necessary to the MeasuredParams class, but I'm stuck with Classifier as it is.

My brute-force approach was to get rid of the fields in MeasuredParams and use an arrayList instead, and store the field names in an Enum object to act as an index pointer. Then loop through the reqdFields list, one element at a time, and find the matching name in the Enum object to find the correct position in the arrayList. Load the value stored at that positon into toClassify, and then continue on to the next element in reqdFields.

I'm not sure how exactly I would search through the Enum object - it would be a lot easier if the field names were stored in a second arrayList. But then the index positions between the two would have to stay matched, and I'm back to using an Enum. I think. I've been running around in circles all afternoon, and I keep thinking there must be an easier way of doing it. I'm just stuck right now and can't see past what I've started.

Any help would be GREATLY appreciated. Thanks so much!

Michael

+3  A: 

You're probably better off using a Map rather than a List, you can use the enum as the key and get the values out.

Map<YourEnumType,ValueType> map = new HashMap<YourEnumType,ValueType>();
Tom
Thank you for declaring map to be Map instead of HashMap - not enough sample code encourages the use of interfaces! But I would consider using an EnumMap instead of HashMap for efficiency.
Bert F
A: 

@Tom's recommendation to use Map is the preferred approach. Here's a trivial example that constructs such a Map for use by a static lookup() method.

private enum Season {

    WINTER, SPRING, SUMMER, FALL;
    private static Map<String, Season> map = new HashMap<String, Season>();
    static {
        for (Season s : Season.values()) {
            map.put(s.name(), s);
        }
    }

    public static Season lookup(String name) {
        return map.get(name);
    }
}

Note that every enum type has two implicitly declared static methods:

public static E[] values();
public static E valueOf(String name);

The values() method returns an array that is handy for constructing the Map. Alternatively, the array may be searched directly. The methods are implicit; they will appear in the javadoc of your enum when it is generated.

Addendum: As suggested by @Bert F, an EnumMap may be advantageous. See Effective Java Second Edition, Item 33: Use EnumMap instead of ordinal indexing, for a compelling example of using EnumMap to associate enums.

trashgod
#1 - I don't get why map/lookup() are necessary since, as you point out, there is already a valueOf() method that does the same thing. Is lookup() more efficient? #2 - if lookup() is useful, it doesn't look thread-safe - better to move the init code to a static initializer block right after the decl of map.
Bert F
Right on both counts; the example is trivial. In actual use, a `Map` only makes sense when the benefit of O(1) lookup outweighs the cost of construction. The size may be inferred from `EnumSet.allOf(Key.class)`. I've updated the example to use a static initializer.
trashgod
Thanks so much for your quick responses guys, and especially the example. I'm going to try an EnumMap, as suggested.
Michael