2017-02-15 91 views
0

我有一个包含多个列表的对象。比较两个对象列表与字符串列表并确保不重叠

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 

我有另一个对象,我存储所有的aObjects。

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

aObject中的每个列表都是不同的设置。如果我添加一个新的设置,我希望能够确保所有组合都不会交叉。因此,如果我在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 

回答

1

,如果您检查清单的路口查看是否有共同的要素是什么?

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 
+0

听起来不错。我必须重申所有的对象,但我意识到它的相交也很重要。这样,我可以覆盖或不覆盖。现在,我只需要弄清楚如何覆盖这些对象。谢谢。 –

+0

我可以显示我的完整代码以供将来参考。 –