将两个对象列表与字符串列表进行比较,并确保没有重叠



我有一个包含多个列表的对象。

Public Class aObject
    Public Property title
    Public Property aList As List(Of String)
    Public Property bList As List(Of String)
    Public Property cList As List(Of String)
    Public Property dList As List(Of String)
End Class

有另一个对象,我在其中存储所有对象。

Public Class bObject
 Private _LocalCollection As List(Of aObject)
 Private _RemoteCollection As List(Of aObject)
End Class

Object 中的每个列表都是不同的设置。如果我添加新设置,我希望能够确保所有组合永远不会交叉。因此,如果我将字母存储在 aList 中,将数字存储在 bList 中,并且有一个带有 {1,6,7} 和 {a,d,z} 的对象,我不想添加另一个包含列表 {2,6,8} 和 {a,f,g} 的设置。但是,我想添加列表 {1,6,7} 和 {b,c,f}。所有四个列表都是相同的方式。我需要检查这四个的组合。我可以使用递归算法并检查所有值,但我想知道是否有其他方法。

我使用以下建议的答案并实现了它:

Public Function checkDuplicates(ByVal strChampions As List(Of String), ByVal strSummonerSpells As List(Of String), ByVal strModes As List(Of String), ByVal strMaps As List(Of String)) As Boolean
    Dim booDuplicates As Boolean = False
    For Each setting In _LocalSettings
        Dim l1 = setting.champions.Intersect(strChampions)
        If l1.Count() > 0 Then
            Dim l2 = setting.summonerspells.Intersect(strSummonerSpells)
            If l2.Count() > 0 Then
                Dim l3 = setting.modes.Intersect(strModes)
                If l3.Count() > 0 Then
                    Dim l4 = setting.maps.Intersect(strMaps)
                    If l4.Count() > 0 Then
                        booDuplicates = booDuplicates Or True
' I am going to create the duplicate settings here to know where to overwrite.
                    End If
                End If
            End If
        End If
    Next
    Return booDuplicates
End Function

如果您检查列表的交集以查看是否有共同元素怎么办?

https://msdn.microsoft.com/en-us/library/bb460136(v=vs.110(.aspx

两个集合 A 和 B 的交集定义为包含 A 的所有元素的集合,这些元素也出现在 B 中,但没有其他元素。

Sub Main()
    Dim a As New List(Of String) From {"1", "6", "7"}
    Dim b As New List(Of String) From {"a", "d", "z"}
    Dim c As New List(Of String) From {"2", "6", "8"}
    Console.WriteLine(String.Format("a intersects b: {0}", a.Intersect(b).Count > 0))
    Console.WriteLine(String.Format("a intersects c: {0}", a.Intersect(c).Count > 0))
    Console.WriteLine(String.Format("b intersects c: {0}", b.Intersect(c).Count > 0))
    Console.ReadLine()
End Sub

输出:

a intersects b: False
a intersects c: True
b intersects c: False

相关内容

  • 没有找到相关文章

最新更新