在C#项目的N阵列的所有组合 [英] All combinations of items in N arrays in C#

查看:243
本文介绍了在C#项目的N阵列的所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以有不同的长度为N阵列结果
重要的是,我不知道有多少数组可以有结果
让我们有3个数组:

I can have N arrays of different length.
Important thing is that I dont know how many arrays I can have.
Let's we have 3 arrays:

arr1: { obj11, obj12, obj13, ...., obj1J }  
arr2: { obj21, obj22, obj23, .........., obj2K }  
arr3: { obj31, obj32, obj33, ................, obj3L }

我怎么能生产出每一个项目的组合与对方是这样的:

How can I produce combinations of each item with each other like this:

1.  [obj11, obj21, obj31]  
2.  [obj11, obj21, obj32]  
..  
L.  [obj11, obj21, obj 3L]  
L+1.[obj11, obj22, obj 31]  
.... 

说到C#实现(也许LINQ的?)

Talking about C# implementation (maybe Linq?)

推荐答案

您正在寻找所有序列的笛卡尔乘积。埃里克利珀有实现的一个很好的写在这里: http://ericlippert.com/2010/06/28/computing-a-cartesian-product-with-linq/

You're looking for the Cartesian Product of all the sequences. Eric Lippert has a nice write up of an implementation here: http://ericlippert.com/2010/06/28/computing-a-cartesian-product-with-linq/

这篇关于在C#项目的N阵列的所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆