一种打印无数内容的有效方法 [英] Efficient method to print the content of an Ienumerable
问题描述
我有一个结果"变量,它是IEnumerable
类型:
I have this "result" var which is a IEnumerable
type:
Dim result = GetCombinations(TextBox1.Text, StringLength)
要获取/写入变量的内容,我需要使用For来迭代内部的所有项目,然后将每个项目转换为数组,如下所示:
To get/write the content of the variable I need to iterate all the items inside using a For and then convert each item to an array, like this:
For Each item In result
RichTextBox1.Text += vbNewLine & item.ToArray
Application.DoEvents()
Next
...所以我的答案是,如果我可以改进代码,例如加入IEnumerable内容以执行其他操作:
...So my answer is If I can improve my code for example to join the IEnumerable content to do something like this else:
RichTextBox1.Text = String.Join(vbNewLine, result) ' This does not work.
我的意思是,一口气".
I mean, a "in one go" thing.
如果没有,是否有其他替代方案比For更好(更快)?
If not, any alternative better (faster) than the For?
更新
这是完整的代码:
Private Shared Function GetCombinations(Of T)(list As IEnumerable(Of T), length As Integer) As IEnumerable(Of IEnumerable(Of T))
If length = 1 Then
Return list.[Select](Function(x) New T() {x})
Else
Return GetCombinations(list, length - 1).SelectMany(Function(x) list, Function(t1, t2) t1.Concat(New T() {t2}))
End If
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Clear()
Dim result = GetCombinations("abc", 5)
' Dim result2 As IEnumerable(Of String) = result.Select(Function(item) New String(item))
' RichTextBox1.Text = String.Join(vbNewLine, result)
For Each item In result
RichTextBox1.Text &= vbNewLine & item.ToArray
' Application.DoEvents()
Next
End Sub
更新2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Method result
Dim result As IEnumerable = Permute_Characters("abc", 2)
' Combine strings into lines
' Dont work
RichTextBox1.Text = String.Join(Environment.NewLine, result.ToString.ToArray)
End Sub
推荐答案
如果要提高速度并减少内存占用,请减少递归和对LINQ的调用(这会隐藏内部循环).
If you want speed and reduced memory footprint, then reduce the recursions and calls to LINQ (which hide internal loops).
现在我已经了解了您想要的内容,请尝试以下代码
Now that I understand what you want, try this code
Public Function GetCombinations(ByVal letters As String, ByVal word_length As Integer) As String()
' abc,2 -> aa, ab, ac, ba, bb, bc, ca, cb, cc
' abc,3 -> aaa, aab, aac, aba, abb, abc, .. , caa, cab, cac
Dim list = letters.ToCharArray()
Dim N_letters = list.Length
Dim N_combinations = CLng(Math.Pow(N_letters, word_length))
Dim result As New List(Of String)(N_combinations)
' indeces holds which letter to use for each combination
' 0 = a, 1 = b, 2 = c, ...
' it starts with (0,0,0..) which corresponds to "aaa.."
Dim indeces As Integer() = New Integer(word_length-1) {}
'Default to 0's
Dim k As Integer
Do
' convert indeces into combination, example: (1,0,2) => (b,a,c) => "bac"
result.Add(New String(indeces.Select(Function(x) list(x)).ToArray()))
'Find next combination by incrementing the first letter,
'and if it spills-over, reset it to 'a' and try the next letter in the word
k = 0
Do
indeces(k) += 1
If indeces(k) = N_letters Then
indeces(k) = 0
k = k + 1
Else
Exit Do
End If
Loop While k < word_length
Loop While k < word_length
Return result.ToArray()
End Function
Sub Main()
Dim result = GetCombinations("abc", 5)
Debug.WriteLine(String.Join(ControlChars.CrLf, result))
End Sub
有结果
aaaaa
baaaa
caaaa
abaaa
bbaaa
cbaaa
acaaa
bcaaa
ccaaa
aabaa
babaa
...
caccc
abccc
bbccc
cbccc
acccc
bcccc
ccccc
这篇关于一种打印无数内容的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!