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

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

问题描述

我对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多个数字字段(高度,重量,腰围大小,手腕大小-某些int,但多数为double).第二类是称为分类器的统计分类器.它已在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天全站免登陆