Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With the recursive method you would put one of your looping in a function pass the loop parameters to that function. Then from within the function's loop, it calls its to nest another loop.</p> <pre><code>void loopFunction(ArrayList&lt;ArrayList&lt;String&gt;&gt; newWords, int level) { if (level == 0) { // terminating condition if (/* compare the indices by for example passing down a list with them in */) { newWords.get(...).add(...); } } else {// inductive condition for (int i = 0; i &lt; len; i++) { loopFunction(newWords, level-1); } } } </code></pre> <p>So for your example you need 3 levels of recursion so you would start the recursion with:</p> <pre><code>loopFunction(newWords, 3); </code></pre> <p><strong>Edit</strong></p> <p>Since you have been having trouble here is a working version. It keeps a list of indices to compare against and it builds up the string as it goes. The rearranged words are added to the 2D array at each level to get all lengths of words. With recursion it is easiest to think functionally and not change and keep the variables immutable (unchangeable). This code mostly does that although I update the <code>indices</code> rather than create a new copy for convenience.</p> <pre><code>void loopFunction(ArrayList&lt;String&gt; lets, ArrayList&lt;ArrayList&lt;String&gt;&gt; newWords, int level, ArrayList&lt;Integer&gt; indices, String word) { if (level == 0) { // terminating condition return; } else { // inductive condition for (int i = 0; i &lt; lets.size(); i++) { if (!indices.contains(i)) { // Make sure no index is equal int nextLevel = level-1; String nextWord = word+lets.get(i); newWords.get(level-1).add(nextWord); indices.add(i); loopFunction(lets, newWords, nextLevel, indices, nextWord); indices.remove((Integer) i); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload