我需要在VB .NET中生成所有组合(而不是排列),我一直在搜索,我发现的只是排列(有些人说组合,但是当我尝试它时,所有都是排列). 我需要的是从字符串数组生成组合: Dim data_array As String
我需要的是从字符串数组生成组合:
Dim data_array As String() = {"one", "two", "three", "four", "five", "six"}
我需要它:
如果我只说一个长度,它必须返回:
one two three four five six
如果我说2长度,它必须回复:
oneone onetwo onethree onefour ... etc ... twoone twotwo twothree ... etc ...
并继续使用所有组合结束.
如果我说更多长度也这样做.
你说你想生成所有组合,但它看起来像是试图生成所有排列.如果您愿意,您可以尝试递归地执行此操作.如果您只想生成组合,那么在附加之前测试Root参数以查看它是否包含myStr.
Public Class Form1 Dim data_array As String() = {"one", "two", "three", "four", "five", "six"} Dim buffer As New List(Of String) Public Sub Permute(ByVal Root As String, ByVal Depth As Integer, ByVal Buffer As List(Of String)) For Each myStr As String In data_array If Depth <= 1 Then Buffer.Add(Root + myStr) Else Permute(Root + myStr, Depth - 1, Buffer) End If Next End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Permute("", 2, buffer) End Sub End Class