Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to populate a Java array with variable subarrays?
    primarykey
    data
    text
    <p>I'd like to write a Java method that operates something like this:</p> <pre> input 1, output { {0}, {1} } input 2, output { {0, 0}, {0, 1}, {1, 0}, {1, 1} } input 3, output { {0, 0, 0}, {0, 0, 1}, {0, 1, 0}, ... {1, 1, 1} } ... </pre> <p>(I use 0 and 1 in the example for concision; the lowest-level subelements might be HIGH and LOW, 'A' and 'Z', or any two other distinct values.)</p> <p>This feels like a good candidate for recursion, but that's just a feeling at this point. All of my efforts so far have seemed suboptimal.* Any thoughts on a good approach, other than using a different language?</p> <p><sup>* For example: Loop over 0 to (2^input)-1; interpret the number as an [input]-digit binary value; use the binary digits to generate the subarray. Bleah.</sup></p> <p><strong>EDIT: Present generalized iterative solution</strong></p> <pre> public enum Item { ITEM1, ITEM2, ...; // As many as needed private static final int ITEM_COUNT = values().length; public static Item[][] allCombinationsOfSize(int comboSize) { int arraySize = (int) Math.pow(ITEM_COUNT, comboSize); Item array[][] = new Item[arraySize][]; for ( int n = 0 ; n &lt; arraySize ; ++n ) { array[n] = nthSubarray(n, comboSize); } return array; } private static Item[] nthSubarray(int n, int comboSize) { Item combo[] = new Item[comboSize]; for ( int i = comboSize - 1 ; i &gt;= 0 ; --i ) { combo[i] = Item.values()[n % ITEM_COUNT]; n /= ITEM_COUNT; } return combo; } } </pre> <p>I believe that allCombinationsOfSize is the method I'm looking for. I still have a sneaking suspicion that I'm missing something more elegant. Nevertheless, the above allows me to write this in my JUnit test ...</p> <pre> for ( Signal signals[] : Signal.allCombinationsOfSize(pinCount) ) { assertEquals( cls.getSimpleName() + " result", expectedResultFor(cls, signals), actualResultFor(cls, signals) ); } </pre> <p>... which is fairly straightforward.</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.
 

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