如何遍历包含与对象类型相同的子对象的对象?

时间:2012-08-28 12:44:23

标签: java xml arrays iteration object-model

好吧,我已经达到了本世纪的思维,并且坐了几个小时才琢磨这一个。我将在这里快速添加一个例子:

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个字段。尺寸,环境类型和伴侣。我现在在java中尝试做什么,使用我创建的对象包装器(第一个例子)是迭代每个子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
        }
    }

有什么想法吗?我接近这个错误吗?

1 个答案:

答案 0 :(得分:1)

我不确定你是否正在使用companyList,但正如Dave所说,这是一个微不足道的递归练习,所以你可以这样做:

    //(...)
    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)

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