Function for bigrams matching
<p>I have got a following function:</p>

<pre><code>public static ArrayList&lt;String&gt; matchLists(ArrayList&lt;String&gt; ar1, ArrayList&lt;String&gt; ar2, ArrayList&lt;String&gt; ar3) {

  ArrayList&lt;String&gt; result = new ArrayList&lt;String&gt;();

  for (int i = 0; i &lt; ar1.size(); i++) {
    for (int j = 0; j &lt; ar2.size(); j++) {
      for (int k = 0; k &lt; ar3.size(); k++) {

        String[] s1 = ar1.get(i).split("\\s");
        String[] s2 = ar2.get(j).split("\\s");
        String[] s3 = ar3.get(k).split("\\s");

        if (s1[1].equals(s2[0]) &amp;&amp; s2[1].equals(s3[0])) {
          result.add(s1[0] + " " + s2[0] + " " + s3[0] + " " + s3[1]);
        }
      }
    }
  }
  return result;
}       
</code></pre>

<p>It takes as input 3 ArrayLists of bigrams and compares them. If the second word from the first array list matches the word from the second array list then they create a sentence.</p>

<p>Example:</p>

<pre><code>1st Array List: he ate
2nd Array List: ate two
3rd Array List: two apples
</code></pre>

<p>creates a sentence he ate two apples. But this function is only limited to 3 ArrayLists. I would like to make it more robust so It would accept a 2d ArrayList of strings where each single ArrayList would be a collection of bigrams and would be checking all the possible matchings from the available bigrams. Can somebody help me with that?</p>
