title: Print ArrayList that depends on other ArrayList
body: <p>I have an array of ArrayList.
    For example, 
              <code>array[0] = a b c d
                 array[1] = e f g h 
                 array[2] = i</code></p>
  
  <p>Every ArrayList in the array must be printed out, but some items are dependent on items in other ArrayLists, which must be printed first.</p>
  
  <pre><code>level 1 - array[0] = a b* c d
  level 2 - array[1] = e* f g h 
  level 3 - array[2] = i
  </code></pre>
  
  <p>The letters with the star next to it must be printed out with the items in the next level. In this case, b must include e, f, g, h since all those letters are in the next level (level2). Since e also has a star next to it, it must first print out i because i is in the next level (level3). The result is as following:</p>
  
  <pre><code>  a
    b
      -e
        -i
      -f
      -g
      -h
    c
    d
  </code></pre>
  
  <p>I have a HashMap to keeps track of which letters require printing of the next level. So, (b, 2), (e, 3) are in the map since b depends on level 2, and 3 depends on level 3.</p>
  
  <p>This is what I've tried</p>
  
  <pre><code>    public String buildString(int level, String word) {
      StringBuilder sb = new StringBuilder();
      for(int i = 0; i &lt; level; i++) {
          sb.append('-');
      }
      sb.append(" ");
      sb.append(word);
      return sb.toString();
  }
  
  public void printList() {
      ArrayList&lt;String&gt; firstLevel = allLevels[0];
      for(int i = 0; i &lt; firstLevel.size(); i++) {
          String word = firstLevel.get(i);
          System.out.println(word);
          if(map.containsKey(word)) {
              ArrayList&lt;String&gt; anotherLevel = allLevels[map.get(word)];
              for(int j = 0; j &lt; anotherLevel.size(); j++) {
                  String word2 = anotherLevel.get(j);
                  System.out.println(buildString(map.get(word), word2));
                  if(map.containsKey(word2)) {
                      ArrayList&lt;String&gt; anotherAnotherLevel = allLevels[map.get(word2)];
                      for(int k = 0; k &lt; anotherAnotherLevel.size(); k++) {
                          String word3 = anotherAnotherLevel.get(k);
                          System.out.println(buildString(map.get(word2), word3));
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Which yields the correct result, but it is not dynamic since it does not work for all cases. How would I go about making the code more dynamic? I'm assuming recursion will be needed...</p>