From: KF (dotslash@snosoft.com)
Date: Mon Apr 08 2002 - 10:25:26 EDT
Heres a piece of java code from Peacenuker (www.skreel.org <http://www.skreel.org> 's admin). This helped me realise why doing this in java made my head hurt.
Thanks again everyone .. I certainly did not expect a response from half the planet!
-KF
import java.io.*;
public class Combinaison
{
private static String [] tableChar_dec = {"ABCDZ", "ABCDZ", "ABCDZ","ABCDZ"};
private static int [] currentIdx_dec = {0, 0, 0, 0};
public static void getCombination(String [] tableChar, int [] currentIdx)
{
if(currentIdx.length != tableChar.length)
throw new IllegalArgumentException("The supplied arrays don't have the same size !");
if(isFinished(tableChar, currentIdx))
return;
System.out.println(getCombinaison(tableChar, currentIdx));
do
{
incrementIdx(tableChar, currentIdx);
System.out.println(getCombinaison(tableChar, currentIdx));
}
while(!isFinished(tableChar, currentIdx));
}
//Look for the index to increment (from the end of the array)
private static boolean incrementIdx(String [] tableChar, int [] currentIdx)
{
for(int i=tableChar.length - 1;i>= 0; i--)
{
if(tableChar[i].length() > (currentIdx[i]+1))
{
currentIdx[i]++;
return true;
}
else
currentIdx[i]=0;
}
return false;//Argh, all is 0, now !!! returned to the beginning ;)
}
//Are all index to the size of the string ?
private static String getCombinaison(String [] tableChar, int [] currentIdx)
{
StringBuffer foobar=new StringBuffer(tableChar.length);//initial capacity will avoid more than one alloc
for(int i=0;i< tableChar.length;i++)
{
foobar.append(tableChar[i].charAt(currentIdx[i]));
}
return foobar.toString();
}
//Are all index to the size of the string ?
private static boolean isFinished(String [] tableChar, int [] currentIdx)
{
for(int i=0;i< tableChar.length;i++)
{
if(tableChar[i].length() != (currentIdx[i]+1))
return false;
}
return true;
}
public static void main(String args[])
{
try
{
Combinaison.getCombination(tableChar_dec, currentIdx_dec);
}
catch(Exception e)
{
System.err.println("ERROR when generating combination");
}
}
}
This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 22:28:03 EDT