Help matching fields between two classes

不问归期 提交于 2019-12-11 12:39:00

问题


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


回答1:


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>();



回答2:


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



来源:https://stackoverflow.com/questions/2570265/help-matching-fields-between-two-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!