title: Recursive object setting java
body: <p>I have a model call Section. This section has 2 fields: color and a list of Section objects.</p>
  
  <pre><code>public class Section{
      public String color;
      public List&lt;Section&gt; sub_sections;
  
  
  }
  </code></pre>
  
  <p>Suppose I get a Json feed with a List of Section objects. Within each Section object is a color field set to a random color and another list of Section objects. Within these nested Section lists the color fields are set to an empty string. We dont know how many nested Sections there are. </p>
  
  <p>How do i set all nested color Strings within the nested Sections to be the same as the color from the first level section? My code below is non recursive for 3 levels deep to give you an ides of the problem.</p>
  
  <pre><code>for(Section section : sectionsList){
          for(Section sub : section.sub_sections){
              sub.color=section.color;
              if(sub.sub_sections.size()&gt; 0){
                  for(Section sub2 : sub.sub_sections){
                      sub2.color=sub.color;
                      if(sub2.sub_sections.size()&gt; 0){
                          for(Section sub3 : sub2.sub_sections){
                              sub3.color=sub2.color;
                          }
                      }
                  }
              }
          }
      } 
  </code></pre>