2017-02-15 73 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

我可以顯示我的完整代碼以供將來參考。 –