C# adding string elements of 4 different string arrays with each other
I need an advice about how to create new string array from 4 different string arrays:
We have 4 string arrays:
string[] arr1 = new string [] {"a1","a2","a3"..., "a30"};
string[] arr2 = new string [] {"d10","d11","d12","d13","d14","d15"};
string[] arr3 = new string [] {"f1","f2","f3"...,"f20"};
string[] arr4 = new string [] {"s10","s11","s12","s13","s14"};
We need to add all string elements of all 4 arrays with each other like this:
a1+d10开发者_如何学Python+f1+s10
a2+d10+f1+s10
...
a1+d11+f1+s10
a2+d11+f1+s10
...
a30+d15+f20+s14
I mean all combinations in that order : arr1_element, arr2_element, arr3_element, arr4_element
So the result array would be like that: string[] arr5 = new string [] {"a1d10f1s10","a2d10f1s10 "....};Make a recursive method that returns all combinations of the arrays:
static IEnumerable<string> GetCombinations(string[][] arrays, int len, string start) {
foreach (string s in arrays[len - 1]) {
if (len == 1) {
yield return s + start;
} else {
foreach (string r in GetCombinations(arrays, len - 1, s + start)) {
yield return r;
}
}
}
}
Usage:
string[][] arrays = { arr1, arr2, arr3, arr4 };
foreach (string s in GetCombinations(arrays, arrays.Length, string.Empty)) {
Console.WriteLine(s);
}
Once upon a time in a programming contest I used this. It worked fine. See if it works for you.
:)
private static IEnumerable<string>
GetCombinations(string[] arr1, string[] arr2, string[] arr3, string[] arr4)
{
int i, j, k, l;
i = j = k = l = 0;
for (i = 0; i < arr4.Length; i++)
for (j = 0; j < arr3.Length; j++)
for (k = 0; k < arr2.Length; k++)
for (l = 0; l < arr1.Length; l++)
yield return (arr1[l] + arr2[k] + arr3[j] + arr4[i]);
}
Sample usage:
public static void Main(string[] args)
{
string[] arr1 = new string[] { "a1", "a2", "a3", "a30" };
string[] arr2 = new string[] { "d10", "d11", "d12", "d13" };
string[] arr3 = new string[] { "f1", "f2", "f3", "f20" };
string[] arr4 = new string[] { "s10", "s11", "s13", "s14" };
var list = GetCombinations(arr1, arr2, arr3, arr4);
foreach (var item in list.Take<string>(10))
{
Console.WriteLine(item);
}
}
LINQ comes very handy in this case:
var arr1 = new [] { "a1", "a2", "a3", "a4", "a5" };
var arr2 = new [] { "b1", "b2", "b3", "b4", "b5" };
var arr3 = new [] { "c1", "c2", "c3", "c4", "c5" };
var arr4 = new [] { "d1", "d2", "d3", "d4", "d5" };
var arr5 = (from a in arr1
from b in arr2
from c in arr3
from d in arr4
select a + b + c + d).ToList();
This 5 lines of code would return:
a1b1c1
a1b1c2
a1b1c3
a1b1c4
a1b1c5
a1b2c1
a1b2c2
[...]
a2b1c1
a2b1c2
a2b1c3
a2b1c4
a2b1c5
a2b2c1
a2b2c2
a2b2c3
a2b2c4
a2b2c5
a2b3c1
[...]
a5b5c4
a5b5c5
Something similar to this might work...
List<string> AddEntries(List<List<string>> entries) {
List<string> finalEntries = new List<string>();
if (entries != null && entries.Length > 0) {
if (entries.Length > 1) {
foreach(string entry in entries) {
foreach(string subentry in AddEntries(entries.Skip(1)) {
finalEntries.Add(entry + subEntry);
}
}
} else {
foreach(string entry in entries[0]) { finalEntries.add(entry); }
}
}
return finalEntries;
}
I apologize for the code, I dont have a compiler here to test. Assumes that you are using C# 3.5 with extension methods. I took the liberty of changing your string[] to List. If you arent using 3.5 you will need to write your own function for trimming the nested arrays down.
you need to use C# 4.0 .Zip() Extension method for IEnumerable<> collections. The method takes deleagate as parameter for that purpose. Sample:
List<string> a = new List<string> { "code" };
List<int> b = new List<int>() { 7 };
var res = a.Zip(b, (p1, p2) => p1.ToString() + p2.ToString());
精彩评论