0

So I have an ArrayList<String[]> list which contains some String[] that I need to get the combinations of.

So if list contained A = {a,b,c} and B = {d,e,f} I want to print

ad,ae,af,bd,be,bf,cd,ce,cf

I am just having trouble seeing how I would have one fixed array then get the other so I can iterate through that.

Edit: To clarify I am trying to do this for N String[] so I cant just get the 0th and 1st element.

KTF
  • 5
  • 3
  • Why did someone come in and downvote all the answers all of a sudden? They were +1 – Parker Oct 25 '14 at 17:35
  • @Parker Because they were wrong. While I believe one shouldn't spoon feed people, assuming in an answer that lists only ever contain two items is a pretty big mistake. Let's just assume the guy knows how to do a two-level deep loop if he only had two String[]. – Lan Oct 25 '14 at 17:38
  • 1
    @John, if the author doesn't specify that there's more than 2 elements in an arraylist, we can't know for sure. We answer with the given info, if we assume otherwise we may give an incorrect answer. Don't just downvote all of the competing answers, that's vote manipulation on your part – Parker Oct 25 '14 at 17:41
  • 1
    @KTF - you're still a little unclear. If you have an arraylist with A,B,C do you still only want combinations of ad, ae, etc? Or do you want combinations of adg, adh, etc? – Parker Oct 25 '14 at 17:44
  • How are you getting `A` and `B` populated? – anubhava Oct 25 '14 at 17:47

5 Answers5

2

Iterate over two arrays and on each step make new string combination. After all just print combinations in some way.

    List<String[]> input = ...;
    Set<String> allCombinations = new HashSet<>();
    for (int i = 0; i < input.size() - 1; i++) {
        for (int j = i + 1; j < input.size(); j++) {
            allCombinations.addAll(twoArraysCombinations(input.get(i), input.get(j)));
        }
    }
    // print allCombinations


private static Set<String> twoArraysCombinations(String[] first, String[] second) {
    Set<String> result = new HashSet<>();
    for (String f : first) {
        for (String s : second) {
            result.add(f + s);
        }
    }
    return result;
}
Sergii Lagutin
  • 10,561
  • 1
  • 34
  • 43
  • That works only for two lists. While his example has only A and B, I doubt many people only store two string arrays in a list of them. – Lan Oct 25 '14 at 17:33
  • 1
    I think it's not difficult to apply this methods for more than 2 string containers. – Sergii Lagutin Oct 25 '14 at 17:38
  • Agreed with @SergeyLagutin, the author originally only said A and B, we can't read their minds, so we answer with the information they give us even if we "doubt many people store 2 string arrays in a list" – Parker Oct 25 '14 at 17:40
  • The crux of his question, to me, seemed to be "I am just having trouble seeing how I would have one fixed array then get the other so I can iterate through that." You don't help him. If it was "I have to String[], how do I print them" he would have asked that. – Lan Oct 25 '14 at 17:41
  • @SergeyLagutin Like the answer now. Putting the twoArraysCombinations in a separate method is quiet intelligent. Keeps the parts simpler and if he wants A x B and B x A, he just needs to edit one line and add a second. Nice coding. – Lan Oct 25 '14 at 17:50
  • Thanks @John. Just attention to detail. – Sergii Lagutin Oct 25 '14 at 17:52
0

Edit: Author changed his question to specify that there are more than 2 lists in an array. This answer works for 2, but it can used to expand to more. It's still unclear if we wants ALL possible combinations, or just combinations of 2

Use nested loops to iterate over both ArrayLists and then concatenate and print the strings.

If both String[] arrays are in the ArrayList you should pull them out first with String[] A = yourArrayList.get(0) and String[] B = yourArrayList.get(1)

for (String aString : A) 
    for (String bString : B)
        System.out.println(aString + bString);
Parker
  • 8,539
  • 10
  • 69
  • 98
0

You can use:

for (String a: list.get(0)) {
   for (String b: list.get(1)) {
      System.out.println(a + b);
   }
}

Where list is your original List<String[]>

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

Just use 2 for-loops

  String a [] ={"a","b","c"};
  String b [] ={"a","b","c"};
  ArrayList<String> d = new ArrayList<>();
  for (String a1 : a) {
    for (String b1 : b) {
    d.add(a1 + b1);
                        }
                      }
  System.out.println(d.toString());
Payam
  • 479
  • 2
  • 18
-3

Impressed with the answers so far....Read the question first guys

I'll give you an outline of a solution (warning typos?). I'll leave it up to you to decide how to put commas between entries and other little intricacies incase this is a homework question. Another poster showed how to put the items in a list instead of printing them. Depending on your exact needs, you may want to merge my example with theirs.

List<String[]> stringLists = ....

for(int firstListIndex = 0; firstListIndex < stringLists.size(); firstListIndex++){
    String[] primaryList = stringLists.get(firstListIndex);
    for(int secondList = firstListIndex + 1; ....){
             String[] secondaryList = ....
             for(String first : primaryList){
                 for(String second : secondaryList){
                     System.out.println(first + second);
                 }
             }

    }

}
Lan
  • 1,206
  • 1
  • 11
  • 26
  • `'ll give you an outline of a solution. I'll leave it up to you to decide how to put commas between entries`, and while he says "I can't just do get(0) and get(1)", he edited that in AFTER the answers were made. He never asked to print commas, they're in the question to separate the concatenations. If you think theres a problem with someone's answer, it's considered politce to comment first, instead of coming in and downvoting everyone's answer but your own (in this case, yours is no better than the others, this is just vote manipulation). – Parker Oct 25 '14 at 17:37
  • @Parker What do you think he meant when he said "I am *just* having trouble seeing how I would have one fixed array then get the other so I can iterate through that." He's saying his only issue is with multiple arrays in the list. Said nothing about the printing of two arrays. Considering he immediately edited his question after getting three answers that did not address it, my downvotes were not uncalled for. I did comment on one explaining why. – Lan Oct 25 '14 at 17:44
  • He edited the question after the answers were made. You didn't even give people a chance to update their answers. Editing a question doesn't notify people who made answers. – Parker Oct 25 '14 at 17:46
  • @Parker I changed my down to an up for Sergey when he changed his answer. I'll do the same for the others if they do too. What was worst, me downvoting answers that didn't remote address KTF's question (even before edits) or the 3-upvotes three answers got despite not addressing the question? – Lan Oct 25 '14 at 17:56