Get all combinations of an array as a string
I want to get all combinations of an array as a string.
For example;
String[] arr={"| x |","| y |","| z |"};
should give a return value as this:
| x | | y | | z |
| x | | z | | y |
| y | | x | | z |
| y | | z | | x |
| z | | x | | y |
| z | | y | | x |
It's possible to do with some for-loops like:
String str ="";
for (int i = 0; i < arr.length; i++){
for (int j = 0; j < arr.length; j++){
for (int k = 0; k < arr.length; k++){
if (i != j && i != k && j != k){
str+=arr[i] +" " + arr[j] +" " + arr[k];
str+="\n";
}
}
}
}
The problem is, that I don't know the length of the array.
Any help will be appreciated...
*************************************************************
[1398 byte] By [
Jacobssona] at [2007-9-29 1:05:31]

> I (when I was MsTingle -:) developed a fast recursive
> permutation algoritm in this thread.
>
> http://forum.java.sun.com/thread.jsp?forum=31&thread=37580&start=30&range=15&hilite=false&q=
My favorite permutation generator is the one in STL C++ code. I based the translation off that when I wrote it and inspiration from this thread:
http://forum.java.sun.com/thread.jsp?forum=4&thread=405188
Here's the code if you want to see:public class Test
{
public static void swap(char[] a, int i, int j)
{
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void reverse(char[] a)
{
reverse(a, 0);
}
public static void reverse(char[] a, int from)
{
int last = a.length - 1;
for(int i = (a.length - from) / 2 - 1; i >= 0; i--)
{
char temp = a[i + from];
a[i + from] = a[last - i];
a[last - i] = temp;
}
}
public static boolean nextPermutation(char[] a)
{
return nextPermutation(a, 0, a.length);
}
public static boolean nextPermutation(char[] a, int begin, int end)
{
if(begin + 1 >= end)
{
return false;
}
int i = end - 1;
while(true)
{
int j = i--;
if(a[i] < a[j])
{
int n = end;
while(a[i] >= a[--n]);
swap(a, i, n);
reverse(a, j);
return true;
}
if(i == begin)
{
reverse(a);
return false;
}
}
}
public static void main(String[] args)
{
char[] c = {'x','y','z'};
do
{
System.out.println(c);
} while(nextPermutation(c));
}
}
I like to think of it as the most flexible, and generally the fastest I've seen (though I don't think it compares to yours after about N is 4 or 5). I created a class of static methods that has the rest of the methods, like previousPermutation() and for the rest of the primitive arrays. It really gives you a lot of control over what you want to do, and I wish Sun had an Algorithms class like C++ that includes the methods they are missing. Anyway, thought I'd share that code.
> This is another one.
>
> http://www.merriampark.com/perm.htm
Yeah, that works, I've seen that link before. I don't think I like the use of BigInteger there, when will anyone need 21! and higher? :O Only then does that look like it has a use... but wow, 21!, how long will that take? :P
> Well i think you have the length() function in the
> String class to know the length of the String.
>
> Hope it helps
To have that information at runtime wont help because at that time the program will already be compiled and it's too late to program the corresponding number of nested loops. You can of course let the program write a program, compile and load it but that's more complicated then the suggested solutions.