How can I make a method in Java which obtains input integer n
, and an array of double x
, and returns all combinations of n elements out of values in x.
Where x={1,2,3}
and n=2,
the expected returns/combinations are
{1,1},{1,2},{1,3},{2,2},{2,1},{2,3},{3,1},{3,2},{3,3}
(the order is not ignored.) The number should be (int) Math.pow(x.length, n)
e.g.
static List<Double[]> getAll(int n, double[] x){
List<Double[]> combinations = new ArrayList<>();
//number of combinations should be x.length ^ n
// when n = 1;
for (int i=0;i<x.length;i++)
combinations.add({x[i]});
return combinations;
}
// return patterns[i] is the i th pattern
//which contains num-elements from the input depths,
// patterns[i][j] is the j th double in patterns[i]
private double[][] void makePatterns(int num, double[] depths) {
int patternN = (int) Math.pow(depths.length, num);
double[][] patterns = new double[patternN][num];
System.out.println(patterns.length);
int i = 0;
int k = 0;
while (i < patternN) {
for (int j = 0; j < num; j++) {
// System.out.println(i+" "+j+" "+(i / (int)Math.pow(depths.length, j))%depths.length);
patterns[i][j] = x[(i / (int)Math.pow(depths.length, j))%depths.length];
}
i++;
}
return patterns;
}
This makePatterns
is where I started...