arrays - How to create a Systematic List in Java? -
hey guys, i'm trying create systematic list generator in java i've come slight error. our example pizza.
problem: suppose have 6 total toppings make pizzas with. how many variations of 5 topping pizza's can possibly make?
so example :
[pepperoni, bacon, pineapple, onion, mushrooms, peppers] [pepperoni, bacon, pineapple, onion, mushrooms, cheese] etc...
while developing, have been practising 3 possible options apposed 6, know there 3 total combinations each 2 elements, making multi-dimentional array easy generate @ time.
public class systematiclist { static string options[] = { "pepperoni", "bacon", "cheese" }; public static void main(string[] args) { /** * generate multi-dimensional array represent combinations of * pizza toppings can possibly have available <code>options</code> * */ string[][] combos = new string[3][2]; /** * ideally create. * combos[0][0] = "pepperoni"; * combos[0][1] = "bacon"; * combos[1][0] = "pepperoni"; * combos[1][1] = "cheese"; * combos[2][0] = "bacon"; * combos[2][1] = "cheese"; */ (int pizzas = 0; pizzas < combos.length; pizzas++) { (int toppings = 0; toppings < combos[pizzas].length; toppings++) { combos[pizzas][toppings] = options[0]; // <<< issue : element. system.out.print(" " + combos[pizzas][toppings]); } system.out.println(""); } /* * current output : * run: * pepperoni bacon * pepperoni bacon * pepperoni bacon * build successful (total time: 0 seconds) * * ^ obvious, not know how i'll select * element array of topping options before/after specified * index [example: 0 range 1 - 2 apposed 0 - 2 * 'dropping' optional element, making easier. * * loop virtually doing actions of : * 0 : 1,2 * 1 : 1,2 * 2 : 1,2 */ } }
i have decided generate 2d list store values.
int[# possible combinations][# possible values]
my current code assumes know possible combinations (though of course not), , regardless can determine values (how many 5, 4, 3, 2 topping pizzas can built)
how able select element options[] while ensuring no other array contains elements you're trying insert. have tried arrays.contains or arrays.equals not know insert compare with, combos[pizzas-1] ?
if (!arrays.contains(combos[pizzas], combos[pizzas-1]) { combos[pizzas][toppings] = options[?]; }
one way @ toppings haven't considered yet. instance when choosing 3 toppings [pepperoni, bacon, pineapple, onion, mushrooms, peppers, cheese], might decide skip cheese, choose peppers, skip mushrooms , onion, , choose pineapple , bacon. need leave enough toppings finish pizza of course, can't skip cheese, peppers, mushrooms, onion and pineapple. sample code in javascript using recursive algorithm give idea.
function combos(toppings, count) { var result = []; helper(result, [], toppings, toppings.length, count); return result; } function helper(result, selection, toppings, length, count) { if (count == 0) // no more toppings left add result.push(selection); // must solution else // start @ --count because need able add other toppings (var = --count; < length; i++) // add selected topping selection // consider toppings haven't looked @ yet helper(result, selection.concat([toppings[i]]), toppings, i, count); }
Comments
Post a Comment