I am looking for a method in java to have all combinations of the elements of x taken m at a time.
please note all proposed algorithms (see the below link and code) so far just print list of combination with using a recursive function strategy and don't save the results in a array
for instance if str[] data={"a","b","c","d"}
with m=2
(please note: data and m are arguments for the method)
the method returns an array :
(a,b
a,c
a,d
b,c
b,d
c,d)
Again the below code just print them and not save them in an array:
import java.util.Arrays;
public class Combination {
public static void main(String[] args){
String[] arr = {"A","B","C","D","E","F"};
combinations2(arr, 3, 0, new String[3]);
}
static void combinations2(String[] arr, int len, int startPosition, String[] result){
if (len == 0){
System.out.println(Arrays.toString(result));
return;
}
for (int i = startPosition; i <= arr.length-len; i++){
result[result.length - len] = arr[i];
combinations2(arr, len-1, i+1, result);
}
}
}