我正在尝试合并2个数组,
SELECT JSON_EXTRACT_SCALAR(raw_json , "$[\"Type\"]") as parsed_type FROM `table` LIMIT 1000
.tooltip .arrow:before {
border-top-color: #008ec3 !important;
}
.tooltip .tooltip-inner {
background-color: #008ec3;
}
Dim arr1(3) As String
arr1(0) = "a"
arr1(1) = "b"
arr1(2) = "c"
arr1(3) = "d"
Dim arr2(4) As String
arr2(0) = "a"
arr2(1) = "x"
arr2(2) = "x"
arr2(3) = "b"
arr2(4) = "z"
Dim arr3 As String()
合并后会发生的情况是仅打印单个值
ReDim arr3(UBound(arr1) + UBound(arr2) + 1)
Dim i As Integer
For i = 0 To UBound(arr1)
arr3(i * 2) = arr1(i)
arr3(i * 2 + 1) = arr2(i)
Label8.Text = String.Join(",", arr3)
Dim r = From s In arr3
Group By s Into Group, Count()
Where Count = 1
Select s
Order By s
购买它只打印Label9.Text = String.Join(",", r)
,在合并两个应该为Next i
的数组后,它应该打印两个数组中出现一次的所有单个元素。我的代码有什么问题?谢谢。
答案 0 :(得分:2)
由于两个源数组可以具有不同的上限,因此您将不得不使用两者中的较高者进行迭代,并确保不要访问较短数组边界之外的索引
Dim arr1() As String = {"a", "b", "c", "d"}
Dim arr2() As String = {"a", "x", "x", "b", "z"}
Dim arr3(arr1.Length + arr2.Length - 1) As String
Dim iDestination As Integer = 0
For i As Integer = 0 To Math.Max(UBound(arr1), UBound(arr2))
If i <= UBound(arr1) Then
arr3(iDestination) = arr1(i)
iDestination += 1
End If
If i <= UBound(arr2) Then
arr3(iDestination) = arr2(i)
iDestination += 1
End If
Next
这与您进行的结果交错。
如果不需要交织并且您只对结果感兴趣(而不是对算法感兴趣),则可以执行以下操作:
Dim arr1() As String = {"a", "b", "c", "d"}
Dim arr2() As String = {"a", "x", "x", "b", "z"}
Dim r = From s In arr1.Concat(arr2)
Group By s Into Group, Count()
Where Count = 1 Select s
Order By s
这将返回在arr1
或arr2
中恰好出现一次的元素。
答案 1 :(得分:1)
使用Array.CopyTo
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim arr1(3) As String
arr1(0) = "a"
arr1(1) = "b"
arr1(2) = "c"
arr1(3) = "d"
Dim arr2(4) As String
arr2(0) = "a"
arr2(1) = "x"
arr2(2) = "x"
arr2(3) = "b"
arr2(4) = "z"
Dim arr3 As String()
ReDim arr3(arr1.Length + arr2.Length)
arr1.CopyTo(arr3, 0)
arr2.CopyTo(arr3, arr1.Length)
For Each s As String In arr3
Debug.Print(s)
Next
End Sub