VB.NET组合数组x2
我想要两个字符串数组中所有可能的组合VB.NET组合数组x2,vb.net,combinations,Vb.net,Combinations,我想要两个字符串数组中所有可能的组合 两个数组的长度必须相同 结果必须保持秩序 例如: dim lStr1() as string = {"One", "Two", "Three"} dim lStr2() as string = {"EditOne", "EditTwo", "EditThree"} dim res() as string = myAwesomeFunction(lStr1, lStr2) // res : One Two Three One Two EditThree
- 两个数组的长度必须相同
- 结果必须保持秩序
dim lStr1() as string = {"One", "Two", "Three"}
dim lStr2() as string = {"EditOne", "EditTwo", "EditThree"}
dim res() as string = myAwesomeFunction(lStr1, lStr2)
// res :
One Two Three
One Two EditThree
One EditTwo Three
One EditTwo EditThree
EditOne Two Three
EditOne Two EditThree
EditOne EditTwo Three
EditOne EditTwo EditThree
这类似于两个字符串数组的二进制组合。下面的代码将在您的示例中生成该数组。它应该适用于任何一对输入数组。该函数检查输入数组的长度是否相同 GetPermutations函数取自我用于生成数字置换的更一般的类。它返回0和choose-1之间的
total
整数数组,作为迭代器函数,每次调用它时都返回下一个数组
为了匹配您的示例,我返回了一个字符串数组,其中每个元素都是单个字符串,由空格分隔的每个选定字符串组成。您可能会发现返回一个(字符串()的)列表甚至一个(字符串的)列表更有用
Private子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
Dim lStr1()作为字符串={“一”、“二”、“三”}
Dim lStr2()作为字符串={“EditOne”、“EditTwo”、“EditThree”}
Dim res()作为字符串=myAwesomeFunction(lStr1,lStr2)
端接头
函数MyAwesomeFunction(lStr1()作为字符串,lStr2()作为字符串)作为字符串()
Dim组合作为新列表(字符串)
如果lStr1.Length lStr2.Length,则抛出新的ArgumentException(“数组必须具有相同的长度”)
对于GetPermutations中作为整数的每个combo()(lStr1.Length,2)
Dim elem作为新列表(字符串)
对于i,整数=0到combo.Length-1
元素添加(如果(组合(i)=0,lStr1(i),lStr2(i)))
下一个
combos.Add(String.Join(“,elem))
下一个
返回combos.ToArray
端函数
公共迭代器函数GetPermutations(选择为整数,合计为整数)作为IEnumerable(属于整数())
Dim totals()作为整数=可枚举。重复(整数的)(总计,选择)。ToArray
Dim值(选择-1)作为整数
做
收益率
对于索引为整数=选择-1到0步骤-1
值(索引)+=1
如果值(索引)<总计(索引),则继续执行
值(索引)=0
下一个
退出Do
环
端函数
这是另一个解决方案。由于只涉及2个数组,我们可以通过bit fiddle获得所有的“组合”。&“
只是格式化输出以匹配示例
Private Function myAwesomeFunction(Array1() As String, Array2() As String) As String()
If Array1.Length <> Array2.Length Then
Throw New ArgumentException("Array lengths must be equal.")
End If
Dim combos(CInt(2 ^ Array1.Length) - 1) As String
For i As Integer = 0 To combos.Count - 1
For j = 0 To Array1.Length - 1
If (i And (1 << j)) > 0 Then
combos(i) += Array2(j) & " "
Else
combos(i) += Array1(j) & " "
End If
Next
Next
Return combos
End Function
私有函数myAwesomeFunction(Array1()作为字符串,Array2()作为字符串)作为字符串()
如果数组1.长度数组2.长度,则
抛出新ArgumentException(“数组长度必须相等”)
如果结束
Dim组合(CInt(2^Array1.Length)-1)作为字符串
对于i作为整数=0的组合。计数-1
对于j=0到阵列1。长度-1
如果(i)和(10),则
组合词(i)+=Array2(j)和“
其他的
组合体(i)+=Array1(j)和“
如果结束
下一个
下一个
返回组合
端函数
所有可能性都是6!(或720)除非有一些约束。但似乎有一些约束,因为示例结果只有3个元素。你说得对,这不是所有的可能性,因为我必须保持顺序!问题是我没有发现约束例外,它就像两个字符串数组之间的二进制。你还没有自己定义所有规则,所以我们将是guss根据您的需要进行编辑。例如,EditTwo-Two-Two-Two-Two-Two-Two
?不知道如何解释,但两个数组中只能使用一列,这是这里的难点:sMaybeGetPermutations(3,2)
应该是GetPermutations(lStr1.Length,2)
?是否最好使用If Array1.Length Array2.Length来比较数组的大小?两者之间的区别是什么?长度与计数。根据,答案是“是!”好主意!
Private Function myAwesomeFunction(Array1() As String, Array2() As String) As String()
If Array1.Length <> Array2.Length Then
Throw New ArgumentException("Array lengths must be equal.")
End If
Dim combos(CInt(2 ^ Array1.Length) - 1) As String
For i As Integer = 0 To combos.Count - 1
For j = 0 To Array1.Length - 1
If (i And (1 << j)) > 0 Then
combos(i) += Array2(j) & " "
Else
combos(i) += Array1(j) & " "
End If
Next
Next
Return combos
End Function