I have 5x5 element table. Let's say each row in that table is a jar. Each element in row is a different color ball in a jar. We are taking one ball from first jar, one ball from second, one ball from third... and so on to 5th jar. We have 5 ball color combination... then we put ball's back to relevant jars. Question : how many combination variants is possible ? Answer n ^ n , where n is table size !
The problem is, I never know what how big table is, though is always symetric (n x n) elements. I want to write UNIVERSAL method which will return all possible color combinations.
For table 5x5 elements it would look like this :
private int combinations = 0;
private char table[][] = { {'A','B','C','D','E'},
{'F','G','H','I','J'},
{'K','L','M','N','O'},
{'P','Q','R','S','T'},
{'U','V','X','Y','Z'}};
public Prog() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
for (int k= 0; k < 5; k++) {
for (int l = 0; l < 5; l++) {
for (int m = 0; m < 5; m++) {
System.out.println(table[0][i] +" " + table[1][j]+ " " + table[2][k]+ " " + table[3][l]+ " " + table[4][m]);
combinations++;
}
System.out.println("--------------");
}
}
}
}
System.out.println("Total combination is : " + combinations);
}
... but above code is only for 5x5 table. If I got 4x4, or 3x3, I need to modify all the for loops to work properly... Would somebody please help me to write a method that would modify itself acording to table size and return proper combinations ?
Thanks !!