Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: v_borchert@despammed.com (Volker Borchert) Newsgroups: comp.lang.java.programmer Subject: Re: What's the best way to make a cross string set? Date: 29 Jul 2017 06:03:04 GMT Organization: Private site at Eddersheim, Germany Lines: 24 Distribution: world Message-ID: References: <6f9ff338-1944-4b64-83b8-4a921ce57a89@googlegroups.com> X-Trace: individual.net dEGu5kplCBqCd6D+CrLyTgzPhJMfbFF9CtI/SR8yLCG/KidfoL Cancel-Lock: sha1:qTevNwMSBRRt0/gk3YC/eXj/wyQ= Xref: csiph.com comp.lang.java.programmer:37021 Eric Sosman wrote: > If by "complete set" you mean "the Cartesian product of all the > lists contained in list1," where list1 might hold N embedded lists > (more generally, Collections) instead of just three, I'd suggest a > recursive solution along the lines of: > > void tuples(List bigList, int which, String soFar) { > if (which < bigList.size()) { > for (Object item : bigList.get(which)) { > tuples(bigList, which + 1, soFar + item); > } > } else { > System.out.println(soFar); > } > } > ... > tuples(list1, 0, ""); I'd reuse a single StringBuilder. -- "I'm a doctor, not a mechanic." Dr Leonard McCoy "I'm a mechanic, not a doctor." Volker Borchert