Java - Using recursion to fill ArrayList
<p>What I am trying to do:</p>

<ol>
<li>take all <code>User</code> from <code>user.getFriends()</code> and add it to <code>temp</code></li>
<li>then from <code>user.getFriends()</code>, check those <code>User</code> objects and their <code>user.getFriends()</code> and add those to <code>temp</code></li>
<li><code>temp</code> cannot contain duplicates</li>
<li>lastly.. it must be done through recursion</li>
</ol>

<p>Here is the code:</p>

<pre><code>public ArrayList&lt;User&gt; getRecommendations(User user, ArrayList&lt;User&gt; temp, int n)
{
    if (n&lt;user.getFriends().size()){
        if (!temp.contains(user.getFriends().get(n))){
            temp.add(user.getFriends().get(n));
        }
        getRecommendations(user, temp, ++n);
    } return temp;
}
</code></pre>

<p>The gist of my problem is that I need to pass in a <code>User</code> object, add that objects friends (using <code>getFriends()</code>), and then get those object's friends (using the same method) all into a single <code>ArrayList&lt;User&gt;</code> using recursion...</p>

<p>From this method, I've been able to <code>getFriends()</code> of the initial <code>User</code> object being passed in. The recursion works fine in this case, but I cannot seem to figure out how I should design this code to get friends' of friends <code>User</code> objects.</p>