Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: What's the best way to make a cross string set? Date: Thu, 27 Jul 2017 15:49:55 -0400 Organization: A noiseless patient Spider Lines: 56 Message-ID: References: <6f9ff338-1944-4b64-83b8-4a921ce57a89@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 27 Jul 2017 19:45:55 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="fafe98e8bd85deeac969b8a5cc8f205c"; logging-data="32076"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+oUOvcgkXGZ8s1M5V1mrKP" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 In-Reply-To: <6f9ff338-1944-4b64-83b8-4a921ce57a89@googlegroups.com> Content-Language: en-US Cancel-Lock: sha1:tyo3jmQhGwoz6G7UR8Qob1I5XEQ= Xref: csiph.com comp.lang.java.programmer:37009 On 7/27/2017 3:12 PM, Eric Douglas wrote: > I have a variable number of lists of values, as in > ArrayList list1 = new ArrayList() > ArrayList list2 = new ArrayList() > list2.add("a") > list2.add("b") > list2.add("c") > list1.add(list2) > ArrayList list3 = new ArrayList() > list3.add("1") > list3.add("2") > list3.add("3") > list1.add(list3) > ArrayList list4 = new ArrayList() > list4.add("fg") > list4.add("hj") > list4.add("kl") > list1.add(list4) > > Now I want to print only the complete set, as in > a1fg > b1fg > c1fg > a2fg > b2fg > c2fg > a3fg > b3fg > c3fg > a1hj > b1hj > ... 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, ""); If that's not what you want, please explain "complete set" more fully. -- esosman@comcast-dot-net.invalid Twelve hundred seventy-three days to go.