我花了数小时寻找解决方案,但没有成功。希望有人可以帮助我。
我有一个M个原始邮政编码上N个项目的动态数组。
例如:
项目1:11001、54010、60621
项目2:11001,60621
项目3:60621
我想创建一个新的数组,如下所示:
路线1:11001、11001、60621
路线2:11001、60621、60621
路线3:54010、11001、60621
以此类推-直到Route 6。
有什么建议吗?
----------------------有什么方法可以不用Linq来完成? VB.net和Linq不能一起使用:)
最佳答案
听起来您想从Eric Lippert's blog post编写此函数来响应Generating all Possible Combinations:
public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(
this IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
from accseq in accumulator
from item in sequence
select accseq.Concat(new[] {item}));
}
这样您就可以编写如下代码:
int[][] items = {
new[] { 11001, 54010, 60621 },
new[] { 11001, 60621 },
new[] { 60621 }
};
var routes = CartesianProduct(items);
foreach (var route in routes)
Console.WriteLine(string.Join(", ", route));
并获得如下输出:
11001, 11001, 60621 11001, 60621, 60621 54010, 11001, 60621 54010, 60621, 60621 60621, 11001, 60621 60621, 60621, 60621
EDIT: Here's the VB.NET version (in VS2010)
Imports System.Runtime.CompilerServices
Module Module1
<Extension()>
Private Function CartesianProduct(Of T)(
ByVal sequences As IEnumerable(Of IEnumerable(Of T))) _
As IEnumerable(Of IEnumerable(Of T))
Dim emptyProduct As IEnumerable(Of IEnumerable(Of T)) =
New IEnumerable(Of T)() {Enumerable.Empty(Of T)()}
Return sequences.Aggregate(
emptyProduct,
Function(accumulator, sequence)
Return (From accseq In accumulator
From item In sequence
Select accseq.Concat(New T() {item}))
End Function)
End Function
Sub Main(ByVal args As String())
Dim items = New Integer()() {New Integer() {11001, 54010, 60621},
New Integer() {11001, 60621},
New Integer() {60621}}
Dim routes = items.CartesianProduct()
Dim route As IEnumerable(Of Integer)
For Each route In routes
Console.WriteLine(String.Join(", ", route))
Next
End Sub
End Module
当然,如果您根本不需要任何LINQ,下面是一个完全不依赖LINQ的递归实现:
public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(
this IEnumerable<IEnumerable<T>> sequences)
{
var accum = new List<T[]>();
var list = sequences.ToList();
if (list.Count > 0)
CartesianRecurse(accum, new Stack<T>(), list, list.Count - 1);
return accum;
}
static void CartesianRecurse<T>(List<T[]> accum, Stack<T> stack,
List<IEnumerable<T>> list, int index)
{
foreach (T item in list[index])
{
stack.Push(item);
if (index == 0)
accum.Add(stack.ToArray());
else
CartesianRecurse(accum, stack, list, index - 1);
stack.Pop();
}
}
它不会以与第一个函数相同的顺序返回项目,但在功能上是相同的。如果您不喜欢LINQ或递归,则这里有一个不使用LINQ的方法,它与递归版本的功能相同:
public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(
this IEnumerable<IEnumerable<T>> sequences)
{
var accum = new List<T[]>();
var list = sequences.ToList();
if (list.Count > 0)
{
var enumStack = new Stack<IEnumerator<T>>();
var itemStack = new Stack<T>();
int index = list.Count - 1;
var enumerator = list[index].GetEnumerator();
while (true)
if (enumerator.MoveNext())
{
itemStack.Push(enumerator.Current);
if (index == 0)
{
accum.Add(itemStack.ToArray());
itemStack.Pop();
}
else
{
enumStack.Push(enumerator);
enumerator = list[--index].GetEnumerator();
}
}
else
{
if (++index == list.Count)
break;
itemStack.Pop();
enumerator = enumStack.Pop();
}
}
return accum;
}