arrays - Nesting an undefined number of loops in java -


i have array in java, of changing length (around 3-5). want change value of each element each possible element of array (newvalues) combinations

i'd loop in loop, in case number of loops defined length of array. i'm strangely confused. take example:

arraylist<integer> originallist = new arraylist<integer>(); \\has varying length arraylist<integer> newvalues = new arraylist<integer>();  originallist.add(0); originallist.add(0);  newvalues.add(1); newvalues.add(2); newvalues.add(3); 

the array is:

0 0 

i want loop trough of these perform action them:

1 1 1 2  1 3 2 1  2 2 2 3 ... 

i know become exponentially large, since original array should not big should ok.

try this.

static void loop(list<integer> originallist, list<integer> newvalues, int index) {     if (index >= originallist.size())         system.out.println(originallist);     else {         (int = 0; < newvalues.size(); ++i) {             originallist.set(index, newvalues.get(i));             loop(originallist, newvalues, index + 1);         }     } } 

and

    arraylist<integer> originallist = new arraylist<integer>();     arraylist<integer> newvalues = new arraylist<integer>();      originallist.add(0);     originallist.add(0);      newvalues.add(1);     newvalues.add(2);     newvalues.add(3);      loop(originallist, newvalues, 0); 

result:

[1, 1] [1, 2] [1, 3] [2, 1] [2, 2] [2, 3] [3, 1] [3, 2] [3, 3] 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -