如何从存储在列表中的对象获取特定字段值的列表? [英] How to get a list of specific fields values from objects stored in a list?
问题描述
说我有一个对象列表,其中包含两个字段 field1
和 field2
,两个字段均为String类型。
Say I have an list of objects with two fields field1
and field2
, both of String type.
如果可能的话,如何获得所有 field1
值的列表,而不必遍历该列表?
How do I get a list of all field1
values without having to iterate over the list, if it is at all possible?
推荐答案
对象是对内存地址的引用。然后,该对象的字段是指向其他内存地址的其他引用。
因此,对象列表就是引用列表。因此,列表不可能直接访问对象字段(由引用提供的引用)。简短的答案是否。
An object is a reference towards a memory address. Then, the fields of this objects are other references towards other memory addresses. Hence, a list of objects is a list of references. So, it's impossible for the list to direclty access the object fields (references given by the references). The short answer is no.
注意:无论如何,您都会找到一个可以满足您需要的API,它仍然在内部循环。
Note: anyway you'll find an API that does what you want, it still loops in the inside.
这篇关于如何从存储在列表中的对象获取特定字段值的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!