帮助匹配两个类之间的字段 [英] Help matching fields between two classes

查看:21
本文介绍了帮助匹配两个类之间的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Java 的经验还不是很丰富,我希望有人能指引我走向正确的方向,因为现在我觉得我只是在撞墙......

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

第一个类称为 MeasuredParams,它有 40 多个数字字段(身高、体重、腰围、腕围 - 一些整数,但大部分是双精度).第二类是称为分类器的统计分类器.它已经在 MeasuredParams 字段的子集上进行了训练.分类器所训练的字段名称按顺序存储在名为 reqdFields 的数组中.

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.

我需要做的是加载一个新数组 toClassify,其中的值存储在 MeasuredParams 的字段中,这些值与 reqdFields 中的字段列表(包括顺序)相匹配.我可以对 MeasuredParams 类进行任何必要的更改,但我仍然坚持使用 Classifier.

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.

我的蛮力方法是去掉 MeasuredParams 中的字段并使用 arrayList 代替,并将字段名称存储在 Enum 对象中以充当索引指针.然后循环遍历reqdFields 列表,一次一个元素,在Enum 对象中找到匹配的名称,在arrayList 中找到正确的位置.将存储在该位置的值加载到 toClassify 中,然后继续处理 reqdFields 中的下一个元素.

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.

我不确定我将如何搜索 Enum 对象 - 如果字段名称存储在第二个 arrayList 中会容易得多.但是两者之间的索引位置必须保持匹配,我又回到使用枚举.我认为.我整个下午都在兜圈子,我一直在想一定有更简单的方法来做到这一点.我现在卡住了,看不清我已经开始的事情.

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!

迈克尔

推荐答案

使用 Map 而不是 List 可能更好,您可以使用枚举作为键并获取值.

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

这篇关于帮助匹配两个类之间的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆