</pre><pre name="code" class="html"><span style="white-space:pre"> </span>private static char SPLIT_STR = '|';
private static char SPLIT_InSTR = ',';
public List<string> generate(String s)
{
String[] skus = s.Split(SPLIT_STR);
// the array like the some skus
string[][] skuDetails = splitStringToChar(skus);
// record sku generated status
int[] skuStatus = createFlags(skus.Length);
int productTotal = countProductTotal(skuDetails);
String[] products = new String[productTotal];
return fillProductsBySkuDetails(skuDetails, skuStatus, products);
//return new String[] { };
//return products;
}
private string[][] splitStringToChar(String[] sarr)
{
string[][] carrs = new string[sarr.Length][];
for (int i = 0, n = sarr.Length; i < n; i++)
{
//sarr[i] == a,b,c
String s = sarr[i];
//carrs[i] = s.ToCharArray();
string[] sArr = s.Split(SPLIT_InSTR);
carrs[i] = sArr;
}
return carrs;
}
private int[] createFlags(int length)
{
int[] flags = new int[length];
initFlags(flags);
return flags;
}
private void initFlags(int[] flags)
{
for (int i = 0, n = flags.Length; i < n; i++)
{
flags[i] = 0;
}
}
private int countProductTotal(string[][] skuDetails)
{
int count = 1;
foreach (string[] cs in skuDetails)
{
count *= cs.Length;
}
return count;
}
private List<string> fillProductsBySkuDetails(string[][] skuDetails, int[] skuStatus, String[] products)
{
List<string> strList = new List<string>();
int skuTotal = skuDetails.Length;
int productTotal = products.Length;
for (int ipro = 0; ipro < productTotal; ipro++)
{
StringBuilder sb = new StringBuilder();
StringBuilder productSku = new StringBuilder(skuTotal);
for (int isku = 0; isku < skuTotal; isku++)
{
productSku.Append(skuDetails[isku][skuStatus[isku]]);
sb.Append(skuDetails[isku][skuStatus[isku]] + ",");
}
strList.Add(sb.ToString());
updateSkuStatus(skuDetails, skuStatus, 0, skuTotal);
}
return strList;
}
// skuStatus is like a number, and every updating it add one step.
private void updateSkuStatus(string[][] skuDetails, int[] skuStatus, int index, int skuTotal)
{
// when no sku to update
if (index == skuTotal) return;
skuStatus[index] = skuStatus[index] + 1;
if (skuStatus[index] >= skuDetails[index].Length)
{
skuStatus[index] = 0;
updateSkuStatus(skuDetails, skuStatus, index + 1, skuTotal);
}
}
调用:List<string> numberList = this.generate("0,1,2,3|4|5,6,7");