使用递归连接不同数组中的元素(Connect element in distinct array using recursion)
如果我有两个数组
A:[A,B] B:[1,2,3]如何创建字符串列表,如[A_1, A_2, A_3, B_1, B_2, B_3]
阵列的数量不规则,可能还有3个
A:[A,B] B:[1,2,3] C:[w,x,y,z] D:[m,n] E:[p,q,r]我可以使用递归来解决它吗?
if I have two array
A:[A,B] B:[1,2,3]how can I create a string List like [A_1, A_2, A_3, B_1, B_2, B_3]
the number of array is not regular, it's maybe have 3 more
A:[A,B] B:[1,2,3] C:[w,x,y,z] D:[m,n] E:[p,q,r]can I use recursive to solve it?
最满意答案
因此,我们定义一个函数Merge ,它获取stings列表的列表并将它们合并到您想要的可枚举字符串中
static void Main(string[] args) { var a = new[] { "A", "B" }; var b = new[] { "1", "2", "3" }; var c = new[] { "x", "y", "z", "w" }; var result = Merge(a, b, c); foreach (var r in result) { Console.WriteLine(r); } } public static IList<string> Merge(params IEnumerable<string>[] lists) { return Merge((IEnumerable<IEnumerable<string>>) lists); } public static IList<string> Merge(IEnumerable<IEnumerable<string>> lists) { var retval = new List<string>(); var first = lists.FirstOrDefault(); if (first != null) { var result = Merge(lists.Skip(1)); if (result.Count > 0) { foreach (var x in first) { retval.AddRange(result.Select(y => string.Format("{0}_{1}", x, y))); } } else { retval.AddRange(first); } } return retval; }如果您使用Lists作为输入,我们也可以改进这一点
public static IList<string> Merge(params IList<string>[] lists) { return Merge((IList<IList<string>>) lists); } public static IList<string> Merge(IList<IList<string>> lists, int offset = 0) { if (offset >= lists.Count) return new List<string>(); var current = lists[offset]; if (offset + 1 == lists.Count) // last entry in lists return current; var retval = new List<string>(); var merged = Merge(lists, offset + 1); foreach (var x in current) { retval.AddRange(merged.Select(y => string.Format("{0}_{1}", x, y))); } return retval; }So, we define a functions Mergethat takes lists of list of stings and merges them into the string enumerable you want
static void Main(string[] args) { var a = new[] { "A", "B" }; var b = new[] { "1", "2", "3" }; var c = new[] { "x", "y", "z", "w" }; var result = Merge(a, b, c); foreach (var r in result) { Console.WriteLine(r); } } public static IList<string> Merge(params IEnumerable<string>[] lists) { return Merge((IEnumerable<IEnumerable<string>>) lists); } public static IList<string> Merge(IEnumerable<IEnumerable<string>> lists) { var retval = new List<string>(); var first = lists.FirstOrDefault(); if (first != null) { var result = Merge(lists.Skip(1)); if (result.Count > 0) { foreach (var x in first) { retval.AddRange(result.Select(y => string.Format("{0}_{1}", x, y))); } } else { retval.AddRange(first); } } return retval; }we can also improve this, if you use Lists as inputs
public static IList<string> Merge(params IList<string>[] lists) { return Merge((IList<IList<string>>) lists); } public static IList<string> Merge(IList<IList<string>> lists, int offset = 0) { if (offset >= lists.Count) return new List<string>(); var current = lists[offset]; if (offset + 1 == lists.Count) // last entry in lists return current; var retval = new List<string>(); var merged = Merge(lists, offset + 1); foreach (var x in current) { retval.AddRange(merged.Select(y => string.Format("{0}_{1}", x, y))); } return retval; }更多推荐
发布评论