This is the basic principle...
char stuff[]="abc";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
for(int k=0; k<3; k++)
{
print stuff[i];
print stuff[j];
print stuff[k];
print newline
}
}
}
From there you should be able to devise a general model and see where the recursion comes into play.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
so parameterise the whole thing to take a number n instead of using a fixed number of iterations...
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
try the following:
public void printLoop(int index; char[] list; char[] string) {
if (index == list.length) {
System.out.print(new String(string) + " ");
return;
}
for (int i = 0; i < list.length; i++) {
string[index] = list[i];
printLoop(index + 1, list, string);
}
}
char[] string = new char[list.length];
for (int i = 0; i < list.length; i++) {
string[0] = list[i];
printLoop(1, list, string);
System.out.println("");
}
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494