简体   繁体   中英

How to get values from a list that has child lists and store all the values in order in another list

I have a list named ionList which has values named structs in it. Each struct in the list has more lists and those lists has some more structs in it. You can imagine it like kind of a parent list and child lists

I want to retrieve all the structs in the ionList completely and store it in one list named StructList.

The code i used is a recursive code but the data is getting lost when i call the recursive function whenevr i encounter a child list in my ionList.

    public List<IonStruct> findStructsFromParentList(IonList ionList) {
        String objectType = "object_type";
        String diffType = "diff_type";
        String childList = "children";
        IonList ionChildList = null;
        IonList ionChildList1 = null;
        List<IonStruct> ionStructList = new ArrayList<IonStruct>();
        System.out.println("ION LIST"+ionList.size());
        for (int index = 0; index < ionList.size();index++) {
            ionStructList.add((IonStruct) ionList.get(index));
            ionChildList = (IonList) ionStructList.get(index).get(childList);
            if (ionChildList != null) {
                System.out.println("Found child");
                ionStructList.addAll(findStructsFromParentList(ionChildList));

            }

        }

        return ionStructList;


    }

The childList found is altering my for loop initialisation in my recursive call which i think is the reason for not parsing more than one struct with child lists in it. Please help me out..

Thanks !!!

The line that says

ionChildList = (IonList) ionStructList.get(index).get(childList);

should say

ionChildList = (IonList)((IonStruct) ionList.get(index)).get(childList);

because within ionStructList , the indexes will be different, so with your code, you won't retrieve the right list.

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