I need to make every possible version of a list of groups by a text file full of names.
I have the part where it makes unique groups (it all gets stored in a List<List<String>> called rootList)
And that holds this:
[a, b]
[a, c]
[a, d]
[b, c]
[b, d]
[c, d]
And now I want to make another List<List<String>> called resultList that saves the information from rootList as:
List 1:
[a, b]
[c, d]
List 2:
[a, c]
[b, d]
List 3:
[a, d]
[b, c]
How would I go about doing this?
Source:
package com.company; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.regex.Pattern; public class createGroups { public static List<List<String>> rootList = new ArrayList<List<String>>(); public static List<List<String>> resultList = new ArrayList<List<String>>(); public static String comb = ""; public static void main(String[] args) { String[] names = new String[] {"Rik", "Erwin", "Ian", "Mustafa"}; int r = 2; int n = names.length; String[] data = new String[r]; String[] groups = createGroups.combinationUtil(names, data, 0, n-1, 0, r); } static void printCombination(String arr[], int n, int r) { // A temporary array to store all combination one by one String[] data = new String[r]; // Print all combination using temprary array 'data[]' combinationUtil(arr, data, 0, n-1, 0, r); } /* arr[] ---> Input Array data[] ---> Temporary array to store current combination start & end ---> Staring and Ending indexes in arr[] index ---> Current index in data[] r ---> Size of a combination to be printed */ static String[] combinationUtil(String[] arr, String[] data, int start, int end, int index, int r) { if (index == r) { for (int j=0; j<r; j++) { if (j == 1) { comb += data[j]; }else if(j == 0){ comb += data[j] + ", "; } } List<String> node = new ArrayList<String>(); node.add(comb); rootList.add(node); comb = ""; return data; } for (int i=start; i<=end && end-i+1 >= r-index; i++) { data[index] = arr[i]; combinationUtil(arr, data, i+1, end, index+1, r); } if(start == 0){ for (List<String> str: rootList){ System.out.println(str); } System.out.println("Done"); } return data; } }
The output right now is:
[Rik, Erwin]
[Rik, Ian]
[Rik, Mustafa]
[Erwin, Ian]
[Erwin, Mustafa]
[Ian, Mustafa]
Done
But what I want is:
List 1:
[Rik, Erwin]
[Ian, Mustafa]
List 2:
[Rik, Ian]
[Erwin, Mustafa]
List 3:
[Rik, Mustafa]
[Erwin, Ian]