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

时间:2022-08-13 07:31:22

Allright, I have reached the mindscrew of the century and sat a few hours cogitating this one. I will quickly add an example here:

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

public class AdUnitSizes {

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

Now basically, if I were to structure this as an XML file, it would hypothetically represent something like this (Note, Companions is of type AdUnitSize[])

现在基本上,如果我将它构造为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>

Now, As I am trying to point out, AdUnitSizes contains 3 fields. Sizes, EnvironmentTypes and Companions. What I am trying to do in java now, using the object wrapper I have created (1st example) is to Iterate through each and every child Companions Object, And their children Companions Objects till the iteration can go no further (its unlikely in our system that Companions will go more than 3 levels deep, but I need to cater for this anyway)

现在,正如我想指出的那样,AdUnitSizes包含3个字段。尺寸,环境类型和伴侣。我现在在java中尝试做什么,使用我创建的对象包装器(第一个例子)是迭代每个子Companions对象,他们的子Companions对象直到迭代不再进一步(它不太可能在我们的系统中)同伴会超过3级,但无论如何我需要迎合这一点)

This is what I have tried:

这是我尝试过的:

    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 * 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
        }
    }

Any ideas? Am I approaching this wrong?

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

1 个解决方案

#1


1  

I'm not sure awhat you're doing with the companyList, but as Dave said, is a trivial exercise of recursion, so you can do:

我不确定你是否正在使用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);
        }
    }
}

EDIT: If you need to pass something to populate, you can just pass them as a parameter like:

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

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

or set it as a member variable, if this is ok.

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

#1


1  

I'm not sure awhat you're doing with the companyList, but as Dave said, is a trivial exercise of recursion, so you can do:

我不确定你是否正在使用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);
        }
    }
}

EDIT: If you need to pass something to populate, you can just pass them as a parameter like:

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

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

or set it as a member variable, if this is ok.

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