开发者

Help matching fields between two classes

开发者 https://www.devze.com 2022-12-25 06:39 出处:网络
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...

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 sea开发者_JAVA百科rch 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


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'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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号