Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Iterative algorithm.</p> <pre><code>public class A { public static List&lt;List&lt;Integer&gt;&gt; combinations(List&lt;List&lt;Integer&gt;&gt; inputList) { List&lt;List&lt;Integer&gt;&gt; result = new LinkedList&lt;List&lt;Integer&gt;&gt;(); for (Integer i : inputList.get(0)) { List&lt;Integer&gt; temp = new ArrayList&lt;Integer&gt;(1); temp.add(i); result.add(temp); } for (int i = 1; i &lt; inputList.size(); i++) { result = make(result, inputList.get(i)); } return result; } private static List&lt;List&lt;Integer&gt;&gt; make(List&lt;List&lt;Integer&gt;&gt; in, List&lt;Integer&gt; n) { List&lt;List&lt;Integer&gt;&gt; res = new LinkedList&lt;List&lt;Integer&gt;&gt;(); for (List&lt;Integer&gt; l : in) { for (Integer i : n) { List&lt;Integer&gt; cur = new ArrayList&lt;Integer&gt;(l.size() + 1); cur.addAll(l); cur.add(i); res.add(cur); } } return res; } public static void main(String[] args) { List&lt;List&lt;Integer&gt;&gt; inputList = new ArrayList(); inputList.add(new ArrayList&lt;Integer&gt;() {{ add(1); add(2); }}); inputList.add(new ArrayList&lt;Integer&gt;() {{ add(10); add(20); add(30); }}); inputList.add(new ArrayList&lt;Integer&gt;() {{ add(100); }}); System.out.println(combinations(inputList)); } } </code></pre> <p>*Notice that this code <strong>not for production!</strong> You should replace <code>LinkedList</code> with <code>ArrayList</code> with initial size, make checks and so on.</p> <p><strong>upd</strong> usage example provided. there is some code improvement. But it still only draft. I wouldn't recommend you use it in real tasks.</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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