Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a recursive solution:</p> <pre><code>class Test { private static Object[][] createArray(int n, Object[] values) { Object[][] result = null; int m = values.length; if (n == 1) { result = new Object[m][1]; for (int i = 0; i &lt; m; ++i) result[i][0] = values[i]; } else { Object[][] array = createArray(n - 1, values); int l = array.length; result = new Object[m * l][n]; for (int i1 = 0; i1 &lt; m; ++i1) { for (int i2 = 0; i2 &lt; l; ++i2) { int i = i1 * l + i2; for (int j = 0; j &lt; n; ++j) result[i][j] = j == 0 ? values[i1] : array[i2][j - 1]; } } } return result; } private static void printArray(Object[][] array) { System.out.println("{"); for (int i = 0; i &lt; array.length; ++i) { System.out.print(" {"); for (int j = 0; j &lt; array[0].length; ++j) System.out.printf(" %s", array[i][j].toString()); System.out.println(" }"); } System.out.println("}"); } public static void main(String[] args) { Object[] values = {'a', 'b', 'c'}; for (int n = 1; n &lt;= 3; ++n) { System.out.printf("n = %d:\n", n); Object[][] array = createArray(n, values); printArray(array); System.out.println(); } } } </code></pre> <p>Output:</p> <pre><code>n = 1: { { a } { b } { c } } n = 2: { { a a } { a b } { a c } { b a } { b b } { b c } { c a } { c b } { c c } } n = 3: { { a a a } { a a b } { a a c } { a b a } { a b b } { a b c } { a c a } { a c b } { a c c } { b a a } { b a b } { b a c } { b b a } { b b b } { b b c } { b c a } { b c b } { b c c } { c a a } { c a b } { c a c } { c b a } { c b b } { c b c } { c c a } { c c b } { c c c } } </code></pre>
 

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