简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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