Hi, I have two different arraylists such as Array1={"a", "sd", "fgh"} and Array2 = {"c", "op"}. I want to find out the possible combinations of items in these two Arrays....The output will be...
asd,sda,ac,ca,aop,opa,csd,sdc,fgha,afgh,opfgh,fghop,..etc.
I am using C#.Net 2 Win Apps.....
Thanks in advance
Loading
Mike GoldPosted Jul 16, 2007, 5:58 PM
AlanPosted Jul 16, 2007, 12:59 PM
Try this:
using System;
class Test
{
static void Main()
{
string[] Array1 ={"a", "sd", "fgh"};
string[] Array2 = {"c", "op"};
string[] Array3 = new string[Array1.Length + Array2.Length];
Array.Copy(Array1, 0, Array3, 0, Array1.Length);
Array.Copy(Array2, 0, Array3, Array1.Length, Array2.Length);
int[] set = new int[]{0, 1, 2, 3, 4};
int[][] perm = new int[20][];
int count = 0;
for(int i = 0; i < 4; i++)
for(int j = i + 1; j < 5; j++)
{
perm[count++] = new int[]{set[i], set[j]};
perm[count++] = new int[]{set[j], set[i]};
}
Console.WriteLine("The 20 possible permutations are :\n");
for(int i = 0 ; i < 20 ; i++)
{
Console.WriteLine("{0,3} : {1}", i+1, Array3[perm[i][0]] + Array3[perm[i][1]]);
}
Console.ReadLine();
}
}