sentence creation function
<p>I have the following function:</p>

<pre><code>      public ArrayList&lt;ArrayList&lt;Word&gt;&gt; createSentences(ArrayList&lt;ArrayList&lt;Word&gt;&gt; gestures, int startIndex) {
            if (gestures.size() == 1) {

                return gestures;
            }


            ArrayList&lt;Word&gt; ret;
            ArrayList&lt;ArrayList&lt;Word&gt;&gt; result = new ArrayList&lt;ArrayList&lt;Word&gt;&gt;();

            ArrayList&lt;Word&gt; tmp1 = gestures.get(0);
            gestures.remove(0);
            ArrayList&lt;ArrayList&lt;Word&gt;&gt; tmp2 = createSentences(gestures, startIndex + 1);


            for (Word s : tmp1) {
                for (Word s2 : tmp2.get(0)) {
                    ret = new ArrayList&lt;Word&gt;();
                    ret.add(s);
                    ret.add(s2);
                    result.add(ret);


                }


            }


 return result;
    }
</code></pre>

<p>Given an input:</p>

<pre><code>ArrayList&lt;ArrayList&lt;Word&gt;&gt; test= new ArrayList&lt;ArrayList&lt;Word&gt;&gt;();

        ArrayList&lt;Word&gt; gest1 = new ArrayList&lt;Word&gt;();
        gest1.add(new Word("A", "N"));
        gest1.add(new Word("B", "V"));

         ArrayList&lt;Word&gt; gest2 = new ArrayList&lt;Word&gt;();
        gest2.add(new Word("C", "N"));
        gest2.add(new Word("D", "V"));



        test.add(gest1);
        test.add(gest2);
</code></pre>

<p>It produces following results:</p>

<pre><code>[A, C]
[A, D]
[B, C]
[B, D]
</code></pre>

<p>which is fine but given an input:</p>

<pre><code>ArrayList&lt;ArrayList&lt;Word&gt;&gt; test= new ArrayList&lt;ArrayList&lt;Word&gt;&gt;();

        ArrayList&lt;Word&gt; gest1 = new ArrayList&lt;Word&gt;();
        gest1.add(new Word("A", "N"));
        gest1.add(new Word("B", "V"));

         ArrayList&lt;Word&gt; gest2 = new ArrayList&lt;Word&gt;();
        gest2.add(new Word("C", "N"));
        gest2.add(new Word("D", "V"));

         ArrayList&lt;Word&gt; gest3 = new ArrayList&lt;Word&gt;();
        gest3.add(new Word("E", "N"));
        gest3.add(new Word("F", "V"));

        test.add(gest1);
        test.add(gest2);
        test.add(gest3);
</code></pre>

<p>It produces:</p>

<pre><code>[A, C]
[A, E]
[B, C]
[B, E]
</code></pre>

<p>where I would like to achieve all possible combinations of matching like this:</p>

<pre><code>[A, C, E]
[A, C, F]
[A, D, E]
[A, D, F]
[B, C, E]
[B, C, F]
[B, D, E]
[B, D, F]
</code></pre>

<p>Can somebody help me and rewrite the function to produce wanted results.</p>
