各位大虾请帮帮忙
谢谢
9 个解决方案
#1
先排序,再循环比较。
#2
樓上的常規方法,
不知道有沒算法之類的。
不知道有沒算法之類的。
#3
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
For i = 0 To 5
For j = 0 To 5
If a(i) = b(j) Then
ReDim Preserve c(SameIndex) As String
c(SameIndex) = a(i)
SameIndex = SameIndex + 1
GoTo ExitFor
End If
Next
Debug.Print "数组a存在而数组b没有的数据:" & a(i)
ExitFor: Next
For i = 0 To UBound(c)
Print c(i)
Next
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 0 To 5
a(i) = Chr(65 + i)
Next
' a(0) = "A" 'a数组
' a(1) = "B"
' a(2) = "C"
' a(3) = "D"
' a(4) = "E"
' a(5) = "F"
'
b(0) = "K"
b(1) = "D"
b(2) = "F"
b(3) = "A"
b(4) = "O"
b(5) = "Z"
End Sub
Dim i As Integer
Dim j As Integer
For i = 0 To 5
For j = 0 To 5
If a(i) = b(j) Then
ReDim Preserve c(SameIndex) As String
c(SameIndex) = a(i)
SameIndex = SameIndex + 1
GoTo ExitFor
End If
Next
Debug.Print "数组a存在而数组b没有的数据:" & a(i)
ExitFor: Next
For i = 0 To UBound(c)
Print c(i)
Next
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 0 To 5
a(i) = Chr(65 + i)
Next
' a(0) = "A" 'a数组
' a(1) = "B"
' a(2) = "C"
' a(3) = "D"
' a(4) = "E"
' a(5) = "F"
'
b(0) = "K"
b(1) = "D"
b(2) = "F"
b(3) = "A"
b(4) = "O"
b(5) = "Z"
End Sub
#4
顶!楼上算法
#5
可以用hashTable
将数据A放入hashTable
然后用B中的元素 去hashtable中查询 (这里的查询不是for 循环) 直接去取
查询到的 hashtable 移掉
将数据A放入hashTable
然后用B中的元素 去hashtable中查询 (这里的查询不是for 循环) 直接去取
查询到的 hashtable 移掉
#6
寻找A和B中都存在的字符串:
Option Explicit
Function FindSameString(ByVal sArray1 As Variant, ByVal sArray2 As Variant) As Variant
Dim sResult() As String, iResult As Integer
Dim iLoop1 As Integer, iLoop2 As Integer
iResult = 0
For iLoop1 = LBound(sArray1) To UBound(sArray1)
For iLoop2 = LBound(sArray2) To UBound(sArray2)
If sArray2(iLoop2) = sArray1(iLoop1) Then
ReDim Preserve sResult(iResult)
sResult(iResult) = sArray1(iLoop1)
iResult = iResult + 1
Exit For
End If
Next iLoop2
Next iLoop1
FindSameString = sResult
End Function
Private Sub Form_Load()
Dim a(5) As String, b(3) As String
Dim c() As String, i As Integer
a(0) = "A"
a(1) = "BB"
a(2) = "CCC"
a(3) = "DDDD"
a(4) = "EEEEE"
a(5) = "FFFFFF"
b(0) = "BB"
b(1) = "AA"
b(2) = "CCC"
b(3) = "AAA"
c = FindSameString(a, b)
For i = LBound(c) To UBound(c)
Debug.Print c(i)
Next i
End
End Sub
Option Explicit
Function FindSameString(ByVal sArray1 As Variant, ByVal sArray2 As Variant) As Variant
Dim sResult() As String, iResult As Integer
Dim iLoop1 As Integer, iLoop2 As Integer
iResult = 0
For iLoop1 = LBound(sArray1) To UBound(sArray1)
For iLoop2 = LBound(sArray2) To UBound(sArray2)
If sArray2(iLoop2) = sArray1(iLoop1) Then
ReDim Preserve sResult(iResult)
sResult(iResult) = sArray1(iLoop1)
iResult = iResult + 1
Exit For
End If
Next iLoop2
Next iLoop1
FindSameString = sResult
End Function
Private Sub Form_Load()
Dim a(5) As String, b(3) As String
Dim c() As String, i As Integer
a(0) = "A"
a(1) = "BB"
a(2) = "CCC"
a(3) = "DDDD"
a(4) = "EEEEE"
a(5) = "FFFFFF"
b(0) = "BB"
b(1) = "AA"
b(2) = "CCC"
b(3) = "AAA"
c = FindSameString(a, b)
For i = LBound(c) To UBound(c)
Debug.Print c(i)
Next i
End
End Sub
#7
寻找A中存在而B中不存在的字符串, 或者寻找B中存在而A中不存在的字符串
Option Explicit
Function FindNotExistString(ByVal sArray1 As Variant, ByVal sArray2 As Variant) As Variant
Dim sResult() As String, iResult As Integer
Dim iLoop1 As Integer, iLoop2 As Integer
Dim bExist As Boolean
iResult = 0
For iLoop1 = LBound(sArray1) To UBound(sArray1)
bExist = False
For iLoop2 = LBound(sArray2) To UBound(sArray2)
If sArray2(iLoop2) = sArray1(iLoop1) Then
bExist = True
Exit For
End If
Next iLoop2
If Not bExist Then
ReDim Preserve sResult(iResult)
sResult(iResult) = sArray1(iLoop1)
iResult = iResult + 1
End If
Next iLoop1
FindNotExistString = sResult
End Function
Private Sub Form_Load()
Dim a(5) As String, b(3) As String
Dim c() As String, i As Integer
a(0) = "A"
a(1) = "BB"
a(2) = "CCC"
a(3) = "DDDD"
a(4) = "EEEEE"
a(5) = "FFFFFF"
b(0) = "BB"
b(1) = "AA"
b(2) = "CCC"
b(3) = "AAA"
'寻找A中存在而B中不存在的字符串
c = FindNotExistString(a, b)
For i = LBound(c) To UBound(c)
Debug.Print c(i)
Next i
'寻找B中存在而A中不存在的字符串
c = FindNotExistString(b, a)
For i = LBound(c) To UBound(c)
Debug.Print c(i)
Next i
End
End Sub
Option Explicit
Function FindNotExistString(ByVal sArray1 As Variant, ByVal sArray2 As Variant) As Variant
Dim sResult() As String, iResult As Integer
Dim iLoop1 As Integer, iLoop2 As Integer
Dim bExist As Boolean
iResult = 0
For iLoop1 = LBound(sArray1) To UBound(sArray1)
bExist = False
For iLoop2 = LBound(sArray2) To UBound(sArray2)
If sArray2(iLoop2) = sArray1(iLoop1) Then
bExist = True
Exit For
End If
Next iLoop2
If Not bExist Then
ReDim Preserve sResult(iResult)
sResult(iResult) = sArray1(iLoop1)
iResult = iResult + 1
End If
Next iLoop1
FindNotExistString = sResult
End Function
Private Sub Form_Load()
Dim a(5) As String, b(3) As String
Dim c() As String, i As Integer
a(0) = "A"
a(1) = "BB"
a(2) = "CCC"
a(3) = "DDDD"
a(4) = "EEEEE"
a(5) = "FFFFFF"
b(0) = "BB"
b(1) = "AA"
b(2) = "CCC"
b(3) = "AAA"
'寻找A中存在而B中不存在的字符串
c = FindNotExistString(a, b)
For i = LBound(c) To UBound(c)
Debug.Print c(i)
Next i
'寻找B中存在而A中不存在的字符串
c = FindNotExistString(b, a)
For i = LBound(c) To UBound(c)
Debug.Print c(i)
Next i
End
End Sub
#8
可以把数组a、b都jion成两个长的字符串(用逗号或其他特殊符号分隔,注意这个符号不能在任何一个元素里出现过),例如叫astr,bstr,然后用instr把a里的元素值都在bstr里找一次,如果在bstr里能找到,则把这个值记录在c数组里,然后把astr、bstr里所有这个元素都用replace去掉。。。。。循环结束后,c数组里的就是共有的元素,astr剩下的就是a里有b里没有的,bstr剩下的就是b里有a里没有的(astr、bstr剩下的元素可以用split从新放到数组里)。。。。
#9
谢谢,辛苦各位了
#1
先排序,再循环比较。
#2
樓上的常規方法,
不知道有沒算法之類的。
不知道有沒算法之類的。
#3
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
For i = 0 To 5
For j = 0 To 5
If a(i) = b(j) Then
ReDim Preserve c(SameIndex) As String
c(SameIndex) = a(i)
SameIndex = SameIndex + 1
GoTo ExitFor
End If
Next
Debug.Print "数组a存在而数组b没有的数据:" & a(i)
ExitFor: Next
For i = 0 To UBound(c)
Print c(i)
Next
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 0 To 5
a(i) = Chr(65 + i)
Next
' a(0) = "A" 'a数组
' a(1) = "B"
' a(2) = "C"
' a(3) = "D"
' a(4) = "E"
' a(5) = "F"
'
b(0) = "K"
b(1) = "D"
b(2) = "F"
b(3) = "A"
b(4) = "O"
b(5) = "Z"
End Sub
Dim i As Integer
Dim j As Integer
For i = 0 To 5
For j = 0 To 5
If a(i) = b(j) Then
ReDim Preserve c(SameIndex) As String
c(SameIndex) = a(i)
SameIndex = SameIndex + 1
GoTo ExitFor
End If
Next
Debug.Print "数组a存在而数组b没有的数据:" & a(i)
ExitFor: Next
For i = 0 To UBound(c)
Print c(i)
Next
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 0 To 5
a(i) = Chr(65 + i)
Next
' a(0) = "A" 'a数组
' a(1) = "B"
' a(2) = "C"
' a(3) = "D"
' a(4) = "E"
' a(5) = "F"
'
b(0) = "K"
b(1) = "D"
b(2) = "F"
b(3) = "A"
b(4) = "O"
b(5) = "Z"
End Sub
#4
顶!楼上算法
#5
可以用hashTable
将数据A放入hashTable
然后用B中的元素 去hashtable中查询 (这里的查询不是for 循环) 直接去取
查询到的 hashtable 移掉
将数据A放入hashTable
然后用B中的元素 去hashtable中查询 (这里的查询不是for 循环) 直接去取
查询到的 hashtable 移掉
#6
寻找A和B中都存在的字符串:
Option Explicit
Function FindSameString(ByVal sArray1 As Variant, ByVal sArray2 As Variant) As Variant
Dim sResult() As String, iResult As Integer
Dim iLoop1 As Integer, iLoop2 As Integer
iResult = 0
For iLoop1 = LBound(sArray1) To UBound(sArray1)
For iLoop2 = LBound(sArray2) To UBound(sArray2)
If sArray2(iLoop2) = sArray1(iLoop1) Then
ReDim Preserve sResult(iResult)
sResult(iResult) = sArray1(iLoop1)
iResult = iResult + 1
Exit For
End If
Next iLoop2
Next iLoop1
FindSameString = sResult
End Function
Private Sub Form_Load()
Dim a(5) As String, b(3) As String
Dim c() As String, i As Integer
a(0) = "A"
a(1) = "BB"
a(2) = "CCC"
a(3) = "DDDD"
a(4) = "EEEEE"
a(5) = "FFFFFF"
b(0) = "BB"
b(1) = "AA"
b(2) = "CCC"
b(3) = "AAA"
c = FindSameString(a, b)
For i = LBound(c) To UBound(c)
Debug.Print c(i)
Next i
End
End Sub
Option Explicit
Function FindSameString(ByVal sArray1 As Variant, ByVal sArray2 As Variant) As Variant
Dim sResult() As String, iResult As Integer
Dim iLoop1 As Integer, iLoop2 As Integer
iResult = 0
For iLoop1 = LBound(sArray1) To UBound(sArray1)
For iLoop2 = LBound(sArray2) To UBound(sArray2)
If sArray2(iLoop2) = sArray1(iLoop1) Then
ReDim Preserve sResult(iResult)
sResult(iResult) = sArray1(iLoop1)
iResult = iResult + 1
Exit For
End If
Next iLoop2
Next iLoop1
FindSameString = sResult
End Function
Private Sub Form_Load()
Dim a(5) As String, b(3) As String
Dim c() As String, i As Integer
a(0) = "A"
a(1) = "BB"
a(2) = "CCC"
a(3) = "DDDD"
a(4) = "EEEEE"
a(5) = "FFFFFF"
b(0) = "BB"
b(1) = "AA"
b(2) = "CCC"
b(3) = "AAA"
c = FindSameString(a, b)
For i = LBound(c) To UBound(c)
Debug.Print c(i)
Next i
End
End Sub
#7
寻找A中存在而B中不存在的字符串, 或者寻找B中存在而A中不存在的字符串
Option Explicit
Function FindNotExistString(ByVal sArray1 As Variant, ByVal sArray2 As Variant) As Variant
Dim sResult() As String, iResult As Integer
Dim iLoop1 As Integer, iLoop2 As Integer
Dim bExist As Boolean
iResult = 0
For iLoop1 = LBound(sArray1) To UBound(sArray1)
bExist = False
For iLoop2 = LBound(sArray2) To UBound(sArray2)
If sArray2(iLoop2) = sArray1(iLoop1) Then
bExist = True
Exit For
End If
Next iLoop2
If Not bExist Then
ReDim Preserve sResult(iResult)
sResult(iResult) = sArray1(iLoop1)
iResult = iResult + 1
End If
Next iLoop1
FindNotExistString = sResult
End Function
Private Sub Form_Load()
Dim a(5) As String, b(3) As String
Dim c() As String, i As Integer
a(0) = "A"
a(1) = "BB"
a(2) = "CCC"
a(3) = "DDDD"
a(4) = "EEEEE"
a(5) = "FFFFFF"
b(0) = "BB"
b(1) = "AA"
b(2) = "CCC"
b(3) = "AAA"
'寻找A中存在而B中不存在的字符串
c = FindNotExistString(a, b)
For i = LBound(c) To UBound(c)
Debug.Print c(i)
Next i
'寻找B中存在而A中不存在的字符串
c = FindNotExistString(b, a)
For i = LBound(c) To UBound(c)
Debug.Print c(i)
Next i
End
End Sub
Option Explicit
Function FindNotExistString(ByVal sArray1 As Variant, ByVal sArray2 As Variant) As Variant
Dim sResult() As String, iResult As Integer
Dim iLoop1 As Integer, iLoop2 As Integer
Dim bExist As Boolean
iResult = 0
For iLoop1 = LBound(sArray1) To UBound(sArray1)
bExist = False
For iLoop2 = LBound(sArray2) To UBound(sArray2)
If sArray2(iLoop2) = sArray1(iLoop1) Then
bExist = True
Exit For
End If
Next iLoop2
If Not bExist Then
ReDim Preserve sResult(iResult)
sResult(iResult) = sArray1(iLoop1)
iResult = iResult + 1
End If
Next iLoop1
FindNotExistString = sResult
End Function
Private Sub Form_Load()
Dim a(5) As String, b(3) As String
Dim c() As String, i As Integer
a(0) = "A"
a(1) = "BB"
a(2) = "CCC"
a(3) = "DDDD"
a(4) = "EEEEE"
a(5) = "FFFFFF"
b(0) = "BB"
b(1) = "AA"
b(2) = "CCC"
b(3) = "AAA"
'寻找A中存在而B中不存在的字符串
c = FindNotExistString(a, b)
For i = LBound(c) To UBound(c)
Debug.Print c(i)
Next i
'寻找B中存在而A中不存在的字符串
c = FindNotExistString(b, a)
For i = LBound(c) To UBound(c)
Debug.Print c(i)
Next i
End
End Sub
#8
可以把数组a、b都jion成两个长的字符串(用逗号或其他特殊符号分隔,注意这个符号不能在任何一个元素里出现过),例如叫astr,bstr,然后用instr把a里的元素值都在bstr里找一次,如果在bstr里能找到,则把这个值记录在c数组里,然后把astr、bstr里所有这个元素都用replace去掉。。。。。循环结束后,c数组里的就是共有的元素,astr剩下的就是a里有b里没有的,bstr剩下的就是b里有a里没有的(astr、bstr剩下的元素可以用split从新放到数组里)。。。。
#9
谢谢,辛苦各位了