2011-02-03 80 views
0

我如何使用另一種陣列VB6濾波器使用另一種陣列VB6

編輯 過濾陣列給定的陣列A,除去在從數組A

+2

,給予兩個數組「A」和「B」從`A`獲取元素數組,這個元素也存在於`B`中? – 2011-02-03 18:59:26

+0

沒有我添加更多信息 – Smith 2011-02-03 19:19:23

回答

0

我已經找到自己的答案,感謝所有誰作爲貢獻

Function FilterArray(ByVal Source As String, ByVal Search As String, Optional _ 
    ByVal Keep As Boolean = True) As String 


    Dim i As Long 
    Dim SearchArray()  As String 
    Dim iSearchLower  As Long 
    Dim iSearchUpper  As Long 

    If LenB(Source) <> 0 And LenB(Search) <> 0 Then 
     SearchArray = Split(Search, " ") 
    Else 
     FilterArray = Source 
     Exit Function 
    End If 
    iSearchLower = LBound(SearchArray) 
    iSearchUpper = UBound(SearchArray) 

    For i = iSearchLower To iSearchUpper 
     DoEvents 
     Source = Join(Filter(Split(Source, " "), SearchArray(i), Keep, _ 
      vbTextCompare), " ") 
    Next i 
    FilterArray = Source 
End Function 
0

數組B中的所有元件。在這種情況下,I」的陣列d只是對一個數組進行排序,然後遍歷第二個數組,如果發現它們,則從第一個數組中刪除它們。這個算法似乎需要O(n lg n)並且做你想做的事情。

0

假設他們爲整型數組:

Dim FilteredArray() As Integer 
Dim X as Long 
Dim Y as Long 
Dim Z as Long 
Dim bDupe as Boolean 

Z = -1 
For X = 0 to UBound(A) 
    bDupe = False 
    For Y = 0 to UBound(B) 
      If A(X) = B(Y) Then 
       bDupe = True 
       Exit For 
      End If 
    Next 
    If Not bDupe Then 
      Z = Z + 1 
      ReDim Preserve FilteredArray(Z) 
      FilteredArray(Z) = A(X) 
    End If 
Next 
+0

數組elemenets是字符串 – Smith 2011-02-04 08:15:22

0

嘗試是這樣的

Option Explicit 

Private Sub Form_Load() 
    Dim vElem   As Variant 

    For Each vElem In SubstractArray(Array("aa", "b", "test"), Array("c", "aa", "test")) 
     Debug.Print vElem 
    Next 
End Sub 

Private Function SubstractArray(arrSrc As Variant, arrBy As Variant) As Variant 
    Dim cIndex   As Collection 
    Dim vElem   As Variant 
    Dim vRetVal   As Variant 
    Dim lIdx   As Long 

    If UBound(arrSrc) < LBound(arrSrc) Then 
     Exit Function 
    End If 
    '--- build index collection 
    Set cIndex = New Collection 
    For Each vElem In arrBy 
     cIndex.Add vElem, "#" & vElem 
    Next 
    '--- allocate output array 
    lIdx = LBound(arrSrc) 
    ReDim vRetVal(lIdx To UBound(arrSrc)) As Variant 
    '--- iterate source and seek in index 
    For Each vElem In arrSrc 
     On Error Resume Next 
     IsObject cIndex("#" & vElem) 
     If Err.Number <> 0 Then 
      vRetVal(lIdx) = vElem 
      lIdx = lIdx + 1 
     End If 
     On Error GoTo 0 
    Next 
    '--- shrink output array 
    If lIdx = LBound(vRetVal) Then 
     vRetVal = Split(vbNullString) 
    Else 
     ReDim Preserve vRetVal(0 To lIdx - 1) As Variant 
    End If 
    SubstractArray = vRetVal 
End Function