I have a question tonight. I am trying to make a java program that will take an array and then print all combinations that array of n size. For example: the full array is abcd, I want combinations of 2, so ideally I'd want aa, ab, ac, ect.
This is my current code, all it seems to output is a, b, c, d, e, ect:
public class test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Input Array:
String[] alphabet = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
int r = 3;
int n = alphabet.length;
//I need pairings of n (length) so like. 6/7/8
//Make an array to temporarily store the data, and commit recursion
print(alphabet, n, r);
}
public static String[] buildAlphabeticPassword(String[] alphabet, String[] data, int start, int end, int index, int r){
if (index == r){
for(int i=0; i <= r; i++){
System.out.println(data[i]+" ");
System.out.println("");
return data;
}
}
for( int n=start; n <= end && end-n+1 >= r-index; n++){
data[index] = alphabet[n];
buildAlphabeticPassword(alphabet, data, n+1, end, index+1, r);
}
return null;
}
public static void print(String[] alphabet, int n, int r){
String data[] = new String[r];
buildAlphabeticPassword(alphabet, data, 0, n-1, 0,r);
}
}
I apologize if I'm missing something super duper obvious. Thank you for your time!