简体   繁体   English

如何从列表中提取字符串[] <MyStructure>

[英]How to extract string[] from List<MyStructure>

I just would like to extract the "name" attribute of my structure into a string[]. 我只想将我的结构的“名称”属性提取到字符串[]中。

I know there must be a smart solution - but I don't have an idea what to look for. 我知道必须有一个明智的解决方案-但我不知道要寻找什么。

List<MyStruct> myStruct1;

public class MyStruct1 {

    public String name1;
    public List<MyStruct2> myStruct2;
}

public class MyStruct2 {
    public String name2;
    public List<MyStruct3> myStruct3;
}

and what I am looking for is 而我正在寻找的是

String[] names1 = ? the array of all name1 coming from MyStruct1 ?

I know the .toArray conversion for simple lists, but I don't have an idea how to get it from inside a structure. 我知道简单列表的.toArray转换,但是我不知道如何从结构内部获取它。 I would think there is a solution WITHOUT having to loop manually. 我认为没有手动循环的解决方案。

Many thanks! 非常感谢!

Maybe I am overlooking something, but: 也许我忽略了一些东西,但是:

String[] names = new String[myStruct1.length()];
for(int i = 0; i < myStruct1.length(); ++i) {
    names[i] = myStruct1.get(i).name1;   
}

Or, are MyStruct1 and MyStruct2 subclasses of MyStruct and you only want the names from MyStruct2 objects? 或者,是MyStruct的MyStruct1和MyStruct2子类,您只需要MyStruct2对象的名称吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM