比较 vb.net 中的两个数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17784901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparing two arrays in vb.net
提问by Random User
I need to do Reverse Intersectionoperation with two arrays and save the result in a different array
我需要对两个数组进行反向交集操作并将结果保存在不同的数组中
Eg: Array A {1, 2, 3}; Array B {1, 2, 3, 4, 5, 6} Resultant Array Should be {4, 5, 6}
例如:数组 A {1, 2, 3}; 数组 B {1, 2, 3, 4, 5, 6} 结果数组应该是 {4, 5, 6}
I Tried out the following logic but didn't work
我尝试了以下逻辑但没有用
int k = 0;
int a[2] = {1,10};
int p[10];
int roll[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i = 0; i < 2; i++)
{
for (int j = 1; j <= 10; j++)
{
if (a[i] == roll[j])
{
break;
}
else
{
p[k] = 0;
p[k] = roll[j];
k++;
}
}
}
I need it for my vb.net project
我的 vb.net 项目需要它
回答by Tim Schmelter
I don't understand how that C# code is related to your VB.NET problem, if you only want to find integerswhich are in one array and not in the other, use Enumerable.Except:
我不明白该 C# 代码与您的 VB.NET 问题有何关联,如果您只想查找integers在一个数组中而不在另一个数组中的内容,请使用Enumerable.Except:
Dim intsA = {1, 2, 3}
Dim intsB = {1, 2, 3, 4, 5, 6}
Dim bOnly = intsB.Except(intsA).ToArray()
回答by SysDragon
Try something like this if you can't use Linq:
如果您不能使用 Linq,请尝试以下操作:
Function RevIntersect(arr1() As String, arr2() As String) As String()
Dim sResult, aux As New List(Of String)()
aux.AddRange(arr1)
aux.AddRange(arr2)
For Each elem As String In aux
If (Not arr1.Contains(elem) OrElse Not Arr2.Contains(elem)) AndAlso _
Not sResult.Contains(elem) Then
sResult.Add(elem)
End If
Next
Return sResult.ToArray()
End Function

