0

好吧,我已经达到了世纪的头脑,坐了几个小时思考这个。我将在这里快速添加一个示例:

public class AdUnitSizes {

protected String environmentType;
@XmlElement(name = "size")
protected List<Size> sizes;
protected List<Companions> companions;

现在基本上,如果我将其构建为 XML 文件,它会假设表示类似这样的内容(注意,Companions 的类型为 AdUnitSize[])

    <adUnitSizes>
    <size>
        <width>800</width>
        <height>600</height>
        <isAspectRatio>true</isAspectRatio>
    </size>
    <environmentType>BROWSER</environmentType>
    <companions>  <!--adunitsize[]-->
        <size>
            <width>800</width>
            <height>600</height>
            <isAspectRatio>true</isAspectRatio>
        </size>
        <environmentType>BROWSER</environmentType>
        <companions>
            <size>
                <width>800</width>
                <height>600</height>
                <isAspectRatio>true</isAspectRatio>
            </size>
            <environmentType>BROWSER</environmentType>
            <companions>...</companions>    
        </companions>               
    </companions>
</adUnitSizes>

现在,正如我试图指出的,AdUnitSizes 包含 3 个字段。尺寸、EnvironmentTypes 和 Companions。我现在尝试在 java 中做的事情,使用我创建的对象包装器(第一个示例)是遍历每个子 Companions 对象,以及他们的子 Companions 对象,直到迭代无法继续(在我们的系统中不太可能) Companions 的深度会超过 3 个级别,但无论如何我都需要满足这一点)

这是我尝试过的:

    AdUnitSize[] adUnitSizeArray = adUnit.getAdUnitSizes();
    if(adUnitSizeArray != null){
        List<AdUnitSizes> adUnitSizesList = adUnitWrapper.getAdUnitSizes();
        for(int i = 0; i < adUnitSizeArray.length; i++){
            AdUnitSizes adUnitSizes = new AdUnitSizes();

            aem.adservices.google.dfp6.om.Size size = new aem.adservices.google.dfp6.om.Size(); //Name clash with Google Size Class
            size.setHeight(adUnitSizeArray[i].getSize().getHeight());
            size.setWidth(adUnitSizeArray[i].getSize().getWidth());
            size.setIsAspectRatio(adUnitSizeArray[i].getSize().getIsAspectRatio());
            adUnitSizes.getSizes().add(size);

            adUnitSizes.setEnvironmentType(adUnitSizeArray[i].getEnvironmentType().getValue());

            List<Companions> companionsList = adUnitSizes.getCompanions();
            //Need to iterate through all of adUnitSizeArray's Companions, and its companions children and so forth
        }
    }

有任何想法吗?我接近这个错误吗?

4

1 回答 1

1

我不确定你在用 companyList 做什么,但正如戴夫所说,这是一个简单的递归练习,所以你可以这样做:

    //(...)
    List<Companions> companionsList = adUnitSizes.getCompanions();
    iterateOverCompanions(companionsList);
}

private void iterateOverCompanions(List<Companions> companionsList) {
    for(Companions companion : companionsList) {
        //Do stuff with companion
        List<Companions> anotherCompanionList = companion.getCompanionsList();
        if (anotherCompanionList  != null && anotherCompanionList.size()>0) {
            iterateOverCompanions(anotherCompanionList);
        }
    }
}

编辑:如果您需要传递一些东西来填充,您可以将它们作为参数传递,例如:

iterateOverCompanions(List<Companions> companionsList, Size size, AnotherStuff stuff)

或将其设置为成员变量,如果可以的话。

于 2012-08-28T12:57:33.897 回答