Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do I list all possible combinations?

807600Jun 22 2007 — edited Jun 22 2007
I find myself needing to be able to produce a list of all possible combinations of letters (case-insensitive) between 1 and 20 characters. A perfect job for a computer to do. Unfortunately, I'm a moron, and can't quite figure out how to code this. Using my current method, I'd have thousands of for loops and very sore fingers. Can someone help me out and show me a better way to do this?

Desired output format:
a
b
c
a,a
b,a
c,a
a,b
b,b
c,b
a,c
b,c
c,c
a,a,a
b,a,a
c,a,a
a,a,b
b,a,b
c,a,b
a,a,c
b,a,c
c,a,c
a,b,a
b,b,a
c,b,a
a,b,b
b,b,b
c,b,b
a,b,c
b,b,c
c,b,c
a,c,a
b,c,a
c,c,a
a,c,b
b,c,b
c,c,b
a,c,c
b,c,c
c,c,c
The ugly code I used to create the above list:
var arr = new Array();
var alphabet = new Array("a","b", "c");
for(var x=0;x<3;x++){ 
arr[0]=alphabet[x];
document.write('<br>' + arr);
}

for(var y=0;y<3;y++){
arr[0]=alphabet[y];
arr[1]=alphabet[0];
document.write('<br>' + arr);
}
for(var y=0;y<3;y++){
arr[0]=alphabet[y];
arr[1]=alphabet[1];
document.write('<br>' + arr);
}
for(var y=0;y<3;y++){
arr[0]=alphabet[y];
arr[1]=alphabet[2];
document.write('<br>' + arr);
}
for(var y=0;y<3;y++){
arr[0]=alphabet[y];
arr[1]=alphabet[0];
arr[2]=alphabet[0];
document.write('<br>' + arr);
}
for(var y=0;y<3;y++){
arr[0]=alphabet[y];
arr[1]=alphabet[0];
arr[2]=alphabet[1];
document.write('<br>' + arr);
}
for(var y=0;y<3;y++){
arr[0]=alphabet[y];
arr[1]=alphabet[0];
arr[2]=alphabet[2];
document.write('<br>' + arr);
}
for(var y=0;y<3;y++){
arr[0]=alphabet[y];
arr[1]=alphabet[1];
arr[2]=alphabet[0];
document.write('<br>' + arr);
}
for(var y=0;y<3;y++){
arr[0]=alphabet[y];
arr[1]=alphabet[1];
arr[2]=alphabet[1];
document.write('<br>' + arr);
}
for(var y=0;y<3;y++){
arr[0]=alphabet[y];
arr[1]=alphabet[1];
arr[2]=alphabet[2];
document.write('<br>' + arr);
}
for(var y=0;y<3;y++){
arr[0]=alphabet[y];
arr[1]=alphabet[2];
arr[2]=alphabet[0];
document.write('<br>' + arr);
}
for(var y=0;y<3;y++){
arr[0]=alphabet[y];
arr[1]=alphabet[2];
arr[2]=alphabet[1];
document.write('<br>' + arr);
}
for(var y=0;y<3;y++){
arr[0]=alphabet[y];
arr[1]=alphabet[2];
arr[2]=alphabet[2];
document.write('<br>' + arr);
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 20 2007
Added on Jun 22 2007
22 comments
582 views