如何从包含子列表的列表中获取值,并将所有值按顺序存储在另一个列表中

时间:2014-02-19 07:57:08

标签: java json recursion ion

我有一个名为ionList的列表,其中包含名为structs的值。 列表中的每个结构都有更多列表,这些列表中包含更多结构。你可以把它想象成一种父列表和子列表

我想完全检索ionList中的所有结构,并将其存储在一个名为StructList的列表中。

我使用的代码是递归代码,但是当我在我的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;


    }

找到的childList在我的递归调用中改变了我的for循环初始化,我认为这是不解析多个包含子列表的结构的原因。请帮帮我......

谢谢!!!

1 个答案:

答案 0 :(得分:3)

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

应该说

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

因为在ionStructList范围内,索引会有所不同,因此对于您的代码,您将无法检索正确的列表。