递归对象设置java

时间:2014-01-31 18:03:47

标签: java recursion

我有一个模型调用部分。此部分有两个字段:颜色和Section对象列表。

public class Section{
    public String color;
    public List<Section> sub_sections;


}

假设我得到一个带有Section对象List的Json feed。在每个Section对象中,是一个设置为随机颜色的颜色字段和另一个Section对象列表。在这些嵌套的Section列表中,颜色字段设置为空字符串。我们不知道有多少嵌套的部分。

如何将嵌套Sections中的所有嵌套颜色字符串设置为与第一级部分的颜色相同?我的下面的代码是3级深度的非递归,以便为您提供问题的概念。

for(Section section : sectionsList){
        for(Section sub : section.sub_sections){
            sub.color=section.color;
            if(sub.sub_sections.size()> 0){
                for(Section sub2 : sub.sub_sections){
                    sub2.color=sub.color;
                    if(sub2.sub_sections.size()> 0){
                        for(Section sub3 : sub2.sub_sections){
                            sub3.color=sub2.color;
                        }
                    }
                }
            }
        }
    } 

4 个答案:

答案 0 :(得分:5)

如果我理解你,这可能是一个解决方案。你需要一个没有参数的公共启动方法和一个获取原始颜色的递归方法。

public class Section{
  public String color;
  public List<Section> sub_sections;

  /** Propagates the current section color to all sub sections. */
  public void propagate() {
    propagate(this.color);
  }

  /** Propagates the passed color to this section and all sub sections recursively. */
  private void propagate(String propagatedColor) {
    color = propagatedColor;
    for (Section sub : sub_sections) {
      sub.propagate(propagatedColor);
    }
  }

}

比您的使用代码看起来像:

for (Section section : sectionsList) {
  section.propagate();
}

答案 1 :(得分:2)

如果我理解你,那么这样的事情可能是一个解决方案:

import java.util.ArrayList;
import java.util.List;



public class Section{
    public String color;
    public List<Section> sub_sections;

    public Section(String color,List<Section> subs){
        this.color = color;
        this.sub_sesctions = subs;
    }

    public static List<Section> init(List<Section> sectionsList){
        List<Section> all = new ArrayList<Section>();
        for(Section section : sectionsList){
            List<Section> subs = initRec(section.sub_sections, section.color);
            all.add(new Section(section.color,subs));
        } 
        return all;
    }

    private List<Section> initRec(List<Section> sectionsList, String color){
        List<Section> subs = new ArrayList<Section>();
        for(Section sub : sectionsList){           
            subs.add( new Section(color, initRec(sub.sub_sections, color) ) );
        }
        return subs;
    }


}

您开始致电init(List<Section> sectionsList)

更新:我更新了构造函数

答案 2 :(得分:1)

也许是这样的:

void setColor(Section s){
    //Loop through all subsections
    for(Section sub:s){
        //Set each child subsection to parent color
        sub.color = s.color;
        //Check if child subsection has more children
        if(sub.sub_sections.size() > 0){
            //If it does treat the child as a parent and feed it back into the method
            setColor(sub);
        }
        //If it has no more children, method returns and propagates back up the stack.
    }
}

没有测试过这个,但它应该有用......

答案 3 :(得分:1)

public void setTopLevelColor(String topLevelColor) {
    this.color = topLevelColor;
    if(this.sub_sections.size() > 0) {
        for(Section sub : this.sub_sections) {
            sub.setTopLevelColor(topLevelColor);
        }
    }
}