我想要两个字符串数组的所有可能组合。
例如:
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
这就像2个字符串数组的二进制组合。
答案 0 :(得分:1)
以下代码将在您的示例中生成数组。它适用于任何一对输入数组。该函数检查输入数组的长度是否相同。
GetPermutations函数取自我用于生成数字排列的更通用的类。它返回0 {和<script type="text/javascript">
//THIS IS PROTOTYPE VERSION 1.0 CREATE NEW FILE AND REVISE BEFORE FINAL COPY
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var squid = new Image();
squid.src = "squid.png";
var x = 15;
var y = 10;
setInterval(Main, 1000/60);
function Main() {
ctx.clearRect(0,0,1000,1000)
ctx.drawImage(squid,x,y)
y+= 1;
}
function jump() {
y -= 20;
}
function moveForward() {
x += 5;
}
document.onkeydown = function (e) {
if(e.which == 32){
jump();
}
if (e.which == ) {
moveForward();
}
}
</script>
之间的total
整数数组,并且作为Iterator函数,每次调用它时都会返回下一个数组。
为了匹配你的例子,我返回了一个String数组,其中每个元素都是一个字符串,由每个用空格分隔的选定字符串组成。您可能会发现返回List(Of String())甚至List(Of List(Of String))更有用
choose - 1
答案 1 :(得分:1)
这是另一种解决方案。由于只涉及2个阵列,我们可以小提琴来获得所有“组合”。 & " "
只是格式化输出以匹配示例。
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